如何解决需要显示国旗
我有一个数据集,其中的能源生产以2个方面表示。我想在每个geom行的末尾以及在国家名称旁边的图例上显示相关国家的国旗。
有什么办法可以解决此问题?
library(tidyverse) #install.packages("tidyverse)
library(tidymodels) #install.packages("tidymodels")
library(skimr) #install.packages("skimr")
library(ggplot2) #install.packages("ggplot2")
library(dplyr) ##install.packages("deplyr")
library(knitr) #install.packages("knitr")
library(tidyr) #install.packages("tidyr")
library(viridis) #install.packages("viridis")
devtools::install_github("rensa/ggflags") #install.packages("devtools")
library(ggflags)
ggplot(categ_top10EnergyModf,aes(x = factor(year),y = ggwt_hours,color = country_name,group = country_name)) +
geom_line(size = 1.5) +
geom_point(size = 3) +
geom_flag(country = country_name) +
scale_y_continuous(labels = scales::comma) +
facet_wrap(. ~ type2,scale = 'free') +
labs(x = "Year",y = "Energy Production (GWh)",title = "Analysis of the growth of Renewable/Non-Renewable Energy production",fill = "Country",color = "Country") +
theme(plot.title = element_text(hjust = 0.5)) +
theme_grey()
> dput(head(categ_top10EnergyModf))
structure(list(country = c("DE","DE","FR","DE"
),country_name = c("Germany","Germany","France","Germany"),type2 = c("Non-Renewable","Non-Renewable","Non-Renewable"
),year = c(2016L,2017L,2018L,2016L,2018L),ggwt_hours = c(471984,449906,448690.614,447109.694,445175.494,393234.585)),row.names = c(NA,-6L),groups = structure(list(country = c("DE","FR"),"France"),.rows = structure(list(1L,2L,6L,5L,3L,4L),ptype = integer(0),class = c("vctrs_list_of","vctrs_vctr","list"))),6L),class = c("tbl_df","tbl","data.frame"),.drop = TRUE),class = c("grouped_df","tbl_df","data.frame"))
解决方法
像这样吗?
您需要使用tolower(country)
来检测国家/地区的名称。
要拥有一个唯一的图例,您需要在colour
和country
的{{1}}中设置相同的名称。
我注意到您为labs
设置了一个设置,但是要使其正常工作,您需要在theme
之后进行设置,否则它将被覆盖。
要仅在末尾设置标志,您需要将theme_gray
的{{1}}中的x
和y
设置为仅等于最后一点。这就是aes
管道的开头。
使用完整的数据集,可视化效果会更好。
geom_flag
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。