Home > Software design >  Fill in gaps in geom_area chart
Fill in gaps in geom_area chart

Time:05-28

When I produce a geom_area graph using the below code there are white gaps between the groups. Can anyone suggest a way to fill them in? I initially thought it was due to there not being zeros for each group in the code, when I add them, it just has a diagonal line from the top of the chart to the bottom. This is a common solution to the answers on SO but doesnt work in this example.

library(tidyverse)
date<-as.Date(c('2022-05-02','2022-05-09', '2022-05-16', '2022-05-23', '2022-05-30','2022-06-06'))
total<-c(1,1,1,1,1,1)
gp<-c( "group1","group1", "group2","group2","group3","group3")
data<-data.frame(date,total,gp, stringsAsFactors = FALSE)

data%>%  
  ggplot() 
  geom_area(aes(x=date, y=total, fill=gp, group=gp))

CodePudding user response:

Maybe you want something like this:

library(tidyverse)
data%>%  
  ggplot(aes(x = date, y = total, fill = gp, group = gp)) 
  geom_bar(stat = "identity", width = 7)  
  scale_x_date(breaks = '1 week', expand = c(0.01, 0.01))

Output:

enter image description here

  •  Tags:  
  • r
  • Related