棒棒糖图表行上的文本左对齐

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

希望有一个棒棒糖图表,其本质上是在线顶部的轴文本。现在这是我的代码:

temp <- data.frame("question" = c("Better meeting planning, i.e. organized with sufficient notice",
                                  "Clearer communication of meeting objectives and agenda",
                                  "Debriefing contributors who did not attend the meeting",
                                  "Having the right participants",
                                  "Meeting Frequency Meeting Duration",
                                  "Meetings starting and ending on time",
                                  "Respect during meetings (behavior and tone of leaders)",
                                  "Sticking to the meeting agenda",
                                  "Taking decisions and clearly defining a call to action"),
                   "n" = c(45, 51, 30, 27, 22, 34, 34, 22, 37), 
                   "per" = c(0.15, 0.17, 0.1, 0.09, 0.07, 0.11, 0.11, 0.07, 0.12))

ggplot(temp, aes(reorder(question, per), per,)) +
  geom_segment(aes(x=reorder(question, per), xend=reorder(question, per), y=0, yend=per), color="#F7C52A") +
  geom_point(color = "#F7C52A", size=4, alpha=1) +
  geom_text(aes(label = reorder(question, per)),
            size = 3, hjust = 1.05, vjust = -0.9, color = "black") +
  theme_minimal() +
  theme(legend.position = "n", axis.text.x = element_blank(), panel.grid = element_blank(), axis.text.y = element_blank()) +
  labs(x = "", y = "", fill = "") +
  coord_flip()

Current Chart

差不多就这样了,但是文本是右对齐的并且锚定在要点上,我明白为什么会这样。还考虑尝试从 x 轴移动文本,但也无法使其工作。

谢谢!

r ggplot2 charts
1个回答
0
投票
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.3.2

temp <- data.frame("question" = c("Better meeting planning, i.e. organized with sufficient notice",
                                  "Clearer communication of meeting objectives and agenda",
                                  "Debriefing contributors who did not attend the meeting",
                                  "Having the right participants",
                                  "Meeting Frequency Meeting Duration",
                                  "Meetings starting and ending on time",
                                  "Respect during meetings (behavior and tone of leaders)",
                                  "Sticking to the meeting agenda",
                                  "Taking decisions and clearly defining a call to action"),
                   "n" = c(45, 51, 30, 27, 22, 34, 34, 22, 37), 
                   "per" = c(0.15, 0.17, 0.1, 0.09, 0.07, 0.11, 0.11, 0.07, 0.12))

ggplot(temp, aes(per, reorder(question, per))) +
  geom_segment(aes(x = 0, 
                   xend = per,
                   y = reorder(question, per), 
                   yend = reorder(question, per)), color="#F7C52A") +
  geom_point(color = "#F7C52A", size=4, alpha=1) +
  geom_text(aes(x = 0, label = reorder(question, per)),
            size = 3, 
            hjust = 0, 
            vjust = -0.9, 
            color = "black") +
  theme_minimal() +
  theme(legend.position = "n", 
        axis.text.x = element_blank(), 
        panel.grid = element_blank(), 
        axis.text.y = element_blank()) +
  labs(x = NULL, 
       y = NULL, 
       fill = NULL)

创建于 2024-04-09,使用 reprex v2.0.2

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