R geom_smooth what to write in aes

I work with R and ggplot. I have already drawn point for 4 different data.frames. And now I want to draw 4 regression lines for this points sets.

My previous code:

ggplot() + ggtitle("title")+ xlab("date") + ylab("value") + geom_point(data=toplot$1, aes(x=date, y=x, color='1'), size = 4, shape=1) + geom_point(data=toplot$2, aes(x=date, y=x, color='2'), size = 4, shape=2)+ geom_point(data=toplot$3, aes(x=date, y=x, color='3'), size = 4, shape=3)+ geom_point(data=toplot$4, aes(x=date, y=x, color='4'), size = 4, shape=4)+ scale_colour_manual(name = "legend", values = c('1'='green', "2"="red", "3"="blue", "4"="brown"))

When I add a line for the first data.frame

geom_smooth(data=toplot$1, formula=date~x,method=lm, color='1',aes(x=date, y=x)) 

I receive a message:

Only one unique x value each group.Maybe you want aes(group = 1)

If I add line:

geom_smooth(data=toplot$1, formula=date~x,method=lm, color='1',aes(group=1)) 

I receive another message:

stat_smooth requires the following missing aesthetics: x, y

May be you know, what I need to write as aes argument (without any aes it also doesn't work).

Thank you.

7

4 Answers

The short answer is aes(x=date, y=x, color='1'), and the proper answer is that you should learn to use ggplot2. See below for an example of how to use grouping.

# prepare data
toplot2 <- do.call(rbind, toplot)
toplot2[, "group"] <- factor(rep(1:length(toplot), times=sapply(toplot, nrow)))
# ggplot command
ggplot(toplot2, aes(x=date, y=x, color=group, shape=group)) + geom_point(size=4) + geom_smooth(method=lm) + ggtitle("title")+ xlab("date") + ylab("value") + scale_colour_manual(name = "legend", values = c('1'='green', '2'='red', '3'='blue', '4'='brown')) + scale_shape_manual(name = "legend", values = c('1'=1, '2'=2, '3'=3, '4'=4))

EDIT: It seems like you have to actually add aes(x=date, y=x, group=1, color='1') to your example or aes(x=date, y=x, color=group, shape=group, group=group) in my version. See e.g. Adding a simple lm trend line to a ggplot boxplot or Joining means on a boxplot with a line (ggplot2), although they also don't explain why.

2

The short answer is probably something like:

geom_smooth(data=toplot$1, formula=y~x,method=lm, color='1',aes(x = date, y = x,group=1))

(The formula in geom_smooth is always "generic" in the sense that you reference a response and covariate with x and y, no matter what you called them in the other layers.)

However, this:

geom_point(data=toplot$1, aes(x=date, y=x, color='1'), size = 4, shape=1) +
geom_point(data=toplot$2, aes(x=date, y=x, color='2'), size = 4, shape=2)+
geom_point(data=toplot$3, aes(x=date, y=x, color='3'), size = 4, shape=3)+
geom_point(data=toplot$4, aes(x=date, y=x, color='4'), size = 4, shape=4)

is not the right way to go. Basically anytime you find yourself repeating a single geom like this over and over again is a good sign that you're not using ggplot correctly.

Instead, one would generally combine all four data frames into a single one using rbind, and then create a third variable grp with values 1-4 to label each section. Then you'd just do a single layer:

geom_point(data = full_data,aes(x = date,y = x, color = grp, shape = grp), size = 4)
2

The smoothing function, in this case "lm", uses variables defined in aes so you could use something like the following

toplot1 <- data.frame(date=seq(Sys.Date()-30,Sys.Date(),1), x= 1:31+ rnorm(31, 0, 2))
toplot2 <- data.frame(date=seq(Sys.Date()-30,Sys.Date(),1), x= 1:31+ 5 + rnorm(31, 0, 2)) sp <- ggplot() + ggtitle("title")+ xlab("date") + ylab("value") + geom_point(data=toplot1, aes(x=date, y=x, color='1'), size = 4, shape=1) + geom_point(data=toplot2, aes(x=date, y=x, color='2'), size = 4, shape=2)+ scale_colour_manual(name = "legend", values = c ('1'='green', "2"="red", "3"="blue", "4"="brown")) sp <- sp + geom_smooth(data=toplot1, aes(x=date, y=x, color="1"), formula = y~x, method="lm") + geom_smooth(data=toplot2, aes(x=date, y=x, color="1"), formula = y~x, method="lm")
plot(sp)

I don't quite understand the structure of your dataframes with the names toplot$1, etc.

my script run, only add group=1 x,y axis.

graf_disp <- ggplot(dados, aes(x=Treatments, y=Survival, group=1)) + geom_point(aes(col=Treatments)) + geom_smooth(method=lm, se=F) + labs(subtitle = "Weeds Survival Percentage", y = "Weeds Survival (%)", x = "Treatments")
plot(graf_disp)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like