2
votes

I am trying to create a legend denoting how the plot is color coded. However, this legend is not showing.

Here is what the output currently isenter image description here:

Does this have something to do with the current plot already taking up the entire space?

Here is my code below to create the plot (and attempt to add a legend):

cols <- c("50 miles"="#B10026","100 miles"="#FC4E2A","150 miles"="#FEB24C", "200 miles"="#FFEDA0")
hurricane_plot <- ggplot(data = US_subset_counties) +
  geom_polygon(mapping = aes(x = long, y = lat, group = group), size = 0.2, color="gray", fill=NA) +
  geom_polygon(data=counties_200, aes(x = long, y = lat, group = group), size = 0.2, fill="#FFEDA0") +
  geom_polygon(data=counties_150, aes(x = long, y = lat, group = group), size = 0.2, fill="#FEB24C") +
  geom_polygon(data=counties_100, aes(x = long, y = lat, group = group), size = 0.2, fill="#FC4E2A") +
  geom_polygon(data=counties_50, aes(x = long, y = lat, group = group), size = 0.2, fill="#B10026") +
  geom_polygon(data=US_subset_states, aes(x = long, y = lat, group = group), size = 0.2, color = "black", fill=NA) +
  theme_bw() + 
  theme(panel.background = element_blank()) +
  scale_colour_manual(name="Distance From Radius",values=cols)
hurricane_plot

Thanks in advance!

EDIT:

After @Ben's comment, I tried putting fill inside the aes to no avail. However, when I changed fill to col and used guides, this is the result I get:

enter image description here

How would I change the colors of the boxes and labels to the cols vector above, and is there a way to remove the border colors on my plot?

This is my updated code:

hurricane_plot <- ggplot(data = US_subset_counties) +
  geom_polygon(mapping = aes(x = long, y = lat, group = group), size = 0.2, color="gray", fill=NA) +
  geom_polygon(data=counties_200, aes(x = long, y = lat, group = group, color="#FFEDA0"), size = 0.2, fill="#FFEDA0") +
  geom_polygon(data=counties_150, aes(x = long, y = lat, group = group, color="#FEB24C"), size = 0.2, fill="#FEB24C") +
  geom_polygon(data=counties_100, aes(x = long, y = lat, group = group, color="#FC4E2A"), size = 0.2, fill="#FC4E2A") +
  geom_polygon(data=counties_50, aes(x = long, y = lat, group = group, color="#B10026"), size = 0.2, fill="#B10026") +
  geom_polygon(data=US_subset_states, aes(x = long, y = lat, group = group), size = 0.2, color = "black", fill=NA) +
  theme_bw() + theme(panel.background = element_blank()) +
  guides(color=guide_legend("Distance From Radius"))
1
perhaps try moving fill = in each geom_polygon into the aes and using scale_fill_manual?Ben
When I do that, nothing is colored on my plot for some reason; this however does display a legend on the side.Alan
if you could, please provide sample data for US_subset_counties, counties_200, counties_150, etc. and will try to provide an answer. Hope this will help.Ben

1 Answers

2
votes

@Alan -

Here's a reproducible example of what you might need after subsetting US state data. It uses cols as you have done, fill in aes, and scale_fill_manual. Let me know if this is what you have in mind, hope this helps.

library(maps)

us_states <- map_data("state")

states1 <- subset(us_states, region %in% c("california", "oregon", "washington"))
states2 <- subset(us_states, region %in% c("illinois", "indiana", "iowa"))
states3 <- subset(us_states, region %in% c("new york", "massachusetts", "connecticut"))

cols <- c("West"="#B10026","Midwest"="#FC4E2A","Northeast"="#FEB24C")

state_plot <- ggplot(data = us_states) +
  geom_polygon(data=us_states, aes(x = long, y = lat, group = group), size = 0.2) +
  geom_polygon(data=states1, aes(x = long, y = lat, group = group, fill="West"), size = 0.2) +
  geom_polygon(data=states2, aes(x = long, y = lat, group = group, fill="Midwest"), size = 0.2) +
  geom_polygon(data=states3, aes(x = long, y = lat, group = group, fill="Northeast"), size = 0.2) +
  theme_bw() + 
  theme(panel.background = element_blank()) +
  scale_fill_manual(name="Area of Country", values=cols)

state_plot

US map with fill and legend