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!
