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 in gaps in geom_area chart

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))

>Solution :

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

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

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