2012-12-03 26 views
0

我正在使用ggplot2繪製迴歸線。我也想用可信區間標出平均值以顯示它是否顯着。例如:如何在使用ggplot2時使繪圖縮放相同?

library("ggplot2") 
library("gridExtra") 
library("epicalc") 
options(digits=2) 

subset1 <- subset(na.omit(iris), Species == "setosa") 
subset2 <- subset(na.omit(iris), Species == "versicolor") 
subset3 <- subset(na.omit(iris), Species == "virginica") 

meanx <- c(ci(subset1$Sepal.Length)$mean, 
       ci(subset2$Sepal.Length)$mean, 
       ci(subset3$Sepal.Length)$mean) 

meany <- c(ci(subset1$Sepal.Width)$mean, 
      ci(subset2$Sepal.Width)$mean, 
      ci(subset3$Sepal.Width)$mean) 

Species <- factor(c("setosa", "versicolor", "virginica")) 
meanmatrix <- as.data.frame(cbind(Species, meanx, meany)) 

lowerx <- c(ci(subset1$Sepal.Length)$lower95ci, 
      ci(subset2$Sepal.Length)$lower95ci, 
      ci(subset3$Sepal.Length)$lower95ci) 

upperx <- c(ci(subset1$Sepal.Length)$upper95ci, 
      ci(subset2$Sepal.Length)$upper95ci, 
      ci(subset3$Sepal.Length)$upper95ci) 

lowery <- c(ci(subset1$Sepal.Width)$lower95ci, 
      ci(subset2$Sepal.Width)$lower95ci, 
      ci(subset3$Sepal.Width)$lower95ci) 

uppery <- c(ci(subset1$Sepal.Width)$upper95ci, 
      ci(subset2$Sepal.Width)$upper95ci, 
      ci(subset3$Sepal.Width)$upper95ci) 

px <- ggplot(data = meanmatrix, geom = 'blank', 
      aes(y = meanx, x = meany,color = factor(Species))) 
pbx <- px + 
    geom_point(size = 5) + 
    geom_errorbar(aes(ymin=lowerx, ymax=upperx), colour="black", width=.1) + 
    scale_color_manual(values = c("#00FFFF", "#FFFF00", "#00FF00")) + 
    theme(panel.background = element_rect(fill='white', colour='red'), 
     axis.title.x = element_blank(), 
     axis.title.y = element_blank(), 
     legend.position = "none") + 
    coord_flip() 


py <- ggplot(data = meanmatrix, geom = 'blank', 
      aes(y = meany, x = meany,color = factor(Species))) 
pby <- py + 
    geom_point(size = 5) + 
    geom_errorbar(aes(ymin=lowery, ymax=uppery), colour="black", width=.1) + 
    scale_color_manual(values = c("#00FFFF", "#FFFF00", "#00FF00")) + 
    theme(panel.background = element_rect(fill='white', colour='red'), 
     axis.title.x = element_blank(), 
     axis.title.y = element_blank(), 
     legend.position = "none") 

p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, 
        color = factor(Species))) 

p0 <- p + 
    scale_color_manual(values = c("#00FFFF", "#FFFF00", "#00FF00")) + 
    scale_linetype_manual(breaks = c("0","1"), values = c(1,2), labels = c("male", "female")) + 
    geom_smooth(method = "lm",se = FALSE, size = 1.2) + 
    theme(panel.background = element_rect(fill='white', colour='red'), 
     axis.title.x = element_blank(), 
     axis.title.y = element_blank(), 
     legend.position = "none") 

grid.newpage() 
pushViewport(viewport(layout = grid.layout(nrow=3, ncol=3))) 
print(p0,vp = viewport(layout.pos.row = 1:2, layout.pos.col = 2:3)) 
print(pby,vp = viewport(layout.pos.row = 1:2, layout.pos.col = 1))  
print(pbx,vp = viewport(layout.pos.row = 3, layout.pos.col = 2:3))  

這三個圖的比例是不同的。我如何使它們具有通用性,以便我可以比較它們?謝謝。 enter image description here

+0

什麼是你的代碼中使用的ci函數? – agstudy

+1

如何使用'scale_x_continuous()'和'scale_y_continuous()'設置軸的極限? –

+0

@agstudy我忘了添加庫部分。 ci是一個函數包是epicalc。 – Autumn

回答

1

與Ernest A.評論的一樣,您可以使用scale_x_continuousscale_y_continuous手動更改x軸和y軸的比例。只需將breaks參數設置爲相同的值即可。

也可能更容易繪製陰謀本身的不確定性條或繪製包括95%置信區間的迴歸線。

相關問題