格式化GGplot堆叠barplot

问题描述

我正在制作一组记分卡,在其中生成一组图表,这些图表显示了来自调查的响应的分布,以及特定公司的响应下降的位置。我需要修改图形的格式,堆积的条形图,并添加一些我在下面概述的功能。我已经花了几个小时将图表移到现在的位置,非常感谢您对我在下面概述的功能的帮助。

数据是

Data<-data.frame(Reviewed = c("Annually","Annually","Hourly","Monthly","Weekly","Other","Weekly"),Company=c("a","b","c","d","e","f","g","h","i","j","k"),Question="Q1")

到目前为止,我已经开发了这个

ggplot(Data,aes(x="Question",fill=Reviewed)) + geom_bar(position='fill' ) +
  coord_flip()

enter image description here

我想执行以下操作:

  • 对变量进行排序,以便将它们按以下方式排列在图上:每年,每月,每周,每小时,其他
  • 以百分比表示y轴。即0.25变成25%
  • 将y轴直接移动到条形下方。
  • 删除图例,但将术语移动到图的相应部分下方的对角线上。
  • 添加一条减少50%标记的黑线
  • 在堆栈的中点添加一个点,以表示公司“ e”的价值。
  • 删除灰色背景

这就是我希望完成的图形看起来像的样子。

enter image description here

解决方法

这里有很多东西要解压,所以我将一点一点分解:

对变量进行排序,以便将它们按以下顺序排列在图表上:每年,每月,每周,每小时,其他

将“已审核”分配为有序因素。我要在此颠倒顺序,因为它想先绘制“最低”因子(向左)。

Data$Reviewed <- factor(Data$Reviewed,levels = rev(c('Annually','Monthly','Weekly','Hourly','Other')),ordered = T)

ggplot(Data,aes(x="Question",fill=Reviewed)) + geom_bar(position='fill' ) +
  coord_flip()

enter image description here

以百分比表示y轴。即0.25变成25%

使用scale_y_continuous(labels = scales::percent)调整标签。我相信scales是在安装ggplot2时插入的。

ggplot(Data,fill=Reviewed)) +
  geom_bar(position = 'fill') +
  scale_y_continuous(labels = scales::percent) +
  coord_flip()

enter image description here

将y轴直接移动到钢筋下方。 删除灰色背景

通过将expand = F添加到coord_flip一次完成所有操作。

ggplot(Data,fill=Reviewed)) +
  geom_bar(position = 'fill') +
  scale_y_continuous(labels = scales::percent) +
  coord_flip(expand = F)

enter image description here

删除图例...

添加theme(legend.position = 'none')

ggplot(Data,fill=Reviewed)) +
  geom_bar(position = 'fill') +
  scale_y_continuous(labels = scales::percent) +
  coord_flip(expand = F) +
  theme(legend.position = 'none')

enter image description here

但是将术语移动到图的相应部分下方的对角线上。

这更困难,需要花费很多时间。

  1. 使用geom_text制作标签
  2. 使用“计数”统计信息计算沿条的位置
  3. 通过提供伪造的x坐标将标签移动到图的底部
  4. 使用position_stack将标签对准条形的中心,并使用hjust使标签与x轴邻接。
  5. 添加角度。
  6. clip = 'off'中使用coord_flip,以确保这些值不在绘图区域内,因此不会被切掉。
  7. 轻按x限制以裁剪出空白的绘图区域。
  8. 调整theme中的绘图边距,以确保可以看到所有内容。
ggplot(Data,fill=Reviewed)) +
  geom_bar(position = 'fill') +
  geom_text(aes(label = Reviewed,x = 0.45,y = stat(..count../sum(..count..))),stat = 'count',position = position_stack(0.5),hjust = 0,angle = 45) +
  scale_y_continuous(labels = scales::percent) +
  coord_flip(xlim = c(0.555,1.4),clip = 'off',expand = F) +
  theme(plot.margin = margin(0,35,10),legend.position = 'none')

enter image description here

添加一条削减50%标记的黑线

使用geom_hline(yintercept = 0.5);请记住,这是一条“水平”线,因为坐标已翻转。

ggplot(Data,angle = 45) +
  geom_hline(yintercept = 0.5) +
  scale_y_continuous(labels = scales::percent) +
  coord_flip(xlim = c(0.555,20,legend.position = 'none')

enter image description here

在堆栈的中点添加一个点,以表示公司“ e”的价值。

这是很糟糕的。使用与geom_text中相同的y值,使用geom_point为每个Reviewed的值绘制一个点,然后使用position_stack(0.5)将其微移到条形的中心。然后使用scale_color_manual仅为“每周”值着色(这是Reviewed“ e”的Company的对应值)。我敢肯定,有一种方法可以通过编程来做到这一点。

ggplot(Data,angle = 45) +
  geom_hline(yintercept = 0.5) +
  geom_point(aes(y = stat(..count../sum(..count..)),color = Reviewed),size = 5) +
  scale_color_manual(values = 'black',limits = 'Weekly') +
  scale_y_continuous(labels = scales::percent) +
  coord_flip(xlim = c(0.555,legend.position = 'none')

enter image description here

这是我希望完成的图看起来像的样子。

漂亮的东西:

ggplot(Data,fill = Reviewed)) +
  geom_bar(position = 'fill') +
  geom_text(aes(label = Reviewed,expand = F) +
  labs(x = NULL,y = NULL) +
  theme_minimal() +
  theme(plot.margin = margin(0,legend.position = 'none')

enter image description here