Reputation: 11
so I need to add a legend to this. I have the feeling I am doing something wrong with the aes() function, but I could be wrong. Here is my code:
library(readxl)
library(ggplot2)
library(ggpmisc)
Mydata <- read_excel('../...') #Data file
df <- subset(data.frame(x1 = Mydata$rc, y1 = Mydata$`O2 (mol/m^3)`), y1 > 0.051 & x1 > 0) #Points that do count
df2 <- subset(data.frame(x2 = Mydata$rc, y2 = Mydata$`O2 (mol/m^3)`), y2 < 0.051 & y2 > 0) #Point not taken into account
cols = c('Included in regression' = 'blue', 'Excluded from regression' = 'blue')
fills = c('Included in regression' = 'blue', 'Excluded from regression' = 'white')
ggplot()+
geom_point(data = df, aes(x1,y1), color = 'blue')+
geom_point(data = df2, aes(x2,y2), color = 'blue', pch = 1)+
geom_path(data = df, aes(x1,y1), color = 'blue')+
geom_smooth(data = df, aes(x1,y1), formula = y~x, method = 'lm', se = FALSE, color = 'red')+
stat_poly_eq(data = df, formula = y~x, rr.digits = 3, coef.digits = 5,
aes(x1,y1, label = paste("atop(", after_stat(eq.label), ",", after_stat(rr.label), ")", sep = "")),
parse = TRUE, label.x = 'right')+
xlab(expression('dC'[O2]*'/dt (mol m'^-3*' s'^-1*')'))+
ylab(expression('C'['O2']*' (mol m'^-3*')'))+
coord_cartesian(xlim = c(0, 0.00205),ylim = c(0,0.27),expand = c(0,0))+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
Upvotes: 1
Views: 21
Reputation: 339
Put your color = '<color>'
statements inside aes()
. For example,
geom_point(data = df, aes(x1,y1,color = 'blue'))
It's likely your going to want to manage the labels in the legend. In this case, you can use
geom_point(data = df, aes(x1,y1,color = 'mylabel')) +
scale_color_manual(values=c('mylabel'='blue'))
and the legend label will be "mylabel"
.
Upvotes: 1