微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

需要显示国旗

如何解决需要显示国旗

我有一个数据集,其中的能源生产以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)来检测国家/地区的名称。 要拥有一个唯一的图例,您需要在colourcountry的{​​{1}}中设置相同的名称。

我注意到您为labs设置了一个设置,但是要使其正常工作,您需要在theme之后进行设置,否则它将被覆盖。

要仅在末尾设置标志,您需要将theme_gray的{​​{1}}中的xy设置为仅等于最后一点。这就是aes管道的开头。

使用完整的数据集,可视化效果会更好。

geom_flag

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?