ggplot2和R Studio中各州的地图填充颜色

时间:2019-02-28 20:37:09

标签: r ggplot2 colors maps

我正在尝试创建一张充满流感热水平的美国地图。即使将“颜色”更改为“填充”,我也无法用颜色填充每个状态。(它实际上可以填充,但是我找不到改变图例文本的解决方案,所以我决定去“颜色”,因为它将允许使用手动图例文本。下面是代码:

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)

生成的输出:

notDesiredResult.png

修改

事实证明,这是颜色和填充的不匹配,在实施Jack Brookes的校正后,颜色正常工作。但是现在我无法更改图例中的文本...

如下: anotherPic.png

0 个答案:

没有答案