I am creating a scatterplot using ggplot2 in R in which the size and color of the points on the plot correspond to the mean of a 3rd variable. I have figured out how to get the plot to look the way I want, but I can't figure out how to change the name of the variable that appears in the legend (to something more descriptive, and with spaces). I've found similar posts for changing facet labels: and for changing the title of a traditional legend in the ggplot2 documentation using scale_color_discrete, but these options don't seem to work since I am defining the third variable that this legend represents in a different way. Here is an example:
Generate Data:
x<-rnorm(150)
y<-rnorm(150)
d<-cbind.data.frame(x, y)
d$OUT_VAR<-2 + 1.2*d$x + 1.2*d$y
Create Plot:
plot1<-ggplot(d, aes(x, y)) +
geom_point(aes(size = OUT_VAR, alpha = OUT_VAR)) +
scale_size_continuous(breaks=c(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6)) +
scale_alpha_continuous(breaks=c(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6)) +
xlim(-4, 4) +
ylim(-4, 4) +
geom_segment(x = -4, xend = 4, y = 0, yend = 0) +
geom_segment(x = 0, xend = 0, y = -4, yend = 4) +
theme_classic() +
ggtitle("Plotting the value of the outcome
on the X, Y Plane") +
xlab("X Variable") +
ylab("Y Variable") +
theme(title = element_text(size = 14)) +
theme(legend.title = element_text(size = 9)) +
theme(axis.title.x = element_text(size = 12)) +
theme(axis.title.y = element_text(size = 12))
plot1
This creates the following plot:
Instead of 'OUT_VAR', I would like to rename the legend title...maybe something like 'Outcome Variable'. If a single word would work, I realize I could just rename the vector itself, but this is for a professional publication, so I want to be able to add something more descriptive. Any suggestions are much appreciated!