I am trying to create a US map filled with heat level of flu diseases. I couldn't get each state filled in with color even if I changed "color" to "fill".(It would fill in actually, but I can't find a solution to change the legend text, so I decided to go for "color" since it would allow manual legend text). Below is the code:
library(ggplot2)
library(maps)
# Get all states data
all_states <- map_data("state")
# Clean the data
subHeat <- subset(q4_heatMap, WEEK=="4")
region <- tolower(subHeat$STATENAME)
stateHeat <- subHeat$ACTIVITY.LEVEL
stateHeat <- gsub('Level ', '', stateHeat)
usHeat <- data.frame(region,stateHeat)
# Merge two set of dataframes
heatTotal <- merge(all_states, usHeat,by="region")
# heatColor
heatColor <- c("peru", "hotpink", "orchid",
"mediumpurple", "deepskyblue", "cyan3","mediumseagreen",
"limegreen","darkkhaki","salmon")
# Generate plot
usHeatMap <- ggplot(data = heatTotal) +
geom_polygon(aes(x = long, y = lat, color = heatTotal$stateHeat, group = group)) +
coord_fixed(1.3) +
labs(title = "2018-19 Influenza Season Week 4",
x = "Longitude", y="Latitude", color="Heat level") +
scale_color_manual(labels=c("Extreme High","Middle High","Low High",
"Moderate","Low Moderate","Higher Low","Low",
"Minimal","Very Minimal","Extreme Minimal")
,values = heatColor)
Generated output:
Edit
Turned out it was a mismatch of color and fill, after implementing Jack Brookes's correction, the colors worked properly. But now I couldn't change the text in the legend...
As follow: