2016-04-23 72 views
1

几十个地块的要复杂得多,但由于某种原因,我的大脑是不工作这一个我有..奇ggplot叠置条输出

给出一个数据帧和代码...

# 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)) 

我得到一个堆叠酒吧,甚至尝试根据this post重塑,但酒吧(和子酒吧)绝对不是在表中的价值观,6号顶部,我不能为我的生活看到为什么。到目前为止2小时的摆弄,我无法得到这个权利。

Not the plot I was expecting

请有人可以救我的怪僻的大脑?

回答

2

嗨,你有大写Yaes,你必须设置stat = "identity",尝试:

ggplot(data[which(data$val>0),],aes(x=When, y=val)) + 
    geom_bar(aes(fill=var), stat = "identity") 
+0

两个小时!!!!!对于大写字母“Y”.. arghhhhhh !!!!!!!感谢您的发现 – BarneyC