Search code examples
rggplot2geom

geom_smooth no regression line


I have the following data and I am trying to make a scatter plot with regression line using ggplot2 package in R

Here is my code:

df <- read.table(text = "tumor  density fraction
A   2.31    0.224
B   2.76    0.042
C   1.51    0.039
D   1.48    0.429
E   1.97    0.081
F   1.82    0.026
G   1.5 0.083
H   2.38    0.043
I   2.52    0.105
J   1.47    0.200
K   2.17    0.049
L   1.5 0.022
M   2.03    0.100
N   2.2 0.021
O   1.8 0.000
P   1.62    0.055", header = T, sep = "\t")

ggplot(df,aes(x = density, y = fraction, color = tumor))+
  geom_point()+geom_text(label=df$tumor)+
  geom_smooth(method = "lm")

No regression line is plotted?

What can be the issue?


Solution

  • We could remove the color from the first aes and specify it in geom_point

    ggplot(df,aes(x = density, y = fraction))+
       geom_point(aes(color = tumor))+
       geom_smooth(method = 'lm')+ 
       geom_text(label=df$tumor)
    

    -output

    enter image description here

    data

    df <- structure(list(tumor = c("A", "B", "C", "D", "E", "F", "G", "H", 
    "I", "J", "K", "L", "M", "N", "O", "P"), density = c(2.31, 2.76, 
    1.51, 1.48, 1.97, 1.82, 1.5, 2.38, 2.52, 1.47, 2.17, 1.5, 2.03, 
    2.2, 1.8, 1.62), fraction = c(0.224, 0.042, 0.039, 0.429, 0.081, 
    0.026, 0.083, 0.043, 0.105, 0.2, 0.049, 0.022, 0.1, 0.021, 0, 
    0.055)), class = "data.frame", row.names = c(NA, -16L))