[英]Multiple stacked bar chart with ggplot
I have a dataset with four variables measuring respondents' view on different topics.我有一个数据集,其中有四个变量来衡量受访者对不同主题的看法。 I want to plot them into one stacked bar chart so you can compare the values between the different topics.
我想将它们 plot 放入一个堆叠条形图中,以便您可以比较不同主题之间的值。
This are the first rows of the dataset:这是数据集的第一行:
lebanon <- structure(list(climate_change = c(
"Not a very serious problem",
"Not a very serious problem", NA, NA, "A very serious problem",
"A somewhat serious problem"
), air_quality = c(
"A somewhat serious problem",
"Not a very serious problem", NA, NA, "A very serious problem",
"A very serious problem"
), water_polution = c(
"A somewhat serious problem",
"Not a very serious problem", NA, NA, "A very serious problem",
"Not at all a serious problem"
), trash = c(
"A very serious problem",
"Not a very serious problem", NA, NA, "A very serious problem",
"A somewhat serious problem"
)), row.names = c(NA, -6L), class = "data.frame")
I did try with the following code based on this site :我确实尝试了基于 此站点的以下代码:
lebanon %>%
filter(!is.na(climate_change), !is.na(air_quality), !is.na(water_polution), !is.na(trash)) %>%
gather(variable, value, climate_change:trash) %>%
ggplot(aes(x = variable, y = value, fill = value)) +
geom_bar(stat = "identity") +
coord_flip()
Getting this graph:获取此图:
There are three problems with this graph.这张图存在三个问题。
1.) The bar graphs are not the same length. 1.) 条形图的长度不同。
2.) I don't why there is something written at the location where x-axis hits the y-axis. 2.) 我不明白为什么在 x 轴碰到 y 轴的位置写了一些东西。 How do I remove this?
我该如何删除这个?
3.) I want to order the values so they make sense, so I orderer them before with: 3.)我想对这些值进行排序,以便它们有意义,所以我先对它们进行排序:
dataset$climate_change <- factor(dataset$climate_change, levels = c("Not at all a serious problem",
"Not a very serious problem",
"A somewhat serious problem",
"A very serious problem"))
dataset$air_quality <- factor(dataset$air_quality, levels = c("Not at all a serious problem",
"Not a very serious problem",
"A somewhat serious problem",
"A very serious problem"))
dataset$water_polution <- factor(dataset$water_polution, levels = c("Not at all a serious problem",
"Not a very serious problem",
"A somewhat serious problem",
"A very serious problem"))
Yet the values are still unorderer.然而,这些值仍然是无序的。 What am I doing wrong?
我究竟做错了什么? Or is there a more effective way to make a multiple stacked bar chart?
或者有没有更有效的方法来制作多重堆叠条形图?
The main issue with cour code is that you mapped value
, ie a factor var, on y
. cour 代码的主要问题是您在
y
上映射了value
,即因子 var。 Further you can simply use drop_na
instead of filter and simply that the levels of value after the gather instead of repeating it for each var.此外,您可以简单地使用
drop_na
而不是过滤器,并且只需在收集之后使用值的级别,而不是为每个 var 重复它。 (; Try this: (; 尝试这个:
BTW: Please put your data into the post with dput()
, eg dput(head(lebanon))
.顺便说一句:请使用
dput()
将您的数据放入帖子中,例如dput(head(lebanon))
。 See my edit to your post.请参阅我对您帖子的编辑。 Took more time to clean and get the data right than answering the question.
与回答问题相比,清理和正确获取数据需要更多时间。 (;
(;
** EDIT ** To get the bars ordered in the wanted order I make use of the forcats
package. ** 编辑 ** 为了按想要的顺序订购酒吧,我使用了
forcats
package。 First I add_count
the number of respondents thinking the issue is "A very serious problem".首先,我
add_count
认为该问题是“一个非常严重的问题”的受访者数量。 Then I fct_reorder
variable
accordingly, ie -n
to get it descending.然后我
fct_reorder
variable
,即-n
使其降序。 To reverse the order of value
I make use of fct_rev
.为了颠倒
value
顺序,我使用了fct_rev
。
lebanon <- structure(list(climate_change = c(
"Not a very serious problem",
"Not a very serious problem", NA, NA, "A very serious problem",
"A somewhat serious problem"
), air_quality = c(
"A somewhat serious problem",
"Not a very serious problem", NA, NA, "A very serious problem",
"A very serious problem"
), water_polution = c(
"A somewhat serious problem",
"Not a very serious problem", NA, NA, "A very serious problem",
"Not at all a serious problem"
), trash = c(
"A very serious problem",
"Not a very serious problem", NA, NA, "A very serious problem",
"A somewhat serious problem"
)), row.names = c(NA, -6L), class = "data.frame")
library(tidyverse)
lebanon %>%
drop_na() %>%
gather(variable, value, climate_change:trash) %>%
add_count(variable, value == "A very serious problem") %>%
mutate(value = factor(value, levels = c("Not at all a serious problem",
"Not a very serious problem",
"A somewhat serious problem",
"A very serious problem"))) %>%
ggplot(aes(x = forcats::fct_reorder(variable, -n), fill = forcats::fct_rev(value))) +
geom_bar() +
coord_flip()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.