2014-10-03 71 views
1

遇到问题,我有以下数据:使用GGPLOT2功能stat_smooth

#means and SEs 
s2humanlikem<-c(1.895,1.658,2.684,2.421,2.921,2.158,3.632,2.737,4.526,4.105) 
s2humanlikese<-c(.199,.157,.250,.234,.243,.225,.197,.235,.145,.180) 
s2agent<-c("Software","Software","Machine","Machine","Robot","Robot","Android","Android","Human","Human") 
Story<-c("Cooperative","Self-interested") 

#combine into dataframe 
hri2<-data.frame(s2agent,s2humanlikem,s2humanlikese,Story) 

#make numeric and factor 
hri2$s2agent <- factor(hri2$s2agent, levels = c("Software","Machine", "Robot", "Android","Human")) 
hri2$s2humanlikem<-as.numeric(levels(hri2$s2humanlikem))[hri2$s2humanlikem] 
hri2$s2humanlikese<-as.numeric(levels(hri2$s2humanlikese))[hri2$s2humanlikese] 

,我试图用下面的代码绘制:

​​

但是你可以看到,图中的轴是不可思议的。由于某种原因,这两条线也没有对齐,我不明白为什么。任何帮助,将不胜感激。

感谢您的关注!

回答

0

转换你x到数字和去除stat_smoothseq得到期望的结果:

ggplot(hri2,aes(x=as.numeric(s2agent),y=s2humanlikem,group=Story)) + 
    stat_smooth(se=FALSE,method="lm",formula=y~poly(x,4)) + 
    scale_x_continuous(breaks=seq(length(unique(hri2$s2agent))),labels=levels(hri2$s2agent)) 

这导致了以下情节: enter image description here


更新:当你要包括图中的SE,您必须根据您计算的meanse的原始数据框进行绘图。

如果你想使用你提供的数据,例如,你可以使用以下命令:

ggplot(hri2,aes(x=s2agent,y=s2humanlikem,group=Story)) + 
    geom_line(color="blue",size=1) + 
    geom_ribbon(aes(ymin=s2humanlikem-s2humanlikese, ymax=s2humanlikem+s2humanlikese), alpha=0.3) + 
    scale_x_discrete(expand=c(0,0)) + 
    theme_bw() 

这给: enter image description here

+0

啊哈!我应该尝试过。非常感谢。 – smgmu 2014-10-03 15:29:46

+0

有没有办法在这里使用se = TRUE?这个函数似乎是手动计算标准错误,但我已经在数据框中指定了它。 (误差线可以工作,但阴影区域会很好。)谢谢。 – smgmu 2014-10-03 15:39:30

+0

不,因为每个组的每个x值只有一个观察值。您无法从一个值计算SE。 – Jaap 2014-10-03 15:54:27