Regression Line Problems

I'm trying to make a scatter plot with two variables displayed and each have a regression line. I can get the scatter plot but when I try to add the regression line I get nothing but errors. I've tried multiple methods to try and get this to work but no luck. For this specific code I'm getting "Error: stat_smooth requires the following missing aesthetics: y". I've tried adding a formula but that doesn't work either. Any help would be appreciated!

TR <- ggplot(data = Throughfall, aes(x = Rainfall)) +
geom_point(aes(y = TF_Spruce, colour = "Spruce")) +
geom_point(aes(y = TF_Aspen, colour ="Aspen")) +
geom_smooth(method=lm) +
labs(title = "Throughfall vs Precipitation from two Yukon Forests", x = "Rainfall (mm)", y = "Throughfall (mm)", colour = "Tree Species") +
theme(axis.title=element_text(size=10)) +
theme(axis.text=element_text(size=12)) +
theme(plot.title=element_text(size=10, face = 'bold')) +
theme(
legend.position = c(0.15, 0.85),
legend.margin = margin(1, 3, 3, 3)
)

Are you truing to do something like this?

library(ggplot2)

ggplot(iris, aes(x = Sepal.Length)) + 
  geom_point(aes(y = Sepal.Width), col = "blue") +
  geom_point(aes(y = Petal.Length), col = "red") +
  geom_smooth(aes(y = Sepal.Width),  method = "lm", col = "blue") +
  geom_smooth(aes(y = Petal.Length), method = "lm", col = "red")

I've managed to get it to work by adding the second regression line in after the original code. It wouldn't let me do one after the other for some reason.

TR <- ggplot(data = Throughfall, aes(x = Rainfall)) +
geom_point(aes(y = TF_Spruce, colour = "Spruce")) +
geom_point(aes(y = TF_Aspen, colour ="Aspen")) +
geom_smooth(stat = 'smooth',
method = lm, formula = y~x, aes(Rainfall, TF_Spruce), colour="turquoise3") +
labs(title = "Throughfall vs Precipitation from two Yukon Forests", x = "Rainfall (mm)", y = "Throughfall (mm)", colour = "Tree Species") +
theme(axis.title=element_text(size=10)) +
theme(axis.text=element_text(size=12)) +
theme(plot.title=element_text(size=10, hjust= 0.5, face = 'bold')) +
theme(
legend.position = c(0.15, 0.85),
legend.margin = margin(1, 3, 3, 3)
)

add second regression line

TR + geom_smooth(stat = 'smooth',
method = lm, formula = y~x, aes(Rainfall, TF_Aspen), colour="indianred1")

I'm also having an issue with this code but this forum won't let me post two new topics in one day so I'll add it here for anyone that has any ideas!
I managed to get this code to work yesterday and successfully built a bar graph displaying two variables. Today when I go to rerun the code in order to make adjustments I'm getting code errors with my data. I assume it has something to do with my month data but I don't know what to do any help would be apprecaited!

#Plot 2 code
library(reshape)
Month <- factor(levels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
P1941TO1970 <- c(67.8, 53.1, 56.1, 81.5, 64.2, 56.9, 71.8, 71.1, 73.4, 67.5, 72.4, 60)
P1970TO2010 <- c(64, 57.8, 68.4, 79.1, 79.4, 84.9, 100.7, 79.2, 81.9, 77.4, 84.3, 73)
df <- data.frame(Month, P1941TO1970, P1970TO2010)

here is where I get the error "Error in data.frame(Month, P1941TO1970, P1970TO2010) : arguments imply differing number of rows: 0, 12" but this data all variables have the same number of rows, 12.

require(tidyr)
df.long <- gather(df, variable,value, -Month)

ggplot(data = df.long, aes(x = Month, y = value, fill = variable)) +
geom_col(position = position_dodge()) +
labs(title = "Hamilton Precipitation from 1941-2010", x = "Month", y = "Precipitation (mm)") +
theme(axis.title=element_text(size=10)) +
theme(axis.text=element_text(size=12)) +
theme(plot.title=element_text(size=12, hjust=0.5, face = 'bold')) +
theme(
legend.position = c(0.13, 0.9),
legend.margin = margin(2, 2, 2, 2),
legend.text = element_text(size = 8),
legend.title = element_blank()
)

If you look at length(Month) in your original code, you can see that it is empty.

Try this:

library(reshape)
library(tidyverse)
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

Month <- months
P1941TO1970 <- c(67.8, 53.1, 56.1, 81.5, 64.2, 56.9, 71.8, 71.1, 73.4, 67.5, 72.4, 60)
P1970TO2010 <- c(64, 57.8, 68.4, 79.1, 79.4, 84.9, 100.7, 79.2, 81.9, 77.4, 84.3, 73)
df <- data.frame(Month, P1941TO1970, P1970TO2010) %>% 
  mutate(Months = factor(months, level = months)) # factor here

Great, thanks! I've got the code running again using your method :smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.