2016-07-13 18 views
2
添加geom_smooth()或geom_jitter stat_smooth()()

我有以下腳本:如何GGPLOT

require(datasets) 
data(ToothGrowth) 
library(ggplot2) 
ToothGrowth$dose <- as.factor(ToothGrowth$dose) 
p <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose, shape=dose)) + 
    geom_jitter(position=position_jitter(0.2))+ 
    labs(title="Plot of length by dose",x="Dose (mg)", y = "Length") + stat_summary(fun.data=mean_sdl, size=0.6, geom="pointrange", color="black") 
p + theme_classic() + 
    theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), 
      axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) 

這使得數字是這樣的:

enter image description here

我的問題是我如何添加smoothing line以上的黑色(平均值)點?

我試着加入stat_smooth()但不起作用。

+3

的代碼是不可再生(你已經有了> 9000 SO代表,所以你知道這意味着什麼)。另外,3分的平滑線幾乎沒有意義。你真的想達到什麼目的? – hrbrmstr

+1

@hrbrmstr我讓它可以重現 –

回答

2
require(datasets) 
data(ToothGrowth) 
library(ggplot2) 
ToothGrowth$dose <- as.factor(ToothGrowth$dose) 
p <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose, shape=dose)) + 
    geom_jitter(position=position_jitter(0.2))+ 
    labs(title="Plot of length by dose",x="Dose (mg)", y = "Length") + stat_summary(fun.data=mean_sdl, size=0.6, geom="pointrange", color="black") 
p + theme_classic() + 
    theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), 
     axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) 

lo <- loess(as.numeric(dose)~len,data=ToothGrowth) 

p1 <- p+geom_line(aes(x=predict(lo))) 

enter image description here

3

你可以,如果你離開dose爲數字,而不是使其成爲一個因素直接通過geom_smooth做到這一點。對於顏色和形狀映射,您可以使用factor(dose)或使用不同的名稱創建新的分類劑量變量。

data(ToothGrowth) 
# New categorical dose for shapes and colors 
ToothGrowth$Dose <- factor(ToothGrowth$dose) 

ggplot(ToothGrowth, aes(x = dose, y = len, color = Dose, shape = Dose)) + 
    geom_jitter(position=position_jitter(0.2))+ 
    labs(title="Plot of length by dose",x="Dose (mg)", y = "Length") + 
    stat_summary(fun.data=mean_sdl, size=0.6, geom="pointrange", color="black") + 
    theme_classic() + 
    theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), 
     axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) + 
    geom_smooth(aes(x = dose, y = len), inherit.aes = FALSE, se = FALSE) 

enter image description here

如果你真的想你的X軸是分類的,你需要通過每個因子水平表示通過在分類劑量使用變量的as.numeric整數繪製平滑線。這正在使用x變量的行列而不是實際值繪製平滑線,這在實際情況中可能有意義也可能沒有意義。

ToothGrowth$Dose <- factor(ToothGrowth$dose) 

ggplot(ToothGrowth, aes(x = Dose, y = len, color = Dose, shape = Dose)) + 
    geom_jitter(position=position_jitter(0.2))+ 
    labs(title="Plot of length by dose",x="Dose (mg)", y = "Length") + 
    stat_summary(fun.data=mean_sdl, size=0.6, geom="pointrange", color="black") + 
    theme_classic() + 
    theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), 
     axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) + 
    geom_smooth(aes(x = as.numeric(Dose), y = len), inherit.aes = FALSE, se = FALSE) 

enter image description here