仅在某些组中的回归线(ggplot2)

时间:2018-11-21 15:29:17

标签: r ggplot2 regression

嗨,我正在尝试仅将回归线添加到Le和Aw组中。尝试使用子集,但没有用。不知道我在做什么错。 这是我的代码:

# Set up the work directory in which all data is gonna be extracted
H1517 = read.csv("Test_Subsetv3.csv") #Change name of the file
# Load the ggplot2 package
library(ggplot2) #Run to create plots
library(grid)
library(cowplot)
library(gridExtra)

test <- ggplot(H1517,aes(PI,FE,color=factor(Seg)))+
  geom_point()+
  geom_smooth(data=subset(H1517,Seg==Le | 
  Seg==Aw),aes(PI,FE,color=factor(Seg)),method=lm,se=FALSE)

test

然后我得到下一个错误:

  

eval(e,x,parent.frame())中的错误:找不到对象'Le'

test  
  

错误:找不到对象“测试”

dput格式的数据。
OP在注释中发布了此示例数据集。

H1517 <-
structure(list(FE = c(69.27030884, 60.62345885, 
59.87889173, 69.27030884, 60.62345885, 59.87889173, 
69.27030884, 60.62345885, 59.87889173, 71.11906144, 
69.27030884, 60.62345885, 59.87889173, 61.88615029, 
71.11906144, 69.27030884, 60.62345885, 59.87889173),
PI = c(0.203496781, 0.197520353, 0.209443661, 
0.110640382, 0.116677419, 0.10343242, 0.299820599,
 0.303724997, 0.261057467, 0.125785204, 0.237294948,
 0.249327695, 0.275266774, 0.300414044, 0.283862484,
 0.148747292, 0.13326041, 0.149232038),
 Seg = structure(c(2L, 2L, 2L, 4L, 4L, 4L, 3L, 3L,
 3L, 4L, 1L, 1L, 1L, 3L, 3L, 5L, 5L, 5L),
 .Label = c("Aw", "Glu", "Le", "Pa", "Ra"),
 class = "factor")), row.names = c(NA, 
-18L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

这对我有用。

test <- ggplot(H1517, aes(PI, FE, color = Seg)) +
  geom_point() +
  geom_smooth(data = subset(H1517, Seg %in% c("Le", "Aw")),
              aes(PI, FE, color = Seg),
              method = lm, se = FALSE)

test

enter image description here

数据。

H1517 <-
structure(list(FE = c(69.27030884, 60.62345885, 
59.87889173, 69.27030884, 60.62345885, 59.87889173, 
69.27030884, 60.62345885, 59.87889173, 71.11906144, 
69.27030884, 60.62345885, 59.87889173, 61.88615029, 
71.11906144, 69.27030884, 60.62345885, 59.87889173),
PI = c(0.203496781, 0.197520353, 0.209443661, 
0.110640382, 0.116677419, 0.10343242, 0.299820599,
 0.303724997, 0.261057467, 0.125785204, 0.237294948,
 0.249327695, 0.275266774, 0.300414044, 0.283862484,
 0.148747292, 0.13326041, 0.149232038),
 Seg = structure(c(2L, 2L, 2L, 4L, 4L, 4L, 3L, 3L,
 3L, 4L, 1L, 1L, 1L, 3L, 3L, 5L, 5L, 5L),
 .Label = c("Aw", "Glu", "Le", "Pa", "Ra"),
 class = "factor")), row.names = c(NA, 
-18L), class = "data.frame")