【问题标题】:Centre and offset labels in ggplot2ggplot2中的中心和偏移标签
【发布时间】:2020-06-11 11:49:08
【问题描述】:

我已经制定了一个情节,但我有两个问题需要帮助。

问题 1)

如何在条形的每个部分中居中标签? 在寻找解决方案时,我试图改变这一行:

  geom_text(aes(label= paste(round(pct),"%")), color = "white", size=2.6, position="fill", stat="identity") 

到

  geom_text(aes(label= paste(round(pct),"%")), color = "white", size=2.6, position=position_stack(vjust = 0.5))

这会将标签放在正确的位置,但现在彩色条不打印了。

问题 2)

将使用情节的地方,它将像这张图片一样狭窄:。

有没有办法自动处理重叠标签?另一个有用的解决方案可能是偏移所有其他标签,以便第一个标签在条的中间上方,第二个标签在中间下方(我希望这是有道理的)。也欢迎其他解决方案。

重现我的情节的代码:

library(ggplot2)

exdata <- structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 
                                            4L), .Label = c("Meget ofte", "Ofte", "Nogle gange", "Aldrig eller næsten aldrig"
                                            ), class = "factor"), Freq = c(4L, 16L, 50L, 38L, 6L, 5L, 55L, 
                                                                           43L), spm = c("Question 1: aaaaaabbbbbbbbbcccccc", "Question 1: aaaaaabbbbbbbbbcccccc", 
                                                                                         "Question 1: aaaaaabbbbbbbbbcccccc", "Question 1: aaaaaabbbbbbbbbcccccc", 
                                                                                         "Question 2: aaaaaabbbbbbbbbcccccc", "Question 2: aaaaaabbbbbbbbbcccccc", 
                                                                                         "Question 2: aaaaaabbbbbbbbbcccccc", "Question 2: aaaaaabbbbbbbbbcccccc"
                                                                           ), pct = c(3.7037037037037, 14.8148148148148, 46.2962962962963, 
                                                                                      35.1851851851852, 5.5045871559633, 4.58715596330275, 50.4587155963303, 
                                                                                      39.4495412844037)), row.names = 21:28, class = "data.frame")

palette1 <- c("#00ABA4", "#008983", "#006763", "#004543")

plot1 <- ggplot(data=exdata, aes(y=Freq, x=spm, fill= fct_rev(Var1))) +
  geom_bar(position="fill", stat="identity") +
  scale_fill_manual(values=c(palette1)) +
  geom_text(aes(label= paste(round(pct),"%")), color = "white", size=2.6, position="fill", stat="identity") + 
  coord_flip() +
  labs(fill = "Svar") +
  labs(title = "Hvor often do you do these things?") +
  ylab("Percentage") +
  xlab("Question") +
  theme(legend.position="bottom", legend.title=element_text(size=10)) +
  guides(fill =guide_legend(reverse = TRUE,nrow=2,byrow=TRUE)) 

plot1

【问题讨论】:

    标签: r ggplot2 bar-chart stacked-chart


    【解决方案1】:

    您可以尝试使用ggrepel 包中的geom_text_repel()。我还建议您切换回position="fill" 以正确放置标签。需要注意的一点是,您还应该将vjust= 应用于文本的position= 调整,就像您尝试使用position_stack() 一样。

    没有排斥标签,这就是它的样子(注意我改变了你的数据,以便标签之间有更多的重叠会很明显):

    # all code the same except for this line
    geom_text(aes(label= paste0(round(pct),"%")),
        color = "white", size=2.6, position=position_fill(vjust = 0.5))
    

    使用geom_text_repel,它看起来像这样。一定要加载library(ggrepel):

    geom_text_repel(aes(label= paste0(round(pct),"%")),
        direction='y',color = "white", size=2.6,
        position=position_fill(vjust = 0.5))
    

    【讨论】:

    • 非常感谢!这正是我所需要的。
    • 也许您可以帮我解决一个额外的问题:标签向上偏移和向下偏移的逻辑是否存在?并且可以控制吗?例如,在您的图像中,顶部栏是向上、向下、向上、向上,而底部栏是向上、向下、向上、向下。
    • 这有点难以指定...在其他情况下,您可以通过子集您的数据和将geom_text() 与一个和geom_text_repel() 与另一个一起使用...但这在这里不起作用,因为您使用position= 与geom_bar() 和position= 进行交互有点像序数数组-应用a 基于观察序列的数据数组的位置(不链接到数据集中的观察关系)。
    • 它实际上已经足够复杂,可以在 SO 中作为一个单独的问题提出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多