Format multiple geom_sf legends - r

I am dealing with multiple sf geometries in ggplot and would like to display legend in the form of a point, a line, and a square (for the polygon). However, geom_sf legend combines my geometry features (i.e. combining line and point) displayed below:
library(ggplot2)
library(sf)
poly1 <- cbind(lon = c(5, 6, 7, 5), lat = c(52, 53, 51, 52))
poly <- st_sf(st_sfc(st_polygon(list(poly1))))
line <- st_sf(st_sfc(list(st_linestring(cbind(lon = c(5.5, 4.5), lat = c(53.5, 54.5))))))
point <- st_sf(st_sfc(st_point(cbind(lon = 5.5, lat = 52.7))))
ggplot() +
geom_sf(data = poly, aes(fill = "A")) +
geom_sf(data = point, aes(colour = "B"), show.legend = "point") +
geom_sf(data = line, aes(colour = "C"), show.legend = "line") +
scale_fill_manual(values = c("A" = "yellow")) +
scale_colour_manual(values = c("B" = "pink", "C" = "purple")) +
theme_minimal()
I would like three separate legends, a single yellow square, a pink point, and a purple line on the same image illustrated below. It is only the case when I plot individual geometry but not the combination of three.
I looked up similar topics, but none of them dealt with point geometries i.e. https://github.com/tidyverse/ggplot2/issues/2460
Would anyone offer any insight on this?
GitHub issue: https://github.com/tidyverse/ggplot2/issues/2763

Inspired by the comment from #Axeman, this issue and this post, the problem is solved using the override.aes argument in guide_legend():
library(ggplot2)
library(sf)
poly1 <- cbind(lon = c(5, 6, 7, 5), lat = c(52, 53, 51, 52))
poly <- st_sf(st_sfc(st_polygon(list(poly1))))
line <- st_sf(st_sfc(list(st_linestring(cbind(lon = c(5.5, 4.5), lat = c(53.5, 54.5))))))
point <- st_sf(st_sfc(st_point(cbind(lon = 5.5, lat = 52.7))))
ggplot() +
geom_sf(data = poly, aes(fill = "A")) +
geom_sf(data = point, aes(colour = "B"), show.legend = "point") +
geom_sf(data = line, aes(colour = "C"), show.legend = "line") +
scale_fill_manual(values = c("A" = "yellow"), name = NULL,
guide = guide_legend(override.aes = list(linetype = "blank", shape = NA))) +
scale_colour_manual(values = c("B" = "pink", "C" = "purple"), name = NULL,
guide = guide_legend(override.aes = list(linetype = c("blank", "solid"),
shape = c(16, NA)))) +
theme_minimal()

I know how to seperate the legends, they only get combined now because you are mapping color twice. By mapping shape to the points and setting the color, you can get around that:
ggplot() +
geom_sf(data = poly, aes(fill = "A")) +
geom_sf(data = point, aes(colour = "B"), show.legend = "point") +
geom_sf(data = line, aes(shape = "C"), show.legend = "line", color = 'purple') +
scale_fill_manual(name = NULL, values = c("A" = "yellow")) +
scale_colour_manual(name = NULL, values = c("B" = "pink")) +
scale_shape_discrete(
name = NULL,
guide = guide_legend(override.aes = list(color = 'purple'))) +
theme_minimal()
But: the point and line are still showing up in all three legends. I don't think they should! Perhaps you can fill a github issue.

Related

Legend mixing shapes with geom_points R

My legend is not showing correctly when I am doing my graph in R using ggplot2. One column of my dataset is represented by a geom_bar and the two others are represented by geom_points (one shape each). The circle and the diamond shape are showing for both 2000 and 2008, the circle being in the diamong for both year. However, the graph works totally fine...
Here is a screenshot:
I have created a simplified version of my dataset:
order_var <- c(1, 4, 3, 5, 6, 2)
alt_name <- c('Agriculture', 'Mining', 'Food products',' Manufacture', 'Chemicals', 'Machinery')
y2000 <- c(20, 40, 50, 80, 30, 70)
y2008 <- c(40, 50, 80, 70, 30, 60)
y2018 <- c(10, 30, 80, 50, 40, 50)
datatest <- data.frame("order_var" = order_var, "alt_name" = alt_name, "y2000" = y2000, "y2008" = y2008, "y2018" = y2018)
And the code for my graph:
datatest %>% ggplot(aes(x = reorder(alt_name, order_var))) +
geom_bar(stat = "identity", aes(y = `y2018`, fill = "2018"), width = 0.7, col = "black") +
geom_point(aes(y = `y2008`, col = "2008"), shape = 23, fill = "white", size = 5) +
geom_point(aes(y = `y2000`, col = "2000"), shape = 19, fill = "black", size = 3) +
xlab("Industry") +
ylab("Percentage") +
theme(legend.position = "top") +
scale_fill_manual(name = '', values = c("2018" = "#4F81BD"), breaks = c("2018")) +
scale_colour_manual(name = '', values = c("2008" = "black", "2000" = "orange"))
If you know how to correct this problem, I would be very grateful!!
Thank you :)
That's a very tricky plot you are trying to make because you are in essence mapping the same aesthetics to different geoms.
The first thing you should do is to reshape your data to the long format. I also divided your dataset between 2018 (the bar), and 2000, 2008 (the points).
df2 <- datatest %>%
pivot_longer(cols = -c(order_var, alt_name)) %>%
mutate(bar = if_else(name == "y2018", 1, 0))
data_bar <- df2 %>% filter(bar == 1)
data_point <- df2 %>% filter(bar != 1)
I also find it useful to add a dodge to your points to avoid overlapping one inside the other as in the case of chemicals with position = position_dodge(width = 0.6).
The first solution gives what you want, but it is a bit of a hack, and I wouldn't recommend doing it as a general strategy. You basically add an aesthetics that you are not going to use to the bars (in this case, linetype), and then override it, as suggested in this answer.
ggplot(data_bar, aes(x = reorder(alt_name, order_var))) +
geom_bar(aes(y = value, linetype = name), fill = "#4F81BD", stat = 'identity', color = 'black') +
geom_point(data = data_point, position=position_dodge(width=0.6), aes(y = value, color = name, shape = name, size = name, fill = name)) +
scale_colour_manual(values = c("orange", "black"), labels = c("2000", "2008")) +
scale_fill_manual(values = c("orange", "white"), labels = c("2000", "2008")) +
scale_shape_manual(values = c(19, 23), labels = c("2000", "2008")) +
scale_size_manual(values = c(3, 5), labels = c("2000", "2008")) +
scale_linetype_manual(values = 1, guide = guide_legend(override.aes = list(fill = c("#4F81BD"))), labels = c("2018")) +
theme(legend.position = "top", legend.title = element_blank()) +
labs(x = "Industry", y = "Percentage")
Another solution, more general, is to avoid using the fill aesthetics for the geom_point and changing the shape to a solid one instead:
ggplot(data_bar, aes(x = reorder(alt_name, order_var))) +
geom_bar(aes(y = value, fill = name), stat = 'identity', color = "black") +
geom_point(data = data_point, position=position_dodge(width=0.6), aes(y = value, color = name, shape = name, size = name)) +
scale_fill_manual(values = c("#4F81BD"), labels = c("2018")) +
scale_colour_manual(values = c("orange", "white"), labels = c("2000", "2008")) +
scale_shape_manual(values = c(19, 18), labels = c("2000", "2008")) +
scale_size_manual(values = c(4, 6), labels = c("2000", "2008")) +
theme(legend.position = "top", legend.title = element_blank()) +
labs(x = "Industry", y = "Percentage")

customize legend in ggplot2 with sf objects

I am plotting (mapping) sf objects with ggplot2. My understanding is that since version 2.2.1 ggplot2 contains the geom geom_sf, for simple feature objects.
I can produce the exact map that I want by doing the following:
library(sf)
library(ggplot2)
# some points to start with
a <- st_as_sf(data.frame(lon = c(1,4,6), lat = c(0,0,-3)), coords = c('lon', 'lat'))
b <- st_as_sf(data.frame(lon = c(2.5,4), lat = c(-4.5,-5)), coords = c('lon', 'lat'))
# circles around those points
buf.a <- st_buffer(a, 1)
buf.b <- st_buffer(b, 1)
# colors to mark the points
sol.a = rgb(1,0,0)
sol.b = rgb(0,0,1)
# colors to fill the circles
fil.a = adjustcolor(sol.a, alpha.f = .25)
fil.b = adjustcolor(sol.b, alpha.f = .25)
# the plot I want
g = ggplot() +
geom_sf(data = buf.a, fill = fil.a, color = NA) +
geom_sf(data = buf.b, fill = fil.b, color = NA) +
geom_sf(data = a, color = sol.a, shape = 20, size = 3) +
geom_sf(data = b, color = sol.b, shape = 20, size = 3)
g
which produces
This is what I want except that it is missing a legend. For that, I am doing
cols.fill = c("GROUP A" = fil.a, "GROUP B" = fil.b)
cols.sol = c("GROUP A" = sol.a, "GROUP B" = sol.b)
g = ggplot() +
geom_sf(data = buf.a, color = NA, aes(fill = 'GROUP A')) +
geom_sf(data = buf.b, color = NA, aes(fill = 'GROUP B')) +
geom_sf(data = a, shape = 20, size = 3, aes(color = 'GROUP A')) +
geom_sf(data = b, shape = 20, size = 3, aes(color = 'GROUP B')) +
scale_fill_manual(name = "circles", values = cols.fill) +
scale_color_manual(name = "points", values = cols.sol)
g
which gives
That's not what I want, because in the legend:
'points' should be points (not squares); and
'circles' should be, well, circles (again, not squares)
Would be nice if the legend could respect the transparency of my colors (which it did in this example).
I tried to change the last couple of lines of the above to something like
scale_fill_manual(name = "circles", values = cols.fill,
guide = guide_legend(override.aes = list(shape = c(19, 19)))) +
scale_color_manual(name = "points", values = cols.sol,
guide = guide_legend(override.aes = list(shape = c(20, 20))))
but that didn't do anything to my plot.
Ideas?
Note: If it ends up being simpler for the plot, I could change the structure of the data, e.g., by combining objects a and b in the same simple feature object and add a column indicating the group (same for buf.a and buf.b).
Here's how far I managed to get to.
g = ggplot() +
geom_sf(data = buf.a, color = NA, aes(fill = 'GROUP A'), show.legend = "point") +
geom_sf(data = buf.b, color = NA, aes(fill = 'GROUP B'), show.legend = "point") +
geom_sf(data = a, shape = 20, size = 3, aes(color = 'GROUP A'), show.legend = "point") +
geom_sf(data = b, shape = 20, size = 3, aes(color = 'GROUP B'), show.legend = "point") +
scale_color_manual(name = "points", values = cols.sol,
guide = guide_legend(override.aes = list(shape = c(20, 20)))) +
scale_fill_manual(name = "circles", values = cols.fill,
guide = guide_legend(override.aes = list(shape = c(20, 20), color = cols.fill, size = 8)))
g
To get rid of the gray background in the legend symbols,
g + theme(legend.key = element_rect(fill = "white"))
The only issue here is that the circles do not have the transparency I wanted. This is odd.
To get the transparency in the legend need to add it to the override.aes, try:
guide = guide_legend(override.aes = list(alpha = 0.5, shape = c(20, 20), color = cols.fill, size = 8, )))

Add item to existing legend

I am new to ggplot2 and I am struggling since hours to add a second legend in my plot.
I am using two data.frames (df_1 and df_2) and two geom_point calls for them. I managed to create a legend for df_2 but I was not able to add a second legend for df_1.
Here a code example with also plot:
########## Create sample data
set.seed(69)
df_1 = data.frame(lat = rnorm(20),
lon = rnorm(20),
cor = c(rep('positive', 12), rep('negative', 8)),
sign = 0)
df_2 = data.frame(lat = rnorm(20),
lon = rnorm(20),
cor = c(rep('positive', 7), rep('negative', 13)),
sign = c(rep(99, 5), rep(95, 6), rep(90,9)))
#### Plot data
library(ggplot2)
p = ggplot() +
# geom_point for df_1
geom_point(data=df_1, aes(x=lon, y=lat),
alpha=0.7, color = 'darkgrey', size = 3) +
# geom_point for df_2
geom_point(data=df_2, aes(x=lon, y=lat, size=sign, colour = cor), alpha = 0.5) +
scale_color_manual(values=c("red", "blue"),
name='cor',
labels = c('neg', 'pos'),
guide = guide_legend(override.aes = list(alpha = 1, size = 3))) +
scale_size(range = c(1,3),
breaks = c(90, 95, 99),
labels = c(0.1, 0.05, 0.01),
name = 'sign',
guide = guide_legend(override.aes = list(colour = 'black',
alpha = 1)))
print(p)
How can I add a legend for the geom_point call of df_1?
It would be enough to add a 3rd darkgrey point to cor (right legend) with label 'not sign'.
I guess the straightforward solution is too do what you ask add 3rd darkgrey point to cor legend. To do this you have to:
Change cor values in df_1 to be all the same.
Specify color in df_1 aes.
Add information for the third point in scale_color_manual.
Code:
# Change values so we would have single color for them
df_1$cor <- "foo"
library(ggplot2)
ggplot() +
geom_point(aes(lon, lat, color = cor), df_1,
alpha = 0.7, size = 3) +
geom_point(aes(lon, lat, size = sign, colour = cor), df_2,
alpha = 0.5) +
scale_color_manual(values = c("darkgrey", "red", "blue"),
labels = c("not sign", "neg", "pos"),
guide = guide_legend(override.aes = list(alpha = 1, size = 3))) +
scale_size(range = c(1, 3),
breaks = c(90, 95, 99),
labels = c(0.1, 0.05, 0.01),
guide = guide_legend(override.aes = list(colour = "black", alpha = 1)))
Result:

Add a single point to legend from geom_point and scale_fill_gradient

Has been now a few months I am working with ggplot2 but I still get stuck very easily on basic things, as the options out here are close to infinite.
Let's assume I have a simple plot created as follows:
set.seed(100)
df_1 = data.frame(lat = rnorm(20),
lon = rnorm(20),
x = rnorm(20))
library(ggplot2)
p = ggplot() +
geom_point(data = df_1,
aes(x=lon, y=lat, fill = x),
size = 5, colour = 'black', pch = 21) +
scale_fill_gradient2(low = "green", mid = 'white', high = "yellow",
breaks = c(-1, 0, 1),
labels = c('-1', '0', '1'),
limits = c(-1,1))
print(p)
How can I add a second legend with title (e.g. y) showing only one of those circles with white background and black contour?
To add extra element to legend, you have to add it to a plot. You can do this with:
geom_point(aes(alpha = ""), head(df_1, 1),
size = 5, fill = "white", pch = 21) +
Here we are adding first point in your dataset, setting it's fill and dummy alpha value (we need to set something within aes to add it to legend). I'm using "" so we won't have any text next to a point.
Also, it's important to add this point before main geom_point because it will cover original point (with white fill). You also need to reset alpha values from "" to 1 and to set wanted legend name for alpha in labs().
library(ggplot2)
ggplot(df_1, aes(lon, lat, fill = x)) +
geom_point(aes(alpha = ""), head(df_1, 1),
size = 5, fill = "white", pch = 21) +
geom_point(size = 5, pch = 21) +
scale_fill_gradient2(low = "green", mid = "white", high = "yellow",
breaks = c(-1, 0, 1),
labels = c("-1", "0", "1"),
limits = c(-1, 1)) +
scale_alpha_manual(values = 1) +
labs(alpha = "y")
PS. I have made some changes in your ggplot2 code:
You can specify data and aes within first ggplot call.
In geom layers aes is first argument, data is second. So instead of geom_point(data = df_1, aes(...). You have use geom_point(aes(...), df_1).
color = "black" is a default setting - you don't need to specify it.
You could add a factor with one level and use scale_color_manual:
set.seed(100)
df_1 = data.frame(lat = rnorm(20),
lon = rnorm(20),
x = rnorm(20),
new = rep('Coordinates', 20))
library(ggplot2)
p = ggplot() +
geom_point(data = df_1,
aes(x=lon, y=lat, fill = x, colour = new),
size = 5, pch = 21) +
scale_fill_gradient2(low = "green", mid = 'white', high = "yellow",
breaks = c(-1, 0, 1),
labels = c('-1', '0', '1'),
limits = c(-1,1)) +
scale_color_manual(name = "", values = "black")
print(p)

geom_point with different legend for fill and shape

Hmmm, maybe it's the temprature or I'm once again do not see the obvious ...
Here is my code:
library(ggplot2)
p <- ggplot()
p <- p + geom_point(aes(x = 1, y=1,bg = "I", group = "B"),pch = 21, size = 20, color=NA)
p <- p + geom_point(aes(x = 1, y=1.125,bg = "I", group = "B" ),pch = 22, size = 20, color=NA)
p <- p + geom_point(aes(x = 0.75, y=1.125,bg = "II", group = "A" ),pch = 22, size = 20, color=NA)
p <- p + geom_point(aes(x = 0.85, y=1.125,bg = "III", group = "A" ),pch = 22, size = 20, color=NA)
p <- p + scale_fill_manual(values= c("darkred", "darkblue", "darkgreen"), guide=guide_legend(override.aes = list(shape = 23)))
#p <- p + scale_fill_manual(values= c("darkred", "darkblue", "darkgreen"), guide=guide_legend(inherit.aes = FALSE))
p <- p + scale_shape_manual(labels = c("circle", "rectangle"),values = c(21, 22))
p
What I'm trying to achieve are basically two legends, one that reflects the color, in this example there are three different colors ("I", "II", and "III"), and two different types of shapes "rectangle" and "circle", there will never be more than these two different shapes.
Unfortunately there are some additional constraints ... I can't use the aesthetic color due to the fact that I'm also using geom_segment to somehow connect the shapes, and that is the second constraint, I have to use ggplot2.
But I'not able to produce these two legends, any help is appreciated ...
Why don't you store all your points in a data frame? It suits perfectly:
df <- data.frame(x = c(1, 1, 0.75, 0.85),
y = c(1, 1.125, 1.125, 1.125),
nr = c("I", "I", "II", "III"),
sh = c("B", "B", "A", "A"))
And now you can easily see the required mapping:
ggplot(df, aes(x, y, fill = nr, shape = sh)) +
geom_point(size = 20, color = NA) +
scale_shape_manual(labels = c("circle", "rectangle"), values = c(21, 22),
guide = guide_legend(override.aes = list(colour = 1))) +
scale_fill_manual(values = c("darkred", "darkblue", "darkgreen"),
guide = guide_legend(override.aes = list(shape = 23)))

Resources