Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fill by group is not showing up in ggplot

I am trying to make a stacked 100% area chart showing the distribution of two rider types (casual vs member) from hours between 0 and 24. However, my plot does not show up with separate fills for my group.

My table is the following:

start_hour_dist <- clean_trips  %>%
  group_by(start_hour, member_casual) %>%
  summarise(n = n()) %>%
  mutate(percentage = n / sum(n))

start_hour_dist table

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

my code for the plot is the following:

ggplot(start_hour_dist, mapping = aes(x=start_hour, y=percentage, fill=member_casual)) + 
  geom_area()

However, when I run the plot, my chart does not have the fill and looks like this:

plot

What can I do to make the plot show up something like this?
image from r-graph-gallery

Thanks!
Ben

>Solution :

Your problem is likely the start_hour column being passed as a character vector. Change to an integer first. For example:

library(tidyverse)

df <- tibble(start_hour = sprintf("%02d", rep(0:23, each = 2)),
       member_casual = rep(c("member", "casual"), times = 24),
       percentage = runif(48)) 




df |> 
  ggplot(mapping = aes(
    x = start_hour,
    y = percentage,
    fill = member_casual
  )) +
  geom_area()

This re-creates your blank graph:

Changing the column type first:


df |> 
  mutate(start_hour = as.integer(start_hour)) |> 
  ggplot(mapping = aes(
    x = start_hour,
    y = percentage,
    fill = member_casual
  )) +
  geom_area(position = "fill")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading