在ggplot map R Studio中更改图例中的文本

Changing the text in legend in ggplot map R Studio

我正在尝试创建一张美国地图,其中包含流感疾病的热度级别。我有 2 个问题:

  1. 我无法更改图例中的变量文本。
  2. 图例顺序错误。 1->10->2->...

这是代码。

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")

我确实使用了scale_fill_map(labels=c(...)),但是没有用。

# 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)

生成的输出:

编辑

q4_heatmap 文件 link -> q4_heatmap.csv

问题是 stateHeat 被读取为字符而不是数字,离散因子类型将其排序为 1、10、2、3 ...

因此,我们应该用 fct_reorder 重新排序因子,并告诉它我们想要 1 到 10 的正确数字顺序。

library(maps)
#> Warning: package 'maps' was built under R version 3.5.2
library(ggplot2)

# Get all states data
all_states <- map_data("state")

# Get usheat data
q4_heatMap <- read.csv("https://download2261.mediafire.com/52r319zccrkg/jkz9ak66bj4sl24/q4_heatmap.csv")

# 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)

# make sure stateHeat is in the correct data type (factor) and the levels are in the right order
library(forcats)
usHeat$stateHeat <- fct_relevel(stateHeat, as.character(1:10))
# check data type and factor level order 
levels(usHeat$stateHeat)
#>  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"

# 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")

# 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"))

# labels and custom colors
usHeatMap + scale_fill_manual(labels=c("Extreme High","Middle High","Low High",
                                        "Moderate","Low Moderate","Higher Low","Low",
                                        "Minimal","Very Minimal","Extreme Minimal")
                      ,values = heatColor)

reprex package (v0.2.1)

于 2019-02-28 创建