【问题标题】:Why coord_polar can not affect the text in annotate in ggplot为什么 coord_polar 不能影响 ggplot 中注释中的文本
【发布时间】: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 1Group 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 包,它真的是一个方便的包,我会更多地学习它。

标签: r ggplot2


【解决方案1】:

您不能在 ggplot2 中原生制作弯曲文本。但是,对于短文本注释,它实际上只是适当旋转文本的一种情况。关于删除group 图例,您只需在生成它的色阶调用中设置guide = FALSE

p1 <- data %>% ggplot(aes(x = disease, y = year, fill= incidence) ) + 
  geom_tile(color = 'black')+ ylab("") + xlab("") +
  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'), guide = FALSE) +
  annotate('text', x = c(1.5, 3.5), y = 2013, 
           label = c('Group 1', 'Group 2'), 
           color = c('#1075BC', '#EE332E'), angle = c(-70, -36), vjust = c(0, 1))

p1 + coord_polar()

【讨论】:

  • 非常感谢,这对我很有帮助。 BTW,在我的真实数据中,我有7组,所以我要一个一个找到合适的角度?如果我有很多组,有没有一种简单的方法可以做到这一点?
  • @zhiweili 你可以用一系列等距的角度来做到这一点。例如,如果您有 7 个标签均匀排列在一个圆圈周围,您可以使用 seq(0, -360, -360/8)[2:8] - 90。然而,这并不能解释你圈子中的白色差距,所以你可能需要将 360 乘以大约 0.8 来解释这个差距,可能类似于seq(0, -0.8 * 360, -(0.8 * 360)/8)[2:8]。此外,您需要正确分隔 x 值 - 可能像 seq(0.5, 4.5, 4/8)[2:8]
猜你喜欢
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 2021-08-14
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 2020-08-05
相关资源
最近更新 更多