I'm new to R and in fact this is my first post on SO. Apologies if my description and layout isn't ideal - all feedback is welcome.
My problem is that I want to create separate plots from a tibble
of data where each plot is for a different country but of the same variables. I then want to save each plot separately as a png.
I am most familiar with tidyverse
and ggplot
to tidy data and then visualise it. I have created a tibble that has 32,454 observations of 4 variables. The data consists of 200 different countries. I wish to create separate geom_bar
plots for each country, for each of the variables (Gov_bal, Priv_bal, Ext_bal). I wish to stack
the values of each variable for each year and then identify them by fill
.
I have looked here https://stats.stackexchange.com/questions/19213/generate-separate-plots-for-each-group-of-records-in-dataset; here Save multiple ggplots using a for loop and here Plot one histogram(separate) for each variable in the column but I haven't been able to achieve what I want.
Here is an example of my data. I'm sure there's a better way I could replicate it but this is the gist of it.
Country <- c("Aus", "Aus", "Aus", "Aus", "USA", "USA", "USA", "USA", "UK", "UK", "UK", "UK")
Year <- c("1990", "1991", "1992", "1993", "1990", "1991", "1992", "1993", "1990", "1991", "1992", "1993")
Gov_bal <- c(5, 6, 5, 8, 8, 9, 5, 4, 6, 7, 4, 8)
Priv_bal <- c(3, 5, 4, 2, 6, 8, 5, 3, 2, 3, 6, 5)
Ext_bal <- c(2, -1, -2, 4, 5, 1, 3, 7, 4, 2, 3, 1)
sect_balances <- data.frame(Country, Year, Gov_bal, Priv_bal, Ext_bal)
sect_balances <- sect_balances %>% pivot_longer(Gov_bal:Ext_bal, names_to = "Sector", values_to = "Value")
The plot I want for each country looks like this. (I've used filter(Country == "Aus")
to just select one country for this example but I want a function/solution that does this automatically for me.)
sect_balances %>% filter(Country == "Aus") %>%
ggplot(aes(x = Year, y = Value, fill = Sector)) +
geom_bar(position = "stack", stat = "identity") +
labs(x = "Year",
y = "Per cent of GDP",
title = "Australia") +
theme_classic()
I am also aware that I can use facet_wrap
to display all plots by "Country" but I want to save each plot individually.
My problem is that I wish to create a loop or some other solution that cycles through the different countries and creates separate geom_bar
plots. So for my above example of data I want a code that creates three separate geom_bar
plots (e.g. one each for "Aus", "USA", "UK") and then saves each plot separately. Obviously for my actual data I want a code that can do this for 200 different countries.
I have tried this code but to be honest I don't have my head around the functions loop
or map
. I need to do some more reading but any help would be great.
for (i in Country) {
country_id <- subset(sect_balances, Country == i)
p <- ggplot(country_id, aes(x = Year, y = Value)) + geom_bar(position = "stack",
stat = "identity")
png(paste("plot_", i, ".png", sep = ""), width = 600, height = 500, res = 120)
print(p)
dev.off()
}
I can't remember where I found this code that I adjusted, but unfortunately it is not a solution to my problem.
Many thanks