在r中使用循环创建50多个国家的地块



我需要帮助使用循环在R.我有52个国家的数据集。我需要为每个国家创建6个地块。代码是所有国家的标准代码,在country_name中有所不同;start_year;end_year。数据来自两个来源- 1/raw_country(取自wdi)和raw_gvt_total_spend(取自IMF)示例代码如下:

#INDIA: PLOTS-----
#1. OOP
c1_title <- expression(paste(bold("India"), ":OOP Spending (2001-12)"))

india_p1 <- ggplot(data = raw_in)+
geom_line(aes(y = SH.XPD.OOPC.CH.ZS, x = year), color = "dark blue")+
labs(title= c1_title, 
x = "", 
y = "% health expenditure")+
theme(axis.title=element_text(size=9),
plot.title = element_text(hjust = 0.5, size=12), 
axis.text = element_text(size=7), 
legend.title = element_blank(),
legend.text = element_text(size = 6))+
scale_x_continuous(breaks = seq(from = 2001, to = 2012, by=2))+
coord_cartesian(xlim=c(2001, 2012))+
scale_y_continuous(expand = expand_scale(add = 10))+
#ylim(50, 85)+
#annotate("text", x = 2009, y = 60, label = "Out-of-Pocket Expenses", color = "orange", size = 2.5) +
ggsave("data/output/graphs/v2/india_p1.pdf", height = 3, width = 5)
#2. GVT SPENDING AS % OF GDP
india_gvt_total_spend <- raw_gvt_total_spend %>%
filter(country== "India")
india_p2 <- ggplot(data = india_gvt_total_spend)+
geom_line(aes(y= Value, x = Year), color = "dark blue")+
labs(title = "Government Spending (2001-12)",
x = "", 
y = "% gdp")+
theme(axis.title=element_text(size=9),
plot.title = element_text(hjust = 0.5, size=12), 
axis.text = element_text(size=7), 
legend.title = element_blank(),
legend.text = element_text(size = 6))+
scale_x_continuous(breaks = seq(from = 2001, to = 2012, by=2))+
coord_cartesian(xlim=c(2001, 2012))+
scale_y_continuous(expand = expand_scale(add = 10))+
#ylim(15, 40)+
#annotate("text", x = 2009, y = 20, label = "Government spending", color = "dark blue", size = 2.5) +
ggsave("data/output/graphs/v2/india_p2.pdf", height = 3, width = 5)
#3. GVT HEALTH EXPENDITURE (% CURRENT HEALTH EXPENDITURE)
india_p3 <- ggplot(data = raw_in)+
geom_line(aes(y = SH.XPD.GHED.CH.ZS, x = year), color = "dark blue")+
labs(title = "Government Health Spending (2001-12)",
x = "", 
y = "% health expenditure")+
theme(axis.title=element_text(size=9),
plot.title = element_text(hjust = 0.5, size=12), 
axis.text = element_text(size=7), 
legend.title = element_blank(),
legend.text = element_text(size = 6))+
scale_x_continuous(breaks = seq(from = 2001, to = 2012, by=2))+
coord_cartesian(xlim=c(2001, 2012))+
scale_y_continuous(expand = expand_scale(add = 10))+
#ylim(5,40)+
#annotate("text", x = 2009, y = 20, label = "Government spending", color = "dark blue", size = 2.5)+
ggsave("data/output/graphs/v2/india_p3.pdf", height = 3, width = 5)
#4. GVT HEALTH EXPENDITURE (% TOTAL PUBLIC EXPENDITURE)
india_p4 <- ggplot(data = raw_in)+
geom_line(aes(y = SH.XPD.GHED.GE.ZS, x = year), color = "dark blue")+
labs(title = "Budget Prioritization of Health (2001-12)",
x = "", 
y = "% government expenditure")+
theme(axis.title=element_text(size=9),
plot.title = element_text(hjust = 0.5, size=12), 
axis.text = element_text(size=7), 
legend.title = element_blank(),
legend.text = element_text(size = 6))+
scale_x_continuous(breaks = seq(from = 2001, to = 2012, by=2))+
coord_cartesian(xlim=c(2001, 2012))+
scale_y_continuous(expand = expand_scale(add = 10))+
#ylim(0, 10)+
ggsave("data/output/graphs/v2/india_p4.pdf", height = 3, width = 5)
#5. EXTERNAL HEALTH SPENDING 
india_p5 <- ggplot(data = raw_in)+
geom_line(aes(y = SH.XPD.EHEX.CH.ZS, x = year), color = "dark blue")+
labs(title = "External Health Spending (2001-12)",
x = "", 
y = "% health expenditure")+
theme(axis.title=element_text(size=9),
plot.title = element_text(hjust = 0.5, size=12), 
axis.text = element_text(size=7), 
legend.title = element_blank(),
legend.text = element_text(size = 6))+
scale_x_continuous(breaks = seq(from = 2001, to = 2012, by=2))+
coord_cartesian(xlim=c(2001, 2012))+
scale_y_continuous(expand = expand_scale(add = 10))+
#ylim(0, 10)+
#annotate("text", x = 2009, y = 5, label = "External Health Expenditure", color = "hot pink", size = 2.5) +
ggsave("data/output/graphs/v2/india_p5.pdf", height = 3, width = 5)
#6. GDP GROWTH 
india_p6 <- ggplot(data = raw_in)+
geom_line(aes(y = NY.GDP.MKTP.KD.ZG, x = year), color = "dark blue")+
labs(title = "Annual GDP Growth (6.5%) (2001-12)",
x = "", 
y = "%")+
theme(axis.title=element_text(size=9),
plot.title = element_text(hjust = 0.5, size=12), 
axis.text = element_text(size=7), 
legend.title = element_blank(),
legend.text = element_text(size = 6))+
scale_x_continuous(breaks = seq(from = 2001, to = 2012, by=2))+
coord_cartesian(xlim=c(2001, 2012))+
scale_y_continuous(expand = expand_scale(add = 10))+
#ylim(0, 20)+
#annotate("text", x = 2009, y = 20, label = "Government spending", color = "dark blue", size = 2.5) +
ggsave("data/output/graphs/v2/india_p6.pdf", height = 3, width = 5)
#INDIA: COMBINE PLOTS 
india_combined<- plot_grid(india_p1, india_p3, 
india_p4, india_p5,
india_p2,india_p6,
ncol= 2, nrow=3, tags= "x")
ggsave(filename="data/output/graphs/v2/india_combined.pdf", plot = india_combined, height = 7, width = 7)

对所有52个国家来说,最好的方法是什么?

谢谢!

尝试sapply()。例如,对于第二类地块GVT SPENDING AS % OF GDP:

required_countries <-c ("United States", "India",.....)
sapply(required_countries, FUN=function(x){
india_gvt_total_spend <- raw_gvt_total_spend %>%
filter(country== x)
title_preformatted <- paste0("Government Spending for",x, "(2001-12)")

x_p2 <- ggplot(data = india_gvt_total_spend)+
geom_line(aes(y= Value, x = Year), color = "dark blue")+
labs(title = title_preformatted,
x = "", 
y = "% gdp")+
theme(axis.title=element_text(size=9),
plot.title = element_text(hjust = 0.5, size=12), 
axis.text = element_text(size=7), 
legend.title = element_blank(),
legend.text = element_text(size = 6))+
scale_x_continuous(breaks = seq(from = 2001, to = 2012, by=2))+
coord_cartesian(xlim=c(2001, 2012))+
scale_y_continuous(expand = expand_scale(add = 10))+
#ylim(15, 40)+
#annotate("text", x = 2009, y = 20, label = "Government spending", color = "dark blue", size = 2.5) +
ggsave(path_preformatted, height = 3, width = 5)
}
)

如果你真的想用for循环来做这件事,你可以遍历required_countries向量:

required_countries <-c ("United States", "India",.....)
for (i in 1:length(required_countries)){
india_gvt_total_spend <- raw_gvt_total_spend %>%
filter(country== required_countries[i])
title_preformatted <- paste0("Government Spending for",required_countries[i]), "(2001-12)")
path_preformatted <- paste0("data/output/graphs/v2/",required_countries[i]),"_p2.pdf")
path_preformatted <- gsub(" ","", path_preformatted) #strip white space - e.g. "United States" -> "UnitedStates"

x_p2 <- ggplot(data = india_gvt_total_spend)+
geom_line(aes(y= Value, x = Year), color = "dark blue")+
labs(title = title_preformatted,
x = "", 
y = "% gdp")+
theme(axis.title=element_text(size=9),
plot.title = element_text(hjust = 0.5, size=12), 
axis.text = element_text(size=7), 
legend.title = element_blank(),
legend.text = element_text(size = 6))+
scale_x_continuous(breaks = seq(from = 2001, to = 2012, by=2))+
coord_cartesian(xlim=c(2001, 2012))+
scale_y_continuous(expand = expand_scale(add = 10))+
#ylim(15, 40)+
#annotate("text", x = 2009, y = 20, label = "Government spending", color = "dark blue", size = 2.5) +
ggsave(path_preformatted, height = 3, width = 5)
}

如果不想手动键入所有国家名称,请在希望绘制的国家集中查找唯一值:

required_countries <- unique(dataset$countries)

相关内容

最新更新