1

我有以下数据,我正在尝试使用 R 中的 ggplot2 包制作带有回归线的散点图

这是我的代码:

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

没有绘制回归线?

可能是什么问题?

4

1 回答 1

1

我们可以color从第一个中删除aes并指定它geom_point

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

-输出

在此处输入图像描述

数据

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))
于 2021-08-17T23:09:06.287 回答