ggplot geom_bar,其中x =多列 [英] ggplot geom_bar where x = multiple columns
问题描述
如何制作条形图,其中X来自数据框的多个值?
How can I go about making a bar plot where the X comes from multiple values of a data frame?
假数据:
data <- data.frame(col1 = rep(c("A", "B", "C", "B", "C", "A", "A", "B", "B", "A", "C")),
col2 = rep(c(2012, 2012, 2012, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015)),
col3 = rep(c("Up", "Down", "Up", "Up", "Down", "Left", "Right", "Up", "Right", "Down", "Up")),
col4 = rep(c("Y", "N", "N", "N", "Y", "N", "Y", "Y", "Y", "N", "Y")))
我要做的是根据col1
,col2
,和col3
.
What I'm trying to do is plot the number (also, ideally, the percentage) of Y
's and N
's in col4
based on grouped by col1
, col2
, and col3
.
总体而言,如果有50行,而其中25行具有Y
,则我应该能够制作一个如下所示的图形:
Overall, if there are 50 rows and 25 of the rows have Y
's, I should be able to make a graph that looks like this:
我知道带有ggplot的基本条形图是:
I know a basic barplot with ggplot is:
ggplot(data, aes(x = col1, fil = col4)) + geom_bar()
我不是要查找col2
在col3
中每个col3
有多少col4
,所以我想facet_wrap()
并不是问题,但我不知道该怎么做代替.
I'm not looking for how many of col4
is found per col3
by col2
, though, so facet_wrap()
isn't the trick, I think, but I don't know what to do instead.
推荐答案
您需要先将数据帧转换为长格式,然后使用创建的变量设置facet_wrap()
.
You need to first convert your data frame into a long format, and then use the created variable to set the facet_wrap()
.
data_long <- tidyr::gather(data, key = type_col, value = categories, -col4)
ggplot(data_long, aes(x = categories, fill = col4)) +
geom_bar() +
facet_wrap(~ type_col, scales = "free_x")
这篇关于ggplot geom_bar,其中x =多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!