【问题标题】:How can I achieve this simple chart using ggplot2?如何使用 ggplot2 实现这个简单的图表?
【发布时间】:2019-07-18 07:44:37
【问题描述】:

假设我在名为 df1 的数据框中有以下数据,我如何使用 ggplot2 对其进行处理以获得以下输出(如下图 1 所示)?

df1如下图:

     Desc         Value
   Extremely       0.22
   Moderately      0.19
   Minimally       0.15
   Not at all      0.44

我还想要如图 1 所示的白色分隔符。

【问题讨论】:

标签: r ggplot2


【解决方案1】:

它应该与这样的东西一起使用:

library(ggplot2)

df <- data.frame(Desc=c("Extremely", "Moderately", "Minimally", "Not at all"), Value = c(0.22,0.19,0.15,0.44), 
                 cat = c(1,1,0,0), x.axis = c(1,1,1,1))

ggplot(df, aes(x=x.axis, y=Value, fill = Desc)) +
  geom_col(colour="white") +
  geom_text(aes(label = paste0(Value, "%")),
            position = position_stack(vjust = 0.5))+
  scale_fill_manual(values=c("#ff7f00", "#ff7f00", "#377eb8", "#377eb8")) +
  theme_minimal(base_size = 16) +
  ylab(NULL) +
  xlab(NULL) +
  coord_flip() +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        legend.position = "none") +
  annotate(geom = 'text', label = "Extremely", x = 0.5, y = 0.22, vjust = 0)+
  annotate(geom = 'text', label = "Moderately", x = 0.5, y = 0.535, vjust = 0)+
  annotate(geom = 'text', label = "Minimally", x = 0.5, y = 0.708, vjust = 0)+
  annotate(geom = 'text', label = "Not at all", x = 0.5, y = 0.89, vjust = 0)

【讨论】:

猜你喜欢
  • 2021-07-11
  • 2011-09-25
  • 2018-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
相关资源
最近更新 更多