如何使用ggplot2实现此简单图表?

时间:2019-07-18 07:44:37

标签: r ggplot2

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

figure 1

df1如下所示:

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

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

1 个答案:

答案 0 :(得分:0)

它应可与以下内容一起使用:

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)

enter image description here