简体   繁体   English

没有显示ggplot2上的Stat_smooth

[英]Stat_smooth on ggplot2 not showing

Im trying to add a lm line to my ggplot's, the code looks like this: 我试图在我的ggplot中添加一行lm,代码如下所示:

# RING data:
#### Read data & Converting factors ####
dat <- read.table("RING R kopi.txt", header=TRUE)  
str(dat)
dat$Vial <- as.factor(dat$Vial)
dat$Line <- as.factor(dat$Line)
dat$rep <- as.factor(dat$rep)
dat$fly <- as.factor(dat$fly)  
str(dat)

datSUM <- summaryBy(t05+t10+t15+t20+t25+t30~rep+Conc+Sex+Line+Vial,data=dat, FUN=sum)
fl<-levels(datSUM$Line)
datA <- droplevels(datSUM[datSUM$Conc=="a",])
datB <- droplevels(datSUM[datSUM$Conc=="b",])
datC <- droplevels(datSUM[datSUM$Conc=="c",]) 
datD <- droplevels(datSUM[datSUM$Conc=="d",])
datE <- droplevels(datSUM[datSUM$Conc=="e",])
datX <- droplevels(datSUM[datSUM$Conc=="x",])
datY <- droplevels(datSUM[datSUM$Conc=="y",])


c <- ggplot(Line, t05.sum, data= datA, facets=Sex~rep)
c + stat_smooth(method=lm, fullrange = TRUE) + geom_point()

The script runs but no line is added, i have also tried using 该脚本运行但没有添加任何行,我也尝试过使用

c + stat_smooth(method=lm) + geom_point()
c + stat_smooth(method=lm, fullrange=TRUE, alpha = 0.05) + geom_point()
c <- qplot(aes(y=Line, x=t05.sum), data= datA, facets=Sex~rep)
c + stat_smooth(method=lm, fullrange = TRUE) + geom_point()

The result: 结果: 在此输入图像描述

subset of dataset: 数据集子集:

structure(list(rep = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3"), class = "factor"), Conc = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "a", class = "factor"), Sex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L ), .Label = c("f", "m"), class = "factor"), Line = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L), .Label = c("20", "23", "40", "73"), class = "factor"), Vial = structure(c(3L, 14L, 24L, 31L, 38L, 4L, 12L, 21L, 26L, 35L, 6L, 8L, 9L, 37L, 40L, 2L, 10L, 11L, 28L, 33L, 13L, 15L, 18L, 22L, 27L), .Label = c("89", "92", "102", "112", "113", "118", "124", "126", "137", "138", "139", "152", "154", "155", "161", "163", "166", "168", "171", "173", "179", "182", "184", "185", "187", "201", " 结构(列表(rep =结构(c)(1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,1L,1L),. Label = c(“1”,“2”,“3”),class =“factor”),Conc =结构(c(1L,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L),。标签=“a”, class =“factor”),性别=结构(c(1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L ,1L,2L,2L,2L,2L,2L),. Label = c(“f”,“m”),class =“factor”),线=结构(c(1L,1L,1L,1L,1L) ,2L,2L,2L,2L,2L,3L,3L,3L,3L,3L,4L,4L,4L,4L,4L,1L,1L,1L,1L,1L),。标签= c(“20” ,“23”,“40”,“73”),类=“因子”),Vial =结构(c(3L,14L,24L,31L,38L,4L,12L,21L,26L,35L,6L,8L) ,9L,37L,40L,2L,10L,11L,28L,33L,13L,15L,18L,22L,27L),.标签= c(“89”,“92”,“102”,“112”,“ 113“,”118“,”124“,”126“,”137“,”138“,”139“,”152“,”154“,”155“,”161“,”163“,”166“ ,“168”,“171”,“173”,“179”,“182”,“184”,“185”,“187”,“201”,“ 203", "205", "215", "218", "220", "222", "225", "231", "240", "245", "251", "252", "260", "267"), class = "factor"), t05.sum = c(39.15, 63.99, 44.55, 25.65, 54.24, 8.37, 72.09, 19.17, 56.43, 78.24, 140.13, 29.43, 19.71, 100.32, 92.16, 11.61, 71.55, 76.41, 123.39, 142.29, 56.97, 66.15, 21.33, 66.15, 33.75), t10.sum = c(59.67, 79.65, 60.21, 8.91, 85.44, 15.93, 101.79, 41.85, 76.95, 84.96, 98.55, 36.45, 31.59, 115.2, 92.16, 26.88, 90.99, 95.31, 127.17, 173.61, 74.79, 81.27, 38.61, 80.73, 101.25), t15.sum = c(78.57, 107.73, 80.73, 59.67, 118.56, 39.15, 98.01, 75.33, 76.95, 90.24, 122.85, 41.31, 51.57, 126.24, 90.24, 43.99, 111.51, 119.07, 134.73, 190.89, 118.53, 121.47, 68.64, 97.47, 120.69 ), t20.sum = c(92.61, 144.45, 109.35, 89.37, 139.68, 59.13, 103.41, 109.89, 74.25, 105.6, 127.71, 54.81, 75.87, 132.96, 93.12, 65.07, 147.15, 127.17, 147.69, 212.49, 163.35, 138.51, 89.91, 120.15, 142.29), t25.sum = c(102.33, 162.27, 127.71, 113.13, 168.96, 76.41, 125.55, 135.81, 74.79, 114.72, 136.35, 203“,”205“,”215“,”218“,”220“,”222“,”225“,”231“,”240“,”245“,”251“,”252“,”260“ ,“267”),类=“因子”),t05.sum = c(39.15,63.99,44.55,25.65,54.24,8.37,72.09,19.17,56.43,78.24,140.13,29.43,19.71,100.32,92.16,11.61 ,71.55,76.41,123.39,142.29,56.97,66.15,21.33,66.15,33.75),t10.sum = c(59.67,79.65,60.21,8.91,85.44,15.93,101.79,41.85,76.95,84.96,98.55,36.45, 31.59,115.2,92.16,26.88,90.99,95.31,127.17,173.61,74.79,81.27,38.61,80.73,101.25),t15.sum = c(78.57,107.73,80.73,59.67,118.56,39.15,98.01,75.33,76.95 ,90.24,122.85,41.31,51.57,126.24,90.24,43.99,111.51,119.07,134.73,190.89,118.53,121.47,68.64,97.47,120.69),t20.sum = c(92.61,144.45,109.35,89.37,139.68, 59.13,103.41,109.89,74.25,105.6,127.71,54.81,75.87,132.96,93.12,65.07,147.15,127.17,147.69,212.49,163.35,138.51,89.91,120.15,142.29),t25.sum = c(102.33,162.27) ,127.71,113.13,168.96,76.41,125.55,135.81,74.79,114.72,136.35, 68.31, 95.85, 139.2, 92.64, 111.51, 185.49, 129.87, 145.53, 220.05, 196.83, 151.47, 136.35, 121.23, 154.17), t30.sum = c(127.71, 177.93, 171.45, 148.23, 195.84, 93.69, 145.53, 166.05, 86.13, 122.88, 147.15, 91.53, 119.07, 142.08, 92.16, 147.69, 200.07, 134.73, 140.67, 226.53, 219.51, 150.93, 155.25, 124.47, 144.99 )), .Names = c("rep", "Conc", "Sex", "Line", "Vial", "t05.sum", "t10.sum", "t15.sum", "t20.sum", "t25.sum", "t30.sum"), row.names = c(NA, 25L), class = "data.frame") 68.31,95.85,139.2,92.64,111.51,185.49,129.87,145.53,220.05,196.83,151.47,136.35,121.23,154.17),t30.sum = c(127.71,177.93,171.45,148.23,195.84,93.69,145.53,166.05 ,86.13,122.88,147.15,91.53,119.07,142.08,92.16,147.69,200.07,134.73,140.67,226.53,219.51,150.93,155.25,124.47,144.99)),. Name = c(“rep”,“Conc”, “性别”,“线”,“小瓶”,“t05.sum”,“t10.sum”,“t15.sum”,“t20.sum”,“t25.sum”,“t30.sum”),行.names = c(NA,25L),class =“data.frame”)

I'm using the dput output you posted as my datA. 我正在使用你发布的dput输出作为我的datA。

Inspecting datA, we can see that Line is a factor: 检查datA,我们可以看到Line是一个因素:

'data.frame':   25 obs. of  11 variables:
 $ rep    : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
 $ Conc   : Factor w/ 1 level "a": 1 1 1 1 1 1 1 1 1 1 ...
 $ Sex    : Factor w/ 2 levels "f","m": 1 1 1 1 1 1 1 1 1 1 ...
 $ Line   : Factor w/ 4 levels "20","23","40",..: 1 1 1 1 1 2 2 2 2 2 ...
 [TRUNCATED]

So, when you try to use "stat_smooth" it does not know what to do with levels of Line when trying to fit a smoother. 因此,当您尝试使用“stat_smooth”时,它不知道在尝试适合平滑时如何处理Line级别。

You need to convert the Line to a numeric before plotting. 在绘图之前,您需要将Line转换为数字。

You can do this by changing the dataframe: 您可以通过更改数据框来执行此操作:

datA$Line <- as.numeric(as.character(Line)) # need to convert before to char
# or the numeric values will become the internal factor numbers and not the labels

Then you plot as you did: 然后你就像你做的那样:

c <- qplot(Line, t05.sum, data= datA, facets=Sex~rep)
c + stat_smooth(method=lm, fullrange = TRUE) + geom_point()

You can also make the conversion on the qplot call, like this: 你也可以在qplot调用上进行转换,如下所示:

c <- qplot(as.numeric(as.character(Line)), t05.sum, 
        data= datA, facets=Sex~rep)

Missing data, I just created a toy data frame. 缺少数据,我刚创建了一个玩具数据框。 I think this is what you need: 我想这就是你需要的:

set.seed(1)
df <- data.frame(Line = sample(c(20, 23, 40, 73), 100, TRUE), 
                 t = runif(100, 0, 200), 
                 Sex = sample(c("F", "M"), 100, TRUE), 
                 rep = sample(c("1", "2", "3"), 100, TRUE))

then the plotting: 然后绘图:

library(ggplot2)

ggplot(df, aes(x= Line, y = t)) + 
  geom_point() + 
  facet_grid(Sex~rep) + 
  stat_smooth()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM