【问题标题】:Regression line just in certain groups (ggplot2)仅在某些组中的回归线(ggplot2)
【发布时间】:2018-11-21 15:29:16
【问题描述】:

您好,我正在尝试将回归线添加到 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")

【问题讨论】:

  • 应该是subset(H1517, Seg=="Le" | Seg=="Aw")
  • @Lyngbakr 或者subset(H1517, Seg %in% c("Le", "Aw"))
  • 您可以发布示例数据吗?请使用dput(H1517) 的输出编辑问题。或者,如果 dput(head(H1517, 20)) 的输出太大。
  • FE PI波段69.27030884 0.203496781谷氨酸60.62345885 0.197520353谷氨酸59.87889173 0.209443661谷氨酸69.27030884 0.110640382帕60.62345885 0.116677419霸59.87889173 0.10343242帕69.27030884 0.299820599勒60.62345885 0.303724997勒59.87889173 0.261057467勒71.11906144 0.125785204帕69.27030884 0.237294948胡60.62345885 0.249327695胡59.87889173 0.275266774胡61.88615029 0.300414044 Le 71.11906144 0.283862484 Le 69.27030884 0.148747292 Ra 60.62345885 0.13326041 Ra 59.87889173 0.149232038 Ra
  • @RuiBarradas 我尝试了你的方法,结果出现了:错误:mapping 必须由aes() 创建

标签: r ggplot2 regression


【解决方案1】:

这对我有用。

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

数据。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 2020-01-12
    • 2015-01-02
    相关资源
    最近更新 更多