2015-07-02 63 views
0

如何使用geom_smooth()函數來突出顯示我的圖形。 。但我無法得到解決方案。使用geom_smooth()或stat_summary()在R中突出顯示差異

這是我的數據集爲:

A1  A2  A3 
21.09542 71.06014 0 
21.09564 71.06064 1 
21.09619 71.06128 1 
21.09636 71.06242 2 
21.09667 71.06564 0 
21.09483 71.06619 3 
..... 

我已經計算了A1和A2的變化,並通過ggplot繪製。 一塊代碼如下:

var.A1 = var(A1) 
var.A2 = var(A2) 
plt <- ggplot(df4, aes(x = A1, y = A2, colour = A3), pch = 17) +geom_point() 
plt + geom_errorbar(aes(x=A1,y=A2, ymin=A2-var.A2, ymax=A2+var.A2),width=0.001)+ 
geom_errorbarh(aes(xmin=A1-var.A1,xmax=A1+var.A1),height=0.001) 

回答

0

我不知道你的問題是什麼,但是當我與您的數據試了一下,似乎有沒有足夠的點來生成更平滑。所以我產生了更多的觀點。

# Generate the original data (I think) 
raw <- c(
21.09542, 71.06014, 0, 
21.09564, 71.06064, 1, 
21.09619, 71.06128, 1, 
21.09636, 71.06242, 2, 
21.09667, 71.06564, 0, 
21.09483, 71.06619, 3) 
m <- matrix(raw,6,3,byrow=T) 
df4 <- data.frame(m) 
names(df4) <- c("A1","A2","A3") 
df4$A3 <- as.factor(df4$A3) 


# Generate a new data set of 100 points with similar statistics 
npts <- 100 
na1 <- rnorm(npts,mean(df4$A1),sd(df4$A1)) 
na2 <- rnorm(npts,mean(df4$A2),sd(df4$A2)) 
na3 <- sample(0:3,npts,replace=T) 
df5 <- data.frame(A1=na1,A2=na2,A3=na3) 
df5$A3 <- as.factor(df5$A3) 

# now plot it 
var.A1 = var(df5$A1) 
var.A2 = var(df5$A2) 

plt <- ggplot(df5, aes(x = A1, y = A2, colour = A3), pch = 17) +geom_point() + geom_smooth() 
plt + geom_errorbar(aes(x=A1,y=A2, ymin=A2-var.A2, ymax=A2+var.A2),width=0.001)+ 
    geom_errorbarh(aes(xmin=A1-var.A1,xmax=A1+var.A1),height=0.001) + geom_smooth() 

print(plt) 

這是情節的樣子。 enter image description here

+0

邁克這不是我想要的。我也製作了同樣的情節,但是按照A3繪製。我想強調整個路徑的標準偏差。 我正在更新問題,以便您可以查看它。 – ayush

+0

邁克你可以查看這[鏈接](http://stackoverflow.com/questions/31228362/how-can-i-highlight-variance-over-a-ggplot)。清楚地顯示了它。這正是我需要的 – ayush