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

问题描述 投票:1回答:1

我正在尝试制作一张充满热量流感疾病的美国地图。我有两个问题:

  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)

生成的输出:

wrongLegend.png

编辑

q4_heatmap文件链接 - > q4_heatmap.csv

r ggplot2 maps legend-properties
1个回答
1
投票

问题是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创建于2019-02-28(v0.2.1)

© www.soinside.com 2019 - 2024. All rights reserved.