Home > Software design >  Fill by group is not showing up in ggplot
Fill by group is not showing up in ggplot

Time:01-31

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

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

CodePudding user response:

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("d", 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")

  • Related