我无法在地图上显示我的颜色类别

I can't get my colour category to show up on my map

提问人:Jobolo 提问时间:11/16/2023 更新时间:11/16/2023 访问量:21

问:

我需要帮助。我想实现的是创建一张地图,其中有选定的国家(20 个独特的国家),根据他们的收入水平突出显示和颜色编码。我想保留其余的国家。我试过在其他地方阅读,但我不确定如何前进。

我试过:

  • 确保将我的国家/地区更改为因子变量
  • 有些国家/地区有多个条目,所以我修改了这个条目

这是我的数据:

structure(list(region = c("Belgium", "Canada", "Cyprus", "Democratic Republic of the Congo", "Denmark", "Ghana", "Greece", "India", "Israel", "Italy", "Kenya", "Malaysia", "Nigeria", "Philippines", "Portugal", "Spain", "Sri Lanka", "Tanzania", "UK", "USA"), `Country by level of income (World Bank)` = c("High", "High", "High", "Low", "High", "Lower middle-income", "High", "Lower middle-income", "High", "High", "Lower middle-income", 
"Upper middle income", "Lower middle-income", "Lower middle-income", "High", "High", "Lower middle-income", "Lower middle-income", "High", "High"), Continent = c("Northern Europe", "North America", "Mediterranean", "Africa", "Northern Europe", "Africa", "Mediterranean", 
"South Asia", "Middle East", "Mediterranean", "Africa", "South East Asia", "Africa", "South East Asia", "Mediterranean", "Mediterranean", "South Asia", "Africa", "Northern Europe", "North America")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L))

我的代码:

library(dplyr)
library(stringr)
library(ggplot2)
library(maps)
library(ggmaps)
library(scales)
library(sf)
library(readxl)
library(extrafont)

options(scipen = 999)

world <- map_data("world")
worldplot <- ggplot() +
  geom_polygon(data = world, aes(x=long, y = lat, group = group)) + 
  coord_fixed(1.3)
worldplot

country_income_map <- left_join(world, country_income, by = "region")
View(country_income_map)
country_income_map <- mutate_at(country_income_map, vars('Country by level of income (World Bank)',
                                     'region'), as.factor)

custom_colour_scale_income <- c("High"='#404E88', "Upper middle income" = '#2A8A8C', 
                                "Lower middle-income" = '#7FD157', "Low" = '#F9E53F')

ggplot(country_income_map, aes( x = long, y = lat, group = group )) +
  geom_polygon(aes(fill = "Country by level of income (World Bank)")) +
  scale_fill_manual(values = custom_colour_scale_income) +
                      guides(fill = guide_legend(reverse = T)) +
  labs(fill = 'Level of income'
       ,title = 'Responses by country and level of income'
       ,x = NULL
       ,y = NULL) +
  theme(text = element_text(family="Gill Sans MT", color = '#EEEEEE')
        ,plot.title = element_text(size = 28)
        ,plot.subtitle = element_text(size = 14)
        ,axis.ticks = element_blank()
        ,axis.text = element_blank()
        ,panel.grid = element_blank()
        ,panel.background = element_rect(fill = '#333333')
        ,plot.background = element_rect(fill = '#333333')
        ,legend.position = c(.18,.36)
        ,legend.background = element_blank()
        ,legend.key = element_blank()
  ) 

我的当前输出:

Responses according to country's level of income

r ggplot2 映射 比例

评论


答:

1赞 Allan Cameron 11/16/2023 #1

您的问题只是您已将填充美学映射到单个字符串,而不是列名称。您需要在非语法列名称周围使用反引号而不是引号:

ggplot(country_income_map, aes( x = long, y = lat, group = group )) +
  geom_polygon(aes(fill = `Country by level of income (World Bank)`)) +
  scale_fill_manual(values = custom_colour_scale_income) +
  guides(fill = guide_legend(reverse = T)) +
  labs(fill = 'Level of income'
       ,title = 'Responses by country and level of income'
       ,x = NULL
       ,y = NULL) +
  theme(text = element_text(family="Gill Sans MT", color = '#EEEEEE')
        ,plot.title = element_text(size = 28)
        ,plot.subtitle = element_text(size = 14)
        ,axis.ticks = element_blank()
        ,axis.text = element_blank()
        ,panel.grid = element_blank()
        ,panel.background = element_rect(fill = '#333333')
        ,plot.background = element_rect(fill = '#333333')
        ,legend.position = c(.18,.36)
        ,legend.background = element_blank()
        ,legend.key = element_blank()
  ) 

enter image description here

评论

0赞 Jobolo 11/16/2023
啊,哈哈,我有一种感觉,这是一件非常简单的事情。谢谢。对不起,我不确定谁先回答!
0赞 Susan Switzer 11/16/2023 #2

一个微小的变化会让你的颜色显现出来。 由于您有一个非法的列名(带空格的名称),因此您必须在它周围添加勾号,而不是在 geom_polygon() 中添加引号

country_income <-
  structure(
    list(
      region = c(
        "Belgium",
        "Canada",
        "Cyprus",
        "Democratic Republic of the Congo",
        "Denmark",
        "Ghana",
        "Greece",
        "India",
        "Israel",
        "Italy",
        "Kenya",
        "Malaysia",
        "Nigeria",
        "Philippines",
        "Portugal",
        "Spain",
        "Sri Lanka",
        "Tanzania",
        "UK",
        "USA"
      ),
      `Country by level of income (World Bank)` = c(
        "High",
        "High",
        "High",
        "Low",
        "High",
        "Lower middle-income",
        "High",
        "Lower middle-income",
        "High",
        "High",
        "Lower middle-income",
        "Upper middle income",
        "Lower middle-income",
        "Lower middle-income",
        "High",
        "High",
        "Lower middle-income",
        "Lower middle-income",
        "High",
        "High"
      ),
      Continent = c(
        "Northern Europe",
        "North America",
        "Mediterranean",
        "Africa",
        "Northern Europe",
        "Africa",
        "Mediterranean",
        "South Asia",
        "Middle East",
        "Mediterranean",
        "Africa",
        "South East Asia",
        "Africa",
        "South East Asia",
        "Mediterranean",
        "Mediterranean",
        "South Asia",
        "Africa",
        "Northern Europe",
        "North America"
      )
    ),
    class = c("tbl_df", "tbl", "data.frame"),
    row.names = c(NA,-20L)
  )

library(dplyr)
library(stringr)
library(ggplot2)
library(maps)
library(ggmaps)
library(scales)
library(sf)
library(readxl)
library(extrafont)

options(scipen = 999)

world <- map_data("world")
worldplot <- ggplot() +
  geom_polygon(data = world, aes(x=long, y = lat, group = group)) + 
  coord_fixed(1.3)
worldplot

country_income_map <- left_join(world, country_income, by = "region")
View(country_income_map)
country_income_map <- mutate_at(country_income_map, vars('Country by level of income (World Bank)',
                                                         'region'), as.factor)

custom_colour_scale_income <- c("High"='#404E88', "Upper middle income" = '#2A8A8C', 
                                "Lower middle-income" = '#7FD157', "Low" = '#F9E53F')

ggplot(country_income_map, aes( x = long, y = lat, group = group )) +
  geom_polygon(aes(fill = `Country by level of income (World Bank)`)) +
  scale_fill_manual(values = custom_colour_scale_income) +
  guides(fill = guide_legend(reverse = T)) +
  labs(fill = 'Level of income'
       ,title = 'Responses by country and level of income'
       ,x = NULL
       ,y = NULL) +
  theme(
    # text = element_text(family="Gill Sans MT", color = '#EEEEEE')
        ,plot.title = element_text(size = 28)
        ,plot.subtitle = element_text(size = 14)
        ,axis.ticks = element_blank()
        ,axis.text = element_blank()
        ,panel.grid = element_blank()
        ,panel.background = element_rect(fill = '#333333')
        ,plot.background = element_rect(fill = '#333333')
        ,legend.position = c(.18,.36)
        ,legend.background = element_blank()
        ,legend.key = element_blank()
  ) 

map