r - 需要出示国旗

标签 r ggplot2

我有一个数据集,其中能源生产以 2 个方面表示。我想在每条几何线的末尾以及国家/地区名称旁边的图例中显示相关国家/地区的国旗。

有什么办法可以解决这个问题吗?

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", "FR", "FR", "DE"
), country_name = c("Germany", "Germany", "France", "France", 
"France", "Germany"), type2 = c("Non-Renewable", "Non-Renewable", 
"Non-Renewable", "Non-Renewable", "Non-Renewable", "Non-Renewable"
), year = c(2016L, 2017L, 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", "DE", "DE", "FR", 
"FR", "FR"), country_name = c("Germany", "Germany", "Germany", 
"France", "France", "France"), year = c(2016L, 2017L, 2018L, 
2016L, 2017L, 2018L), .rows = structure(list(1L, 2L, 6L, 5L, 
    3L, 4L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
"list"))), row.names = c(NA, 6L), class = c("tbl_df", "tbl", 
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))

最佳答案

类似这样的东西吗?

您需要使用tolower(country)来检测国家/地区的名称。 要拥有一个独特的图例,您需要在 labs 中的 colourcountry 设置相同的名称。

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

要在最后设置标志,您需要在 geom_flagaes 中设置 xy仅等于最后一点。这就是 dplyr 管道一开始的用途。

有了完整的数据集,可视化效果应该会更好。

library(ggplot2)
library(dplyr)
library(ggflags)

categ_top10EnergyModf %>%
 mutate(year = factor(year),
        country = tolower(country)) %>% 
 group_by(type2, country) %>% 
 mutate(country_x = max(levels(year)),
        country_y = ggwt_hours[country_x == year]) %>% 
 
 ggplot(aes(x = year,
            y = ggwt_hours,
            color = country,
            group = country)) +
 geom_line(size = 1.5) +
 geom_point(size = 3) +
 geom_flag(aes(x = country_x, 
               y = country_y, 
               country = country)) +
 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", 
      country = "Country", 
      color   = "Country") +
 theme_grey() +
 theme(plot.title = element_text(hjust = 0.5)) +
 scale_country()

enter image description here

关于r - 需要出示国旗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64240614/

相关文章:

r - 如何更有效地计算数据帧的多个子集的斜率?

r - 以 1 万为单位打印 ggplot y Axis 值

python - rPython 和 __future__ 导入

r - 如何绘制具有自定义分布的直方图?

r - 即使所有值都 > 0,为什么 geom_histogram 从负 bin 下限开始?

r - ggplot2scale_fill_gradient2不显示中间颜色

r - 用 R 中的 ggplot 按周绘图

r - ggplot 上的抛物线引用线

r - 如何在ggplot2中组合排斥标签和阴影或光环文本?

根据第二组列替换多列中的值