如何解决格式化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()
我想执行以下操作:
- 对变量进行排序,以便将它们按以下方式排列在图上:每年,每月,每周,每小时,其他
- 以百分比表示y轴。即0.25变成25%
- 将y轴直接移动到条形下方。
- 删除图例,但将术语移动到图的相应部分下方的对角线上。
- 添加一条减少50%标记的黑线
- 在堆栈的中点添加一个点,以表示公司“ e”的价值。
- 删除灰色背景
解决方法
这里有很多东西要解压,所以我将一点一点分解:
对变量进行排序,以便将它们按以下顺序排列在图表上:每年,每月,每周,每小时,其他
将“已审核”分配为有序因素。我要在此颠倒顺序,因为它想先绘制“最低”因子(向左)。
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()
以百分比表示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()
将y轴直接移动到钢筋下方。 删除灰色背景
通过将expand = F
添加到coord_flip
一次完成所有操作。
ggplot(Data,fill=Reviewed)) +
geom_bar(position = 'fill') +
scale_y_continuous(labels = scales::percent) +
coord_flip(expand = F)
删除图例...
添加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')
但是将术语移动到图的相应部分下方的对角线上。
这更困难,需要花费很多时间。
- 使用
geom_text
制作标签 - 使用“计数”统计信息计算沿条的位置
- 通过提供伪造的x坐标将标签移动到图的底部
- 使用
position_stack
将标签对准条形的中心,并使用hjust
使标签与x轴邻接。 - 添加角度。
- 在
clip = 'off'
中使用coord_flip
,以确保这些值不在绘图区域内,因此不会被切掉。 - 轻按x限制以裁剪出空白的绘图区域。
- 调整
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')
添加一条削减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')
在堆栈的中点添加一个点,以表示公司“ 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')
这是我希望完成的图看起来像的样子。
漂亮的东西:
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')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。