R 需要出示国旗吗
我有一个数据集,其中能源生产用两个方面表示。我想在每条geom线的末尾和国家名称旁边的图例上显示相关国家的国旗 有什么办法可以解决这个问题吗R 需要出示国旗吗,r,ggplot2,R,Ggplot2,我有一个数据集,其中能源生产用两个方面表示。我想在每条geom线的末尾和国家名称旁边的图例上显示相关国家的国旗 有什么办法可以解决这个问题吗 library(tidyverse) #install.packages("tidyverse) library(tidymodels) #install.packages("tidymodels") library(skimr) #install.packages("skimr") library(gg
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()
像这样的
您需要使用tolower(country)
来检测国家的名称。
要拥有一个唯一的图例,您需要在color
和country
中设置相同的名称
我注意到您为theme
设置了一个设置,但要使它工作,您需要在theme\u gray
之后设置它,否则它会被覆盖
要在末尾设置标志,需要将geom_标志
的aes
中的x
和y
设置为仅等于最后一点。这就是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()
像这样的事
您需要使用tolower(country)
来检测国家的名称。
要拥有一个唯一的图例,您需要在color
和country
中设置相同的名称
我注意到您为theme
设置了一个设置,但要使它工作,您需要在theme\u gray
之后设置它,否则它会被覆盖
要在末尾设置标志,需要将geom_标志
的aes
中的x
和y
设置为仅等于最后一点。这就是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()
你好!请分享一个可复制的示例,我们需要能够运行您的代码。您需要提供一些数据。最简单的方法是共享dput(categ\u top10EnergyModf)的输出,或者至少是
dput(head(categ\u top10EnergyModf))
添加您正在使用的库。@Edo:与库一起更新了这个问题!请分享一个可复制的示例,我们需要能够运行您的代码。您需要提供一些数据。最简单的方法是共享dput(categ\u top10EnergyModf)的输出,或者至少是dput(head(categ\u top10EnergyModf))
添加你正在使用的库。@Edo:与库一起更新了这个问题,谢谢你的回答。我试过了,但是我犯了这个错误。(函数(类、fdef、mtable)中出错:找不到签名为“NULL”的函数“grobify”的继承方法使用国家/地区而不是国家/地区名称我使用了国家/地区,但它不使用scale\u country()。添加scale\u country()时,上述问题引发,因为英国国旗丢失。因此,英国国旗不会出现在传奇中,因为UK
需要gb
才能被geom_国旗
识别。请在管道的开头添加此项:mutate(国家=如果其他(国家=“英国”、“英国”、“国家”)
查看rensa的github页面,了解所有被接受的名称:Edo,谢谢你的回答。我尝试过,但发现了此错误。(函数(类,fdef,mtable)中的错误:找不到签名为“NULL”的函数“grobify”的继承方法'使用国家/地区而不是国家/地区名称我使用了国家/地区,但它在没有缩放国家/地区()的情况下工作。添加缩放国家/地区()时,上述问题引发,因为英国国旗丢失。因此,英国国旗不会出现在传奇中,因为UK
需要gb
才能被geom_国旗
识别。请在管道的开头添加此项:mutate(国家=如果其他(国家=“英国”、“英国”、“国家”)
查看rensa的github页面,了解所有公认的名称: