2

As the title, I had a fake data:

data <-
structure(list(year = c(2010, 2011, 2012, 2010, 2011, 2012, 2010, 
2011, 2012, 2010, 2011, 2012), disease = c(1, 1, 1, 2, 2, 2, 
3, 3, 3, 4, 4, 4), group = c("A", "A", "A", "A", "A", "A", "B", 
"B", "B", "B", "B", "B"), incidence = c(0.2, 0.3, 0.4, 0.1, 0.3, 
0.2, 0.5, 0.6, 0.7, 0.8, 0.9, 0.3)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

I want to draw a line to show different group (disease 1 and disease 2 belong to group A with one color, disease 3 and disease 4 belong to group B with another color), But the line draw to the year labels not to the last category and also I can not set fill and color in geom_segment:

data <- data %>% mutate(disease = as.factor(disease), group =  as.factor(group))
data %>% ggplot(aes(x = disease, y = year, fill= incidence) ) + 
  geom_tile(color = 'black')+ ylab("") + xlab("") +
  xlim(c("",unique(data$disease))) + 
  ylim(c(2005,2012+1)) + 
  annotate(x="",y=2010:2012,label=2010:2012,size=2.5,geom="text")+
  theme_bw()+
  theme_classic()+
  theme(
    axis.line = element_blank(),
    axis.ticks = element_blank(),
    axis.text.y = element_blank()
    
  ) + 
 geom_segment(aes(x = as.numeric(disease)+0.5, xend = as.numeric(disease)-0.5,
                 y = 2009, yend = 2009, group = group, fill = group), size = 2, color = 'black') 

Any help will be highly appreciated!

2
  • Why not use facetting? Commented Jun 30, 2020 at 5:49
  • @PaulvanOppen. Because there are many groups in my real data and I want to draw a circular heatmap. This is just a part of my code. You can see the my expected plot at here [stackoverflow.com/questions/62556246/… Commented Jun 30, 2020 at 5:52

1 Answer 1

3

Simply replace geom_line by geom_segment:

library(ggplot2)

ggplot(data) + 
  geom_tile(aes(x = disease, y = year, fill= incidence), color = 'black') +
  geom_segment(aes(x = disease - 0.5, xend = disease + 0.5, y = 2009, yend = 2009, group = group, color = group)) +
  ylab("") + 
  xlab("") +
  xlim(c(1:4,"")) + 
  ylim(c(2005,2012+1)) + 
  annotate(x="",y=2010:2012,label=2010:2012,size=2.5,geom="text")+
  theme_bw()+
  theme_classic()+
  theme(
    axis.line = element_blank(),
    axis.ticks = element_blank(),
    axis.text.y = element_blank()
    
  )

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, it works. But I also have some problems and I update my question. 1) In my real data, disease is a factor and the year labels were on the left, so the new code in the question was closer to my situation. The result is not my expected. Because it draws to the labels not to the last category. 2) I want to use fill = group within aes() and color = black without aes() in geom_segment() but failed. Is there a way to set fill and color meanwhile in geom_segment()?
Okay. I see.1) The factor is not an issue. In that case convert to numeric. And set the labels via scale_x_continuous(labels = c(...)). But I'm not sure whether geom_segment will work for the kind of cirular plot you are trying to achieve. 2) geom_segment or geom_line have no fill aesthetic. If you want a fill you can try geom_rect
Thanks a lot and there is just one question left : Why the line draw to year labels not to the last category.
1) Concerning the fill. geom_rect will not work easily as you already have a fill scale. 2) Simply add + 1 in geom_segemnt i.e. as.numeric(disease) + 1 to shift the starting and end point one category to the right.
... just in case I meant as.numeric(disease) + 1 +/- 0.5. (;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.