M. Beausoleil
M. Beausoleil

Reputation: 3555

Mix colour, fill and linetype in R ggplot legend

I have a map where I'm trying to add to a legend with 2 filled polygons and 1 polygon but with only the contour that is outlined (which would be represented only by a dotted line).

library(sf);library(ggplot)
# point
p <- rbind(c(1,2), c(2,1), c(3,2), c(3.5,3.5), c(3.4,3.6), c(3.9,1.4))
(mp <- st_multipoint(p))

p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
p2 <- rbind(c(1,1), c(1,2), c(2,2), c(1,1))
pol <-st_polygon(list(p1,p2))

p3 <- rbind(c(3,0), c(4,0), c(4,1), c(3,1), c(3,0))
p4 <- rbind(c(3.3,0.3), c(3.8,0.3), c(3.8,0.8), c(3.3,0.8), c(3.3,0.3))[5:1,]
p5 <- rbind(c(3,3), c(4,2), c(4,3), c(3,3))
(mpol1 <- st_multipolygon(list(list(p3))))
(mpol2 <- st_multipolygon(list(list(p4))))
(mpol3 <- st_multipolygon(list(list(p5))))

ggplot(mp, aes(geometry = geometry)) + 
  geom_sf(data = pol, aes(geometry = geometry), inherit.aes = FALSE) +
  # Add the points 
  geom_sf(data = mpol1, alpha = 0.5, aes(geometry = geometry, colour = "grey90", fill  = "grey90"), size = 0.05) + 
  geom_sf(data = mpol2,   alpha = 0.5, aes(geometry = geometry, colour = "grey20", fill  = "grey20"), size = 0.05) +
  geom_sf(data = mpol3, alpha = 0.5, aes(geometry = geometry,colour = "grey30", fill=NA), size = 0.8, linetype = "dotted") +
  scale_color_manual(values = c(alpha("grey90",.5),alpha("grey20",.5),alpha("grey30",.5)), labels = c("item1", "item2","item3"), name="My Leg. hurts") +
  scale_fill_manual( values = c(alpha("grey90",.5),alpha("grey20",.5),NA), labels = c("item1", "item2","item3"), name="My Leg. hurts") + 
  theme_bw()+
  theme(legend.key = element_rect(fill = NA), 
        legend.background = element_rect(fill = NA),
        legend.text = element_text(size = 12), 
        legend.title = element_text(size = 12,face='bold'))

Gives

enter image description here

But I want this:

enter image description here

How can this be achieved?

Upvotes: 4

Views: 932

Answers (2)

Ian Campbell
Ian Campbell

Reputation: 24790

It's not exactly what you've requested, but it's very close. You can use the override.aes argument of guides:

ggplot(mp, aes(geometry = geometry)) + 
  geom_sf(data = pol, aes(geometry = geometry), inherit.aes = FALSE) +
  # Add the points 
  geom_sf(data = mpol1, alpha = 0.5, aes(geometry = geometry, colour = "grey90", fill  = "grey90"), size = 0.05) + 
  geom_sf(data = mpol2,   alpha = 0.5, aes(geometry = geometry, colour = "grey20", fill  = "grey20"), size = 0.05) +
  geom_sf(data = mpol3, alpha = 0.5, aes(geometry = geometry,colour = "grey30", fill=NA), size = 0.8, linetype = "dotted") +
  scale_color_manual(values = c(alpha("grey90",.5),alpha("grey20",.5),alpha("grey30",.5)), labels = c("item1", "item2","item3")) +
  scale_fill_manual( values = c(alpha("grey90",.5),alpha("grey20",.5),NA), labels = c("item1", "item2","item3"), guide = FALSE) + 
  theme_bw()+ 
  theme(legend.key = element_rect(fill = NA), 
        legend.background = element_rect(fill = NA),
        legend.text = element_text(size = 12), 
        legend.title = element_text(size = 12,face='bold')) +
  guides(color = guide_legend(override.aes = 
                                list(color=c(NA,NA,"grey20"), 
                                     fill=c("grey90","grey20","white"),
                                     linetype = c("dotted")))) +
  labs(color = "My Leg. hurts")

enter image description here

Unfortunately, I don't see a good way to change the alpha of both the fill and the color in the legend.

Upvotes: 1

Jindra Lacko
Jindra Lacko

Reputation: 8699

To elaborate a bit on the earlier answer by Ian Campbell:

Consider this code (my only addition is show.legend = 'line' in the last geom_sf() call)

ggplot(mp, aes(geometry = geometry)) + 
  geom_sf(data = pol, aes(geometry = geometry), inherit.aes = FALSE) +
  # Add the points 
  geom_sf(data = mpol1, alpha = 0.5, aes(geometry = geometry, colour = "grey90", fill  = "grey90"), size = 0.05) + 
  geom_sf(data = mpol2,   alpha = 0.5, aes(geometry = geometry, colour = "grey20", fill  = "grey20"), size = 0.05) +
  geom_sf(data = mpol3, alpha = 0.5, aes(geometry = geometry,colour = "grey30", fill=NA), size = 0.8, linetype = "dotted", show.legend = 'line') +
  scale_color_manual(values = c(alpha("grey90",.5),alpha("grey20",.5),alpha("grey30",.5)), labels = c("item1", "item2","item3")) +
  scale_fill_manual( values = c(alpha("grey90",.5),alpha("grey20",.5),NA), labels = c("item1", "item2","item3"), guide = FALSE) + 
  theme_bw()+ 
  theme(legend.key = element_rect(fill = NA), 
        legend.background = element_rect(fill = NA),
        legend.text = element_text(size = 12), 
        legend.title = element_text(size = 12,face='bold')) +
  guides(color = guide_legend(override.aes = 
                                list(color=c(NA,NA,"grey20"), 
                                     fill=c("grey90","grey20","white"),
                                     linetype = c("dotted")))) +
  labs(color = "My Leg. hurts")

my leg hurts / a chart

Upvotes: 2

Related Questions