问题描述
我想在左侧图表的背景中添加一张真实的欧洲地图:
原始图的代码取自 here(生成 this graph at "03/08/2020 ? European Power"),并且有一个略短的版本,其中包含我建议的解决方案,但在本文结尾处无法按预期工作。简而言之,我的建议是:
img_path <- "https://upload.wikimedia.org/wikipedia/commons/thumb/6/66/Blank_map_of_Europe_cropped.svg/1000px-Blank_map_of_Europe_cropped.svg.png"
img <- readpnG(img_path)
g <- rasterGrob(img,interpolate = FALSE)
第 2 步:按照建议将图像添加到 ggplot-background here
annotation_custom(g,-Inf,Inf,Inf)
编辑: 为清楚起见,一旦地图出现,我不想进一步调整网格以使每组条形与一个国家/地区的位置完全匹配。我真的只想用右边的地图替换左边的深色背景。
完整示例代码
pacman::p_load(tidyverse,tidytuesdayR,geofacet,grid,png)
## import image
img_path <- "https://upload.wikimedia.org/wikipedia/commons/thumb/6/66/Blank_map_of_Europe_cropped.svg/1000px-Blank_map_of_Europe_cropped.svg.png"
img <- readpnG(img_path)
g <- rasterGrob(img,interpolate = FALSE)
## Load in data
tuesdata <- tidytuesdayR::tt_load('2020-08-04')
## Match countries in data w/ grid,do some pivoting
energy_raw <- tuesdata$energy_types %>%
mutate(country_name = case_when(country_name == "north Macedonia" ~ "N. Macedonia",country_name == "Bosnia & Herzegovina" ~ "Bosnia & H.",country == "UK" ~ "UK",country == "EL" ~ "Greece",TRUE ~ country_name)) %>%
filter(country_name %in% europe_countries_grid2$name) %>%
pivot_longer(cols = c(`2016`,`2017`,`2018`),names_to = "year") %>%
pivot_wider(names_from = type,values_from = value) %>%
select(-country)
## Work out percentages of the total and tidy the data
energy_plot <- left_join(energy_raw %>% filter(level == "Level 1") %>% select(-level) %>% janitor::remove_empty("cols"),energy_raw %>% filter(level == "Level 2") %>% select(-level) %>% janitor::remove_empty("cols")) %>%
janitor::clean_names() %>%
rowwise() %>%
mutate(tot = sum(conventional_thermal,nuclear,hydro,wind,solar,geothermal,other,pumped_hydro_power),across(where(is.numeric),~ 100 * . / tot)) %>%
select(-tot) %>%
ungroup() %>%
pivot_longer(names_to = "energy",values_to = "value",c(conventional_thermal,pumped_hydro_power)) %>%
rbind(expand.grid(country_name = setdiff(europe_countries_grid2$name,energy_raw$country_name),year = 2016:2018,energy = "No Data Available",value = 100)) %>%
mutate(energy = case_when(energy == "conventional_thermal" ~ "Conventional Thermal",energy == "nuclear" ~ "Nuclear",energy == "No Data Available" ~ energy,TRUE ~ "Renewable"),energy = factor(energy,levels = c("Conventional Thermal","Nuclear","Renewable","No Data Available"))) %>%
group_by(energy,year,country_name) %>%
summarise(value = sum(value)) %>% mutate(country_name = str_trunc(country_name,11))
## Plot!
ggplot(energy_plot) +
# background image
annotation_custom(g,Inf) +
# The actual proper plotting stuff here isn't actually that complicated!
aes(x = year,y = value,fill = energy) +
geom_col(color = "#30332E") +
coord_flip() +
scale_y_reverse() +
# facet_geo from the "geofacet" package - I truncate the names here just to be sure
facet_geo( ~ country_name,grid = europe_countries_grid2 %>% mutate(name = str_trunc(name,11))) +
# colours and themes
scale_fill_manual(values = c("#EF476F","#FFD166","#06D6A0","#4B5446")) +
theme_minimal() +
theme(
panel.grid = element_blank(),axis.text.x = element_blank(),plot.margin = unit(c(1,1,1.5,1.2),"cm"),legend.position = c(0.075,.125)
)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)