格式化 GGplot 堆叠条形图
Formatting GGplot stacked barplot
我正在制作一组记分卡,其中我正在生成一组图表,这些图表显示调查响应的分布以及特定公司的响应下降的位置。我需要修改图形、堆叠条形图的格式,并添加我在下面概述的一些功能。我已经花了几个小时将图表恢复到现在的状态,非常感谢您对我在下面概述的功能的帮助。
数据为
Data<-data.frame(Reviewed = c("Annually", "Annually", "Hourly", "Monthly", "Weekly","Monthly","Weekly","Other","Other","Monthly","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 轴。 IE。 0.25 变成 25%
- 将 y 轴直接移动到条形下方。
- 删除图例,但将术语移动到图表相应部分下方的对角线上。
- 添加一条削减 50% 标记的黑线
- 在堆栈的中点为公司“e”的值添加一个点。
- 删除灰色背景
这就是我希望完成的图表的样子。
这里有很多东西要拆开,所以我会一点一点地分解:
Order the variables so they are arranged on plot as follows: Annually,Monthly,Weekly,Hourly,Other
将“已审核”指定为有序因素。我在这里颠倒顺序,因为它想首先绘制“最低”因子(向左)。
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()
Express the y axis in terms of percent. I.e. 0.25 turns into 25%
使用scale_y_continuous(labels = scales::percent)
调整标签。我相信 scales
是在您安装 ggplot2
.
时引入的
ggplot(Data, aes(x="Question", fill=Reviewed)) +
geom_bar(position = 'fill') +
scale_y_continuous(labels = scales::percent) +
coord_flip()
Move y-axis directly underneath the bar.
Remove gray background
这些是通过将 expand = F
添加到 coord_flip
一次性完成的。
ggplot(Data, aes(x="Question", fill=Reviewed)) +
geom_bar(position = 'fill') +
scale_y_continuous(labels = scales::percent) +
coord_flip(expand = F)
Remove the legend...
添加theme(legend.position = 'none')
.
ggplot(Data, aes(x="Question", fill=Reviewed)) +
geom_bar(position = 'fill') +
scale_y_continuous(labels = scales::percent) +
coord_flip(expand = F) +
theme(legend.position = 'none')
but move the terms underneath the respective part of the graph on a diagonal slant.
这更难,需要大量的摆弄。
- 使用
geom_text
制作标签
- 使用 'count' 统计数据
计算柱上的位置
- 通过提供一个假的 x 坐标将标签移动到图的底部
- 使用
position_stack
对齐条形中心的标签,并使用 hjust
使它们紧靠 x 轴。
- 添加角度。
- 在
coord_flip
中使用 clip = 'off'
以确保这些值不会被删除,因为它们在绘图区域之外。
- Fiddle 使用 x 限制裁剪空白绘图区域。
- 调整
theme
中的图边距以确保可以看到所有内容。
ggplot(Data, aes(x="Question", 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, 0, 35, 10),
legend.position = 'none')
Add a black line that cuts down the 50% mark
使用geom_hline(yintercept = 0.5)
;请记住,这是一条“水平”线,因为坐标已翻转。
ggplot(Data, aes(x="Question", 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) +
geom_hline(yintercept = 0.5) +
scale_y_continuous(labels = scales::percent) +
coord_flip(xlim = c(0.555, 1.4), clip = 'off',expand = F) +
theme(plot.margin = margin(0, 0, 20, 10),
legend.position = 'none')
Add a dot in at the midpoint of the stack for the value of company “e”.
这很漂亮 hack-y。使用与 geom_text
中相同的 y 值,使用 geom_point
为 Reviewed
的每个值绘制一个点,然后使用 position_stack(0.5)
将它们推到条形的中心。然后使用 scale_color_manual
只为“每周”值着色(这是 Reviewed
对应 Company
“e”的对应值)。我确信有一种方法可以更编程地执行此操作。
ggplot(Data, aes(x="Question", 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) +
geom_hline(yintercept = 0.5) +
geom_point(aes(y = stat(..count../sum(..count..)),
color = Reviewed), stat = 'count',
position = position_stack(0.5), size = 5) +
scale_color_manual(values = 'black', limits = 'Weekly') +
scale_y_continuous(labels = scales::percent) +
coord_flip(xlim = c(0.555, 1.4), clip = 'off',expand = F) +
theme(plot.margin = margin(0, 0, 20, 10),
legend.position = 'none')
This is what I'm hoping the finished graph will look like.
美化事物:
ggplot(Data, aes(x="Question", 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) +
geom_hline(yintercept = 0.5) +
geom_point(aes(y = stat(..count../sum(..count..)),
color = Reviewed), stat = 'count',
position = position_stack(0.5), size = 5) +
scale_color_manual(values = 'black', limits = 'Weekly') +
scale_y_continuous(labels = scales::percent) +
coord_flip(xlim = c(0.555, 1.4), clip = 'off', expand = F) +
labs(x = NULL, y = NULL) +
theme_minimal() +
theme(plot.margin = margin(0, 0, 35, 10),
legend.position = 'none')
我正在制作一组记分卡,其中我正在生成一组图表,这些图表显示调查响应的分布以及特定公司的响应下降的位置。我需要修改图形、堆叠条形图的格式,并添加我在下面概述的一些功能。我已经花了几个小时将图表恢复到现在的状态,非常感谢您对我在下面概述的功能的帮助。
数据为
Data<-data.frame(Reviewed = c("Annually", "Annually", "Hourly", "Monthly", "Weekly","Monthly","Weekly","Other","Other","Monthly","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 轴。 IE。 0.25 变成 25%
- 将 y 轴直接移动到条形下方。
- 删除图例,但将术语移动到图表相应部分下方的对角线上。
- 添加一条削减 50% 标记的黑线
- 在堆栈的中点为公司“e”的值添加一个点。
- 删除灰色背景
这就是我希望完成的图表的样子。
这里有很多东西要拆开,所以我会一点一点地分解:
Order the variables so they are arranged on plot as follows: Annually,Monthly,Weekly,Hourly,Other
将“已审核”指定为有序因素。我在这里颠倒顺序,因为它想首先绘制“最低”因子(向左)。
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()
Express the y axis in terms of percent. I.e. 0.25 turns into 25%
使用scale_y_continuous(labels = scales::percent)
调整标签。我相信 scales
是在您安装 ggplot2
.
ggplot(Data, aes(x="Question", fill=Reviewed)) +
geom_bar(position = 'fill') +
scale_y_continuous(labels = scales::percent) +
coord_flip()
Move y-axis directly underneath the bar. Remove gray background
这些是通过将 expand = F
添加到 coord_flip
一次性完成的。
ggplot(Data, aes(x="Question", fill=Reviewed)) +
geom_bar(position = 'fill') +
scale_y_continuous(labels = scales::percent) +
coord_flip(expand = F)
Remove the legend...
添加theme(legend.position = 'none')
.
ggplot(Data, aes(x="Question", fill=Reviewed)) +
geom_bar(position = 'fill') +
scale_y_continuous(labels = scales::percent) +
coord_flip(expand = F) +
theme(legend.position = 'none')
but move the terms underneath the respective part of the graph on a diagonal slant.
这更难,需要大量的摆弄。
- 使用
geom_text
制作标签 - 使用 'count' 统计数据 计算柱上的位置
- 通过提供一个假的 x 坐标将标签移动到图的底部
- 使用
position_stack
对齐条形中心的标签,并使用hjust
使它们紧靠 x 轴。 - 添加角度。
- 在
coord_flip
中使用clip = 'off'
以确保这些值不会被删除,因为它们在绘图区域之外。 - Fiddle 使用 x 限制裁剪空白绘图区域。
- 调整
theme
中的图边距以确保可以看到所有内容。
ggplot(Data, aes(x="Question", 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, 0, 35, 10),
legend.position = 'none')
Add a black line that cuts down the 50% mark
使用geom_hline(yintercept = 0.5)
;请记住,这是一条“水平”线,因为坐标已翻转。
ggplot(Data, aes(x="Question", 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) +
geom_hline(yintercept = 0.5) +
scale_y_continuous(labels = scales::percent) +
coord_flip(xlim = c(0.555, 1.4), clip = 'off',expand = F) +
theme(plot.margin = margin(0, 0, 20, 10),
legend.position = 'none')
Add a dot in at the midpoint of the stack for the value of company “e”.
这很漂亮 hack-y。使用与 geom_text
中相同的 y 值,使用 geom_point
为 Reviewed
的每个值绘制一个点,然后使用 position_stack(0.5)
将它们推到条形的中心。然后使用 scale_color_manual
只为“每周”值着色(这是 Reviewed
对应 Company
“e”的对应值)。我确信有一种方法可以更编程地执行此操作。
ggplot(Data, aes(x="Question", 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) +
geom_hline(yintercept = 0.5) +
geom_point(aes(y = stat(..count../sum(..count..)),
color = Reviewed), stat = 'count',
position = position_stack(0.5), size = 5) +
scale_color_manual(values = 'black', limits = 'Weekly') +
scale_y_continuous(labels = scales::percent) +
coord_flip(xlim = c(0.555, 1.4), clip = 'off',expand = F) +
theme(plot.margin = margin(0, 0, 20, 10),
legend.position = 'none')
This is what I'm hoping the finished graph will look like.
美化事物:
ggplot(Data, aes(x="Question", 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) +
geom_hline(yintercept = 0.5) +
geom_point(aes(y = stat(..count../sum(..count..)),
color = Reviewed), stat = 'count',
position = position_stack(0.5), size = 5) +
scale_color_manual(values = 'black', limits = 'Weekly') +
scale_y_continuous(labels = scales::percent) +
coord_flip(xlim = c(0.555, 1.4), clip = 'off', expand = F) +
labs(x = NULL, y = NULL) +
theme_minimal() +
theme(plot.margin = margin(0, 0, 35, 10),
legend.position = 'none')