奇数 ggplot 堆积条输出
Odd ggplot stacked bar output
我有几十个复杂得多的情节,但出于某种原因,我的大脑无法解决这个问题..
给定数据框和代码...
# libraries
require(ggplot2)
require(dplyr)
require(tidyr)
# create data
data <- data.frame("When"=c("(2008 - 2009)","(2010 - 2011)","(2012 - 2013)","(2014-2015)","Cannot Remember"),
"Friend"=c(2,7,15,3,0),
"News Website"=c(2,10,8,3,1),
"Printed Newspaper"=c(0,1,3,0,0),
"Academic Paper"=c(0,0,2,0,0),
"Online Forum"=c(6,16,25,6,1),
"Cannot Remember"=c(1,3,7,2,1)
)
# reshape to long format
data <- gather(data,var,val,2:7)
# plot stacked bar
ggplot(data[which(data$val>0),],aes(x=When, Y=val)) +
geom_bar( aes(fill=var))
我得到了一个堆叠的条形图,甚至尝试按照 重塑,但条形图(和子条形图)绝对不是 table 中的值,在 6 处达到顶峰,我不能对于我的一生,看看为什么。到目前为止,摆弄了 2 个小时,我还是做不对。
谁能救救我的脑残粉?
您好,您的 aes
中有大写的 Y
,您必须设置 stat = "identity"
,请尝试:
ggplot(data[which(data$val>0),],aes(x=When, y=val)) +
geom_bar( aes(fill=var), stat = "identity")
我有几十个复杂得多的情节,但出于某种原因,我的大脑无法解决这个问题..
给定数据框和代码...
# libraries
require(ggplot2)
require(dplyr)
require(tidyr)
# create data
data <- data.frame("When"=c("(2008 - 2009)","(2010 - 2011)","(2012 - 2013)","(2014-2015)","Cannot Remember"),
"Friend"=c(2,7,15,3,0),
"News Website"=c(2,10,8,3,1),
"Printed Newspaper"=c(0,1,3,0,0),
"Academic Paper"=c(0,0,2,0,0),
"Online Forum"=c(6,16,25,6,1),
"Cannot Remember"=c(1,3,7,2,1)
)
# reshape to long format
data <- gather(data,var,val,2:7)
# plot stacked bar
ggplot(data[which(data$val>0),],aes(x=When, Y=val)) +
geom_bar( aes(fill=var))
我得到了一个堆叠的条形图,甚至尝试按照
谁能救救我的脑残粉?
您好,您的 aes
中有大写的 Y
,您必须设置 stat = "identity"
,请尝试:
ggplot(data[which(data$val>0),],aes(x=When, y=val)) +
geom_bar( aes(fill=var), stat = "identity")