Reputation: 2520
But I need opposite - year below and and countries on the top as labels.
Two SO answers are here
Code
ggplot(ownership, aes(x = Country, y = Percent, fill = Category)) +
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Year) +
theme_tufte() +
scale_fill_brewer(palette = "Paired") +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(legend.text = element_text(size = 10)) +
theme(axis.text.x = element_text(size = 12)) +
theme(axis.text.y = element_text(size = 12)) +
theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm")) +
theme(legend.position = "bottom") +
theme(legend.title = element_blank())
Data
> dput(ownership)
structure(list(Country = c("Cote d'Ivoire", "Cote d'Ivoire",
"Ethiopia", "Ethiopia", "Kenya", "Kenya", "Nigeria", "Nigeria",
"Senegal", "Senegal", "South Africa", "South Africa", "Uganda",
"Uganda", "Cote d'Ivoire", "Cote d'Ivoire", "Ethiopia", "Ethiopia",
"Kenya", "Kenya", "Nigeria", "Nigeria", "Senegal", "Senegal",
"South Africa", "South Africa", "Uganda", "Uganda", "Cote d'Ivoire",
"Cote d'Ivoire", "Ethiopia", "Ethiopia", "Kenya", "Kenya", "Nigeria",
"Nigeria", "Senegal", "Senegal", "South Africa", "South Africa",
"Uganda", "Uganda"), Year = c(2014, 2017, 2014, 2017, 2014, 2017,
2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014,
2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017,
2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014, 2017, 2014,
2017, 2014, 2017), Percent = c(10, 7, 22, 35, 16, 9, 42, 34,
9, 11, 56, 50, 9, 9, 5, 7, 0, 0, 39, 47, 2, 5, 3, 10, 13, 17,
18, 24, 19, 27, 0, 0, 19, 26, 0, 0, 4, 22, 2, 2, 17, 26), Category =
c("Category A",
"Category A", "Category A", "Category A", "Category A", "Category A",
"Category A", "Category A", "Category A", "Category A", "Category A",
"Category A", "Category A", "Category A", "Category B", "Category B",
"Category B", "Category B", "Category B", "Category B", "Category B",
"Category B", "Category B", "Category B", "Category B", "Category B",
"Category B", "Category B", "Category C", "Category C", "Category C",
"Category C", "Category C", "Category C", "Category C", "Category C",
"Category C", "Category C", "Category C", "Category C", "Category C",
"Category C")), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -42L), spec = structure(list(cols = list(
Country = structure(list(), class = c("collector_character",
"collector")), Year = structure(list(), class = c("collector_double",
"collector")), Percent = structure(list(), class = c("collector_double",
"collector")), Category = structure(list(), class =
c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
Any tips are appreciated!
UPDATED
After changing X = Year
and facet_grid(~ Country)
I got a better result with some problem on x axis. R treats year differently than I expected. I have 2014 and 2017, it offers me 2014, 2016, 2017.
Upvotes: 0
Views: 188
Reputation: 69151
This gets you close (using simulated data since you didn't provide any):
library(ggplot2)
#make similar data
df <- expand.grid(country = letters[1:4],
year = c("2014", "2017"),
category = LETTERS[1:3])
df$percent <- runif(nrow(df))
ggplot(df, aes(year, percent, fill = category)) +
geom_bar(stat = "identity") +
facet_wrap(~country, ncol = 4, strip.position = "bottom") +
theme(legend.position = "none")
Created on 2019-02-09 by the reprex package (v0.2.1)
Upvotes: 2
Reputation: 2520
Resolved personally. I added as.character()
to x axis.
Final code
ggplot(ownership, aes(x = as.character(Year), y = Percent, fill = Category)) +
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Country) +
theme_tufte() +
scale_fill_brewer(palette = "Paired") +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(legend.text = element_text(size = 10)) +
theme(axis.text.x = element_text(size = 12)) +
theme(axis.text.y = element_text(size = 12)) +
theme(legend.position = "bottom") +
theme(legend.title = element_blank())
Done! Thanks!
Upvotes: 0
Reputation: 956
Dont use facet_grid(~ Year)
, this will make two side by side graphs for the two years (as you have now).
You could have a similar graph to the one you want by using a country-year variable, so:
ownership$CountryYear <- paste(ownership$Country, ownership$Year)
and then:
ggplot(ownership, aes(x = CountryYear, y = Percent, fill = Category)) +
geom_bar(stat = 'identity', position = 'stack') +
...
But you may have to play around a lot with the labels to get a graph exactly like the one you are aiming for.
Upvotes: 1