Reputation: 23
EDIT: SOLVED Used this code:
gg5 <- ggplot(df_tidy, aes(x=Year, y=Landing))+
geom_line()
gg5 + facet_wrap(~df_tidy$country)
I'm sorry, this is slightly a duplicate question. But I'm asking it because I've tried the different answers and nothing seems to work.
I want to create some plots. I want to have separate plots per country. And I want to have different lines on each plot for region. I've got ggplot2 and I've got ggplus. In total, I'm going to have approximately 150 plots, so facet_multiple seems to be the answer so I can spread it over a number of pages. But what I am getting in practice is not what I anticipate in theory.
Here's some dummy data:
library(tidyverse)
country = c("USA", "Canada", "UK", "France", "Mexico")
region = c("North America", "North America", "Europe", "Europe", "North America")
2000 = c(50, 100, 75, 60, 25)
2001 = c(200, 50, 150, 125, 60)
2002 = c(125,75,60, 75, 25)
df = data.frame(country, region, 2000, 2001, 2002)
df_tidy <- df %>% gather(Year, Landing, c(3:5))
country region Year Value
1 USA North America 2000 50
2 Canada North America 2000 100
3 UK Europe 2000 75
4 France Europe 2000 60
5 Mexico North America 2000 25
6 USA North America 2001 200
7 Canada North America 2001 50
8 UK Europe 2001 150
9 France Europe 2001 125
10 Mexico North America 2001 60
11 USA North America 2002 125
12 Canada North America 2002 75
13 UK Europe 2002 60
14 France Europe 2002 75
15 Mexico North America 2002 25
Please just imagine there might be 2 (or 3 or 4..) different regions per country.
Now, if I break individual countries out into their own data.frame, I get the exact plot I want: Correct plot!
#This works
USA <- df_tidy %>%
filter(Country %in% c("USA") & between(Year,2000,2002)) %>%
select(Country, Year, Landing)
ggplot(USA, aes(x=Year, y=Landing)) +
geom_line(data = USA)
#End of USA plot
But when I try to ggplot the entire data frame using facets, I get something like this: Really weird plot
I've tried multiple different ways to do it, and if I don't get an error message, I get the really weird plot.
Some code I've tried:
#Attempt 1:
gg1 <- ggplot(df_tidy, aes(x=df_tidy$Year, y=df_tidy$Landing, color = df_tidy$region))+
geom_line()
#doesnt work
gg1+facet_wrap(~df_tidy$country, ncol = 4, nrow = 4)
#doesnt work
gg1 + facet_grid(df_tidy$country) + facet_grid(df_tidy$region)
#doesnt work
facet_grid(gg1, facets = "country")
#Attempt 2
plist = lapply(split(df_tidy, df_tidy$country), function(d) {
ggplot(d, aes(x=Year, y=Landing))+
geom_line()+
facet_wrap(~ df_tidy$country)+
scale_y_continuous()})
library(gridExtra)
for(i in seq(1, length(plist), 6)) {
pdf(paste0("country_", i, ".pdf"), 7, 5)
grid.arrange(grobs=plist[i:(i+5)],
ncol(3, left="Landing", bottom="Year"))}
Anyways, I can put up every attempt, but I've deleted and changed code so much I don't even remember where I started. Any advice would be appreciated because I really dont want to have to each country by seperate data frame. Thanks!
Update - after trying the solution, here's what I'm still getting:
myPlots <- list()
for(i in unique(df_tidy$region)) {
myPlots[[i]] <- ggplot(subset(df_tidy, df_tidy$country == i),
aes(Year, Landing, color = region)) +
geom_line(size=2)+
labs(title = paste("Landings in", i),
subtitle = "From 2000-2002",
x = "Year",
y = "Landing",
color = NULL) +
scale_color_brewer(palette = "Set2") +
theme_minimal()+
theme(legend.position = "bottom")
}
library(ggpubr) ggarrange(plotlist = myPlots)
ggplot(df_tidy, aes(Year, Landing))+
geom_line()+
facet_wrap(~ df_tidy$region + df_tidy$country)
Upvotes: 1
Views: 987
Reputation: 28339
As you mentioned this question might be a duplicate as there have been many similar problems, however sometimes they are very specific.
To begin with we can use simple facet_wrap
:
ggplot(df_tidy, aes(Year, Landing)) +
geom_line() +
facet_wrap(~ region + country)
Here I'm creating facets that are combinations of region
and country
(~ region + country
)
Another solution would be to
Here I will use same pallet per region (also added minimal visual tweaks).
# Loop over regions and create separate plots
myPlots <- list()
for(i in unique(df_tidy$region)) {
myPlots[[i]] <- ggplot(subset(df_tidy, region == i),
aes(Year, Landing, color = country)) +
geom_line(size = 2) +
labs(title = paste("Landing change in", i),
subtitle = "From xxxx to yyyy",
x = "Year",
y = "Landing",
color = NULL) +
scale_color_brewer(palette = "Set2") +
theme_minimal() +
theme(legend.position = "bottom")
}
# Combine plots with some function
library(ggpubr)
ggarrange(plotlist = myPlots)
Data & Packages:
library(tidyverse)
df_tidy <- structure(list(country = structure(c(5L, 1L, 4L, 2L, 3L, 5L,
1L, 4L, 2L, 3L, 5L, 1L, 4L, 2L, 3L), .Label = c("Canada", "France",
"Mexico", "UK", "USA"), class = "factor"), region = structure(c(2L,
2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L), .Label = c("Europe",
"North America"), class = "factor"), Year = c(2000, 2000, 2000,
2000, 2000, 2001, 2001, 2001, 2001, 2001, 2002, 2002, 2002, 2002,
2002), Landing = c(50, 100, 75, 60, 25, 200, 50, 150, 125, 60,
125, 75, 60, 75, 25)), row.names = c(NA, -15L), .Names = c("country",
"region", "Year", "Landing"), class = "data.frame")
Upvotes: 0