neversaint
neversaint

Reputation: 64024

How to add geom_smooth() or stat_smooth() on geom_jitter() in GGPLOT

I have the following script:

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'))

Which makes the figure like this:

enter image description here

My question is how can I add smoothing line for for the black (mean) points above?

I tried adding stat_smooth() but doesn't work.

Upvotes: 2

Views: 3070

Answers (2)

aosmith
aosmith

Reputation: 36086

You can do this directly via geom_smooth if you leave dose as numeric instead of making it a factor. For the color and shape mapping you can then either use factor(dose) or make a new categorical dose variable with a different name.

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

If you really want your x axis to be categorical you need to draw the smoothed line through the integers each factor level represents by using as.numeric on the categorical dose variables. This is drawing a smoothed line using the ranks of the x variable instead of the actual values, which may or may not make sense in your real situation.

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

Upvotes: 3

Hack-R
Hack-R

Reputation: 23200

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

Upvotes: 2

Related Questions