Using colors in aes() function in ggplot2 - r

I am new to ggplot2. I am trying to understand how to use ggplot. I am reading Wickham's book and still trying to wrap my head around how to use aes() function. In a related thread, we discussed that we should try to avoid using variables inside aes() i.e. "Don't put constants inside aes() - only put mappings to actual data columns."
My objective is to observe the behavior of ggplots when we have color inside aes() for labeling (as described in Wickham's book) and also override the color to print the color.
I started with this:
library(ggplot2)
data(mpg)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(aes(colour = "loess"), method = "loess", se = FALSE) +
geom_smooth(aes(colour = "lm"), method = "lm", se = FALSE) +
labs(colour = "Method")
This nicely plots graphs and labels them. However, I am unhappy with the colors used. So, I experimented with using overriding colors again:
windows()
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(aes(colour = "loess"), method = "loess", se = FALSE, color = "magenta") +
geom_smooth(aes(colour = "lm"), method = "lm", se = FALSE, color = "red") +
labs(colour ="Method")
I added color = "red" and we can see that labs() or aes(color()) doesn't have any effect. Why does this happen? I'm curious. I'd appreciate thoughts.

When you specify, the colour outside aes() gg_plot does not consider the color information being part of the data (and it overwrites previous information) , so there is no legend to display anymore.
If you want to specify your own colors and keep the colour information as "relevant data" and not "display information", you should add a scale_colour_manual() command to specify the legend colours and leave the colour attribute in aes:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(aes(colour = "loess"), method = "loess", se = FALSE) +
geom_smooth(aes(colour = "lm"), method = "lm", se = FALSE) +
labs(colour ="Method") + scale_colour_manual(values = c("loess" = "magenta", "lm" = "red"))

Related

R: Legend for geom_polygon() with single value

I'm using ggplot2 for map plots in R. How do I add a legend entry for a layer without a scale, just for a uniform color:
geom_polygon(data = watercourses, fill = "#0055aa", alpha = .5)
I just want to have the item title "Watercourses" and a color block representing the correct fill color. How does this work? So far, I only figured out how I can include scales to the legend.
Thank you!
EDIT: Here's an example with the NC dataset.
Map without centroids in legend
library(sf)
library(ggplot2)
demo(nc)
nc_centroids <- st_centroid(nc)
ggplot(nc) +
geom_sf(aes(fill = BIR74)) +
scale_fill_gradient(low = "white", high = "red") +
geom_sf(data = nc_centroids, color = "blue") +
coord_sf()
Wrong usage of aes() for legend
ggplot(nc) +
geom_sf(aes(fill = BIR74)) +
scale_fill_gradient(low = "white", high = "red") +
geom_sf(data = nc_centroids, aes(color = "blue")) +
coord_sf()
Trying to add the centroids to the legend (based on the answer of r2evans, https://stackoverflow.com/a/75346358/4921339)
ggplot(nc) +
geom_sf(aes(fill = BIR74)) +
scale_fill_gradient(low = "white", high = "red") +
geom_sf(data = nc_centroids, aes(color = "County centroids")) +
scale_fill_manual(name = "Centroids", values = c("County centroids" = "blue"))
coord_sf()
Throws the following messages and an error:
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.
Error: Continuous value supplied to discrete scale
In my original case I use sp package instead of sf, but the messages and error thrown in the end are the same.
I think I did not yet understand how things work here, unfortunately. Any helping hints are highly appreciated.
If you place your fill in an aes(.), it will create a legend. Since you want a specific color, I suggest also adding scale_fill_manual:
ggplot(mtcars[-(1:3),], aes(mpg, disp)) +
geom_point() +
# placeholder for your `geom_polygon`:
geom_point(data = mtcars[1:3,], aes(fill = "something"), alpha = 0.5) +
scale_fill_manual(name = "something else", values = c("something" = "#0055aa"))
Perhaps this to add your blue points:
ggplot(nc) +
geom_sf(aes(fill = BIR74)) +
scale_fill_gradient(low = "white", high = "red") +
geom_sf(data = transform(nc_centroids, col = "County centroids"), aes(colour = col)) +
coord_sf() +
scale_colour_manual(name = NULL, values = c("County centroids" = "blue"))
EDIT by #winnewoerp: Explanation of how it works, for those who have problems understanding it all (like me until now...):
Add an extra column to the data frame with a unique value to be used within the legend, this can be done e.g. using the transform() function like in the given example or like df$col <- "Column unique value" prior to ggplot().
The (sole?) advantage to using transform is that there is no need to alter the original data, which might affect other processes on it (outside of this image). One disadvantage to doing it this way is that one must hard-code the "Column unique value" in both the transform and the scale_colour_manual, below.
Use scale_colour_manual() to add the legend item (blue colour example):
scale_colour_manual(
name = "Legend title",
values = c("Column unique value" = "blue")
)

Change legend title in ggplot with both shape and color

I'm trying to change the title of my legend, but I only know how to rename it using col or shape, which gives me an extra legend - one based on the shape and one based on col.
Does anyone know how to fix it so that I get one legend with a custom made title that shows the symbol both with their shape and col?
Here's my graph:
mtcars
Library(ggplot2 )
library(ggpmisc)
formula <- y~x
ggplot(mtcars, aes(disp, drat, col=factor(cyl), shape=factor(cyl))) +
geom_point() +
geom_smooth(method = "lm",formula = formula) +
scale_color_manual(values=c("#000000", "#E69F00", "#56B4E9")) +
theme_bw() +
stat_poly_eq(
aes(label = paste(stat(adj.rr.label), stat(p.value.label), sep = "*\", \"*")),
formula = formula, parse = TRUE, size=3)
Stefan: Not sure whether I got you right. If you want one legend for both color and shape with a custom title you can do it via labs(color = "Legend title", shape = "Legend Title"), i.e. give "both" legends the same name.

geom_path is not drawing a line

I'm making depth profiles with ggplot. Some of the lines are drawn between the variable points using geom_path but some are not, even when I try adding "group=1" (which was the only solution I've found for this problem). I'm doing multiple plots for different lakes and for each lake there is one or multiple variables not getting a line by using geom_path. For the code below only the Chl.a variable is not drawing a line, all the others do. What could this depend on?
I also tried geom_line instead but this only worked for some variables since the it draws the line following the x-axis, but I want the line to go vertically following the y-axis. Can I achieve this using geom_line since geom_path doesn't seem to work for all variables?
gs <- ggplot(goodspirit, aes(y=goodspirit$Depth.m)) +
geom_point(aes(x=Temp, colour= "Temp")) +
geom_path(aes(x=Temp, color = "Temp"), size=1.5) +
geom_point(aes(x=zDOmg, color ="z(DO mg/L)")) +
geom_path(aes(x=zDOmg, color ="z(DO mg/L)"), size=1.5) +
geom_point(aes(x=Chl.a, color ="Chl.a"), na.rm = TRUE) +
geom_path(aes(x=Chl.a, color ="Chl.a"), na.rm = TRUE, size=1.5) +
geom_point(aes(x=zN2O, color ="z(N2O.nM)"), na.rm = TRUE) +
geom_line(aes(x=zN2O, color ="z(N2O.nM)"), na.rm = TRUE, size=1.5) +
geom_point(aes(x=Sal.ppt, color ="Salinity.ppt"), na.rm = TRUE) +
geom_line(aes(x=Sal.ppt, color ="Salinity.ppt"), na.rm = TRUE, size=1.5)+
geom_point(aes(x=zph, color ="z(pH)")) +
geom_path(aes(x=zph, color ="z(pH)"), size=1.5) +
scale_x_continuous(position = "top", limits=c(-3,5), expand = c(0,0))+
scale_y_reverse(expand = c(0.05,0))+
ylab("Depth (m)") + xlab("x") + ggtitle("Good spirit lake") + labs(colour
= "Parameters") +
theme(plot.title = element_text(hjust = 0.5)) + theme_light()
gs
enter image description here

Using aesthetics with multiple layers in ggplot

I am new to ggplot. I am trying to understand how to use ggplot. I am reading Wickham's book and still trying to wrap my head around how to use aes() function.
What's the difference between these two implementations of aes():
library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
theme(legend.position = "none")
and
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = class)) +
geom_smooth(method = "lm", se = FALSE) +
theme(legend.position = "none")
Both of them print significantly different graphs. Any help? I am really stuck.
In the first one you are mapping the aesthetics globally, ggplot will try to map these aesthetics to all other geom_xyz() layers.
While in the latter case, you are mapping aesethics to a specific ggplot layer (in your case geom_point())

Difference between setting aes in ggplot function or in a geom?

I wonder how it's working to set aesthetics in ggplot. How it's possible to know where to put the aes in ggplot?
Consider this code:
p <- ggplot(data = mtcars,
mapping = aes(x = wt,
y = mpg,
colour = "blue"))
# A basic scatter plot
hello = p + geom_point(size = 4) + ggtitle(label = 'Hello')
goodbye = p + geom_point(aes(colour = factor(cyl)), size = 4) + ggtitle(label = 'Goodbye')
col.points = p + geom_point(size = 4, color = "blue") + ggtitle(label = 'Col.points')
gridExtra::grid.arrange(hello, goodbye, col.points)
Here, it's possible to see that the colour "blue" is not applied to the points in the first graph (hello), and in the second (goodbye) it's getting the colour from a column. But what's the difference? The col.points example shows that the points are indeed coloured.
The difference is that when the aes are set in the original ggplot, they are inherited by any other geom's that build on top of it. If you specify the aes only in a geom, it will only be used in that geom. If you use any specific aes in geom, they override the settings in ggplot.
In your example code, in the first instance:
p + geom_point(size = 4)
The size of the points is set to 4, and the aes(wt, mp, colour = 'red') is inherited from ggplot. In the second case:
p + geom_point(aes(colour = factor(cyl))
The resulting aes is aes(wt, mpg, colour = factor(cyl) as the wt and mpg are inherited from the ggplot object, and the colour = factor(cyl) overwrites the colour = 'red'.

Resources