1

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?

user3138373
  • 519
  • 1
  • 6
  • 18

1 Answers1

1

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))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Is there an explanation as to why it won't work when we use color in the first aes as compared to using in geom_point. Also I need the tumor labels. Thanks for the time – user3138373 Aug 17 '21 at 23:18
  • can I color the tumor text with same color as the point? – user3138373 Aug 17 '21 at 23:21