Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 奇数ggplot堆叠条输出_R_Ggplot2_Dplyr_Tidyr - Fatal编程技术网

R 奇数ggplot堆叠条输出

R 奇数ggplot堆叠条输出,r,ggplot2,dplyr,tidyr,R,Ggplot2,Dplyr,Tidyr,我有几十个情节要复杂得多,但由于某种原因,我的大脑无法计算出这一个 给定一个数据帧和 # libraries require(ggplot2) require(dplyr) require(tidyr) # create data data <- data.frame("When"=c("(2008 - 2009)","(2010 - 2011)","(2012 - 2013)","(2014-2015)","Cannot Remember"),

我有几十个情节要复杂得多,但由于某种原因,我的大脑无法计算出这一个

给定一个数据帧和

# 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)) 
#库
需要(ggplot2)
需要(dplyr)
需要(三年)
#创建数据

数据您好您的
aes
中有
Y
的大写字母,您必须设置
stat=“identity”
,请尝试:

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

两个小时!!!!!对于大写的“Y”。。啊,啊,啊!!!!!!!谢谢你发现了