I have plotted concentrations of phosphate at their respective sampling sites. However, I have three different depths at which phosphate was measured. Can I make the geom points a particular size to represent the depth?
This is the code that I used to make the uploaded map.
world_coordinates <- map_data("world")
ggplot() + geom_map(
data = world_coordinates, map = world_coordinates,
aes(long, lat, map_id = region))
ggplot() + geom_map(
data = world_coordinates, map = world_coordinates,
aes(long, lat, map_id = region), color= "white", fill = "darkseagreen", size = 0.2) +
geom_point(data=Map_file, aes(x=longitude, y=latitude, color=PO4), size=4, alpha=0.5) + scale_color_gradient2(low="white", mid="deeppink", high="darkorchid4", midpoint=1.5, breaks=seq(0, 3, by=0.3)) +
theme_minimal() +
theme(plot.background = element_rect(fill = 'lightsteelblue', colour = 'white')) +
theme(panel.grid.major = element_line(size = 0.2, linetype = 'solid',
colour = "white")) +
labs(title = "DCM ocean layer (17-188 m) Phosphate Concentrations") +
theme(axis.title = element_text(face="bold", size = 16)) +
theme(axis.text = element_text(face = "bold", size=16, colour = "black")) +
geom_point(shape=21, aes(fill=Map_file$"Depth (m)", size="Map_file"$"PO4"))
You need to wrap size
inside aes()
, then you can use options from the scale_size()
family. I have included two size plotting options, and have manually set the breaks. It can take trial and error to get the point sizes the way you want them (see the comparison of the first two plots to see what I mean). The final plot suggests that shape instead of size might be an appropriate option too.
For the sample PO4 data, I arbitrarily used 100m as the third depth. In the plot code below, there is a commented out option that shows how to use scale_size_area()
for continuous values. These values will also work for scale_size()
for your three depth values.
I made some adjustments to your plot code, such as restricting the background colour to just the panel area. It makes sense to do so IMHO. Another issue is that you only assigned colour in your aes()
even though you want shape = 21
. I have added the fill
aesthetic and updated the plot code accordingly. Also, consider reducing the size of your axis text as it currently dominates your plot.
library(ggplot2)
# Map data
world_coordinates <- map_data("world")
# Example PO4 data
set.seed(42)
Map_file <- data.frame(id = 1:100,
depth = sample(c(17, 100, 188), 100, replace = TRUE),
PO4 = runif(100, 0, 3),
longitude = runif(100, -180, 180),
latitude = runif(100, -90, 90))
# Plot
ggplot() +
geom_map(data = world_coordinates,
map = world_coordinates,
aes(map_id = region),
colour = "white",
fill = "darkseagreen",
linewidth = 0.2) +
geom_point(data = Map_file,
aes(x = longitude, y = latitude, fill = PO4, colour = PO4, size = depth),
alpha = 0.5,
shape = 21) +
scale_fill_gradient2(name = "PO4",
low = "white",
mid = "deeppink",
high = "darkorchid4",
midpoint = 1.5,
breaks = seq(0, 3, by = 0.3)) +
scale_colour_gradient2(name = "PO4", # Match names for single PO4 legend
low = "white",
mid = "deeppink",
high = "darkorchid4",
midpoint = 1.5,
breaks = seq(0, 3, by = 0.3)) +
scale_size(name = "Depth (m)",
range = c(3, 6),
breaks = c(17, 100, 188)) +
# scale_size_area(name = "Depth (m)",
# breaks = c(17, 80, 130, 188)) +
labs(title = "DCM ocean layer (17-188 m) Phosphate Concentrations") +
guides(size = guide_legend(override.aes = list(fill = "grey50", colour = "grey25"))) +
expand_limits(x = world_coordinates$long, y = world_coordinates$lat) +
coord_sf(expand = FALSE) +
theme_minimal() +
theme(panel.background = element_rect(fill = "lightsteelblue",
colour = "white"),
panel.grid.major = element_line(size = 0.2, linetype = "solid",
colour = "white"),
axis.title = element_text(face = "bold", size = 16),
axis.text = element_text(face = "bold", size = 16, colour = "black"))
Note that in the above plot, it is difficult to differentiate between the points representing 100m and 188m. That’s because scale_size*()
treats the values as continuous. Given you stated that you only have three sample depths, technically you have discrete values. Therefore, it may be more appropriate to use scale_size_manual()
. To do so, if your depth values are numeric, you will need to convert them to factors e.g. size = factor(depth)
:
ggplot() +
geom_map(data = world_coordinates,
map = world_coordinates,
aes(map_id = region),
colour = "white",
fill = "darkseagreen",
linewidth = 0.2) +
geom_point(data = Map_file,
aes(x = longitude, y = latitude, fill = PO4, colour = PO4, size = factor(depth)),
alpha = 0.5,
shape = 21) +
scale_fill_gradient2(name = "PO4",
low = "white",
mid = "deeppink",
high = "darkorchid4",
midpoint = 1.5,
breaks = seq(0, 3, by = 0.3)) +
scale_colour_gradient2(name = "PO4", # Match names for single PO4 legend
low = "white",
mid = "deeppink",
high = "darkorchid4",
midpoint = 1.5,
breaks = seq(0, 3, by = 0.3)) +
scale_size_manual(name = "Depth (m)",
values = c("17" = 3, "100" = 4.5, "188" = 6)) +
labs(title = "DCM ocean layer (17-188 m) Phosphate Concentrations") +
guides(size = guide_legend(override.aes = list(fill = "grey50", colour = "grey25"))) +
expand_limits(x = world_coordinates$long, y = world_coordinates$lat) +
coord_sf(expand = FALSE) +
theme_minimal() +
theme(panel.background = element_rect(fill = "lightsteelblue",
colour = "white"),
panel.grid.major = element_line(size = 0.2, linetype = "solid",
colour = "white"),
axis.title = element_text(face = "bold", size = 16),
axis.text = element_text(face = "bold", size = 16, colour = "black"))
Given that your sample depth values are arguably discrete, there’s also a case for using shapes instead of size. Doing so makes your plot more explicitly communicate that there are only three sample depths even though the values represented are from a continuous metric (metres):
ggplot() +
geom_map(data = world_coordinates,
map = world_coordinates,
aes(map_id = region),
colour = "white",
fill = "darkseagreen",
linewidth = 0.2) +
geom_point(data = Map_file,
aes(x = longitude, y = latitude, fill = PO4, colour = PO4, shape = factor(depth)),
alpha = 0.5,
size = 3) +
scale_fill_gradient2(name = "PO4",
low = "white",
mid = "deeppink",
high = "darkorchid4",
midpoint = 1.5,
breaks = seq(0, 3, by = 0.3)) +
scale_colour_gradient2(name = "PO4", # Match names for single PO4 legend
low = "white",
mid = "deeppink",
high = "darkorchid4",
midpoint = 1.5,
breaks = seq(0, 3, by = 0.3)) +
scale_shape_manual(name = "Depth (m)",
values = c(21, 22, 23)) +
labs(title = "DCM ocean layer (17-188 m) Phosphate Concentrations") +
guides(shape = guide_legend(override.aes = list(fill = "grey50", colour = "grey25"))) +
expand_limits(x = world_coordinates$long, y = world_coordinates$lat) +
coord_sf(expand = FALSE) +
theme_minimal() +
theme(panel.background = element_rect(fill = "lightsteelblue",
colour = "white"),
panel.grid.major = element_line(size = 0.2, linetype = "solid",
colour = "white"),
axis.title = element_text(face = "bold", size = 16),
axis.text = element_text(face = "bold", size = 16, colour = "black"))
1