Reputation: 399
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 is:
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:
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"))
Upvotes: 2
Views: 91
Reputation: 30474
@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)
library(ggplot2)
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
Here's a related approach but simplified a bit:
library(maps)
library(ggplot2)
library(tidyverse)
us_states <- map_data("state")
cols <- c("West"="#B10026","Midwest"="#FC4E2A","Northeast"="#FEB24C")
us_states <- us_states %>%
mutate(region_group = case_when(
region %in% c("california", "oregon", "washington") ~ "West",
region %in% c("illinois", "indiana", "iowa") ~ "Midwest",
region %in% c("new york", "massachusetts", "connecticut") ~ "Northeast",
TRUE ~ NA_character_
))
state_plot <- ggplot(data = us_states, aes(x = long, y = lat, group = group), size = 0.2) +
geom_polygon(aes(fill = region_group)) +
theme_bw() +
theme(panel.background = element_blank()) +
scale_fill_manual(name="Area of Country", values = cols)
state_plot
Upvotes: 3