ggplot2中的中心和偏移标签。

问题描述 投票:0回答:1

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

问题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)

凡是会用到的情节,都会像这张图一样,比较狭窄。this picture.

有没有一种方法可以自动处理重叠的标签?另一个有用的解决方案是把每一个标签都偏移,这样第一个标签就在柱子中间的上方,而第二个标签在中间的下方(我希望这是有意义的)。也欢迎其他的解决方案。

重现我的情节的代码。

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个回答
0
投票

你也许可以尝试使用 geom_text_repel() 来自 ggrepel 包。 我也建议你换回 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))

enter image description here

有了 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))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.