【发布时间】:2020-06-30 13:06:02
【问题描述】:
正如标题,我有一个假数据:
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"))
data <- data %>% mutate(disease = as.factor(disease), group = as.factor(group))
中间的图片是这样的:
p1 <- data %>% ggplot(aes(x = disease, y = year, fill= incidence) ) +
geom_tile(color = 'black')+ ylab("") + xlab("") +
# coord_polar(start=0.11) +
xlim(c(unique(data$disease), "")) +
ylim(c(2005,2012+2)) +
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, color = group), size = 2) +
geom_segment(aes(x = as.numeric(disease)-0.5, xend = as.numeric(disease) + 0.5,
y = 2014, yend = 2014, group = group, color = group), size = 2) +
scale_color_manual(values = c('#1075BC', '#EE332E')) +
annotate('text', x = c(1.5, 3.5), y = 2013,
label = c('Group 1', 'Group 2'),
color = c('#1075BC', '#EE332E'))
然后我循环 p1 使用 coord_polar() 但文本“第 1 组”和“第 2 组”不是我预期的循环。
p1 + coord_polar(start=0.11)
还有其他方法可以处理Group 1和Group 2吗?
还有一个小问题:我有两个图例,一个是incidence,另一个是group。如何只显示 incidence 图例并删除 group 图例?
任何帮助将不胜感激!
【问题讨论】:
-
嗨,OP - 很确定您目前无法在
ggplot中制作循环文本,如 this would require an add-on package。否则,have you seen this post,这似乎与您的问题非常相似?最后,似乎有一种方法可以通过circlize包:useful reference here 在极坐标图中制作一些圆形文本。似乎是基础 R,而不是ggplot2。 -
这能回答你的问题吗? Making curved text on coord_polar
-
嗨@ chemdork123,我看到了你评论中的帖子,它没有解决我的问题。我看到了
circlize包,它真的是一个方便的包,我会更多地学习它。