I am trying to create a US map filled with heat level of flu diseases. I am having 2 issues:
- I couldn't change the variable text in the legend.
- The order of the legend is wrong. 1->10->2->...
Here is the code.
library(maps)
library(ggplot2)
# 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")
I did use scale_fill_map(labels=c(...)), but it didn't work.
# Generate plot
usHeatMap <- ggplot(data = heatTotal) +
geom_polygon(aes(x = long, y = lat, fill = stateHeat, group = group)) +
coord_fixed(1.3) +
labs(title = "2018-19 Influenza Season Week 4",
x = "Longitude", y="Latitude", color="Heat level") +
scale_fill_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
q4_heatmap file link -> q4_heatmap.csv
The problem was that stateHeat was being read as a character instead of a number, and the discrete factor type ordered it 1, 10, 2, 3 ...
Therefore, we should reorder the factor, with
fct_reorder
and tell it we want 1 to 10 in proper numeric order.Created on 2019-02-28 by the reprex package (v0.2.1)