Allyssa Hinkle
Allyssa Hinkle

Reputation: 3

geom_smooth not working for trendline, too few points?

I am trying to get a trendline for my two sets of averages, in my main graph I will be putting error bars on the points to show the sd's but below is a simplified version:

ggplot(sl, aes(x=Stresslevel, y=Final, color=Treatment)) + 
  geom_point() +    
  geom_smooth(method = "lm")

In my output I can see in the legend that it is trying to add it, but it is not showing on the graph: enter image description here

Here is an image of the data: enter image description here

Edit: Here is my data, thank you for the advice for getting it>

dput(sl)
structure(list(Stresslevel = structure(c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 3L, 4L, 5L), .Label = c("0", "1", "2 (30%)", "3 (50%)", 
"4 (70%)", "5", "Recovered"), class = "factor"), WL = c(0, 15.5, 
32.8, 52.9, 69.8, 89.2, 13.5, 30, 50, 70), WLsd = c(5, 6.5, 8.1, 
8.8, 10.6, 4.2, 9.8, 5, 5, 5), Final = c(0.0292, 0.0276, 0.0263, 
0.0248, 0.0208, 0.0199, 0.0249, 0.0274, 0.0235, 0.0121), Treatment = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Stressed", "Treated"
), class = "factor"), Finalsd = c(0.0039, 0.0019, 0.0026, 0.0033, 
0.002, 0.0021, 0.0028, 0.0049, 0.0048, 0.0026), Dry = c(0.006, 
0.008, 0.0107, 0.0139, 0.0138, 0.0174, 0.0047, 0.008, 0.0116, 
0.0105), Drysd = c(0.0015, 0.0015, 0.0017, 0.0024, 0.0011, 0.0022, 
0.001, 0.0016, 0.0033, 0.0021), Delta = c(0.0231, 0.0196, 0.0155, 
0.0109, 0.007, 0.0025, 0.0201, 0.0194, 0.012, 0.0016), Deltasd = c(0.0034, 
0.0015, 0.0019, 0.002, 0.0024, 0.001, 0.0025, 0.0043, 0.0035, 
0.0013), WC = c(4.07, 2.54, 1.48, 0.81, 0.52, 0.15, 4.44, 2.48, 
1.11, 0.16), WCsd = c(1.22, 0.59, 0.26, 0.21, 0.2, 0.08, 1.06, 
0.56, 0.45, 0.12), CD = c(1, 1.33, 1.78, 2.31, 2.29, 2.89, 0.78, 
1.33, 1.92, 1.75), CDsd = c(0.24, 0.25, 0.28, 0.4, 0.19, 0.37, 
0.16, 0.26, 0.54, 0.35)), class = "data.frame", row.names = c(NA, 
-10L))

Any help would be greatly appreciated.

Upvotes: 0

Views: 1019

Answers (1)

StupidWolf
StupidWolf

Reputation: 46908

Your x variable is a factor, meaning it is a categorical variable, so it's not clear how to fit a regression line through that:

str(sl)
'data.frame':   10 obs. of  14 variables:
 $ Stresslevel: Factor w/ 7 levels "0","1","2 (30%)",..: 1 2 3 4 5 6 7 3 4 5
 $ WL         : num  0 15.5 32.8 52.9 69.8 89.2 13.5 30 50 70

I am not sure if it makes sense to convert your categories to numeric, that is stresslevel 0 will be 1, stresslevel 1 be 2 etc.. and force a line:

ggplot(sl, aes(x=Stresslevel, y=Final, color=Treatment)) + 
  geom_point() +    
  geom_smooth(aes(x=as.numeric(Stresslevel)),method = "lm",se=FALSE)

enter image description here

I would say it might make sense to connect the lines, if it makes sense to look at the progression of your dependent variable from 0 to 5 stress:

ggplot(sl, aes(x=Stresslevel, y=Final, color=Treatment)) + 
  geom_point() +    
  geom_line(aes(x=as.numeric(Stresslevel)),linetype="dashed")

enter image description here

Upvotes: 1

Related Questions