0

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:

notDesiredResult.png

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: anotherPic.png

JamCon
  • 2,313
  • 2
  • 25
  • 34
tcltan
  • 21
  • 1
  • 8
  • 6
    `aes(..., fill = stateHeat)` and `scale_fill_manual(labels=c(...))` – Jack Brookes Feb 28 '19 at 20:44
  • 1
    You mapped to color instead of fill – camille Feb 28 '19 at 20:58
  • Thank you, it worked! How silly of me... Another question, how can I change the legend text? – tcltan Feb 28 '19 at 21:23
  • Since you've already asked [another question](https://stackoverflow.com/questions/54937547/changing-the-text-in-legend-in-ggplot-map-r-studio) regarding the legend text (& got an answer), I'm voting to close this one for being a simple mis-mapping between `color` & `fill`. – Z.Lin Mar 01 '19 at 08:40

0 Answers0