Reputation: 101
I am having trouble figuring out how to create a specific style of plot in ggplot.
I have data in a tibble that looks like this:
indicator 2015 2019
wdi_lfpr 55.6 58.2
wdi_lfprf 34.9 38.2
wdi_lfprm 77.0 78.4
The values under each year are percents. I would like to plot these so that each indicator appears side by side, and shows values for each year (2015, 2019).
I can't figure out how to go about this in ggplot. Thank you for any help.
EDIT: Thanks to advice from commenters, I have reshaped my data to this format:
indicator year value
wdi_lfpr 2015 55.6
wdi_lfprm 2015 34.9
wdi_lfprf 2015 77.0
wdi_lfpr 2019 58.2
wdi_lfprm 2019 58.2
wdi_lfprf 2019 58.2
Upvotes: 1
Views: 4607
Reputation: 3326
One solution would be:
library(ggplot2)
library(tidyverse)
library(dplyr)
df = data.frame(year = c(2015, 2019),
wdi_lfpr = c(55.6, 58.2),
wdi_lfprf = c(34.9, 38.2),
wdi_lfprm = c(77.0, 78.4)) %>%
pivot_longer(cols = 2:4, names_to = "indicator", values_to = "percent")
ggplot(df, aes(x = as.factor(year), y = percent, fill = indicator)) +
geom_bar(stat = "identity", position = "dodge")
Or:
ggplot(df, aes(x = as.factor(indicator), y = percent, fill = as.factor(year))) +
geom_bar(stat = "identity", position = "dodge")
Upvotes: 2
Reputation: 2758
As others have mentioned, you'll need to make your data tidy before you can use ggplot2
to its full effect:
# Define the dataset
data <- tribble(
~indicator , ~"2015", ~"2019",
"wdi_lfpr" , 55.6 , 58.2,
"wdi_lfprf" , 34.9 , 38.2,
"wdi_lfprm" , 77.0 , 78.4
)
# 'pivot' the data so that every column is a variable
tidy_data <- data %>%
tidyr::pivot_longer(c(`2015`, `2019`), names_to = "year", values_to = "value")
In your example plot there are some issues.
Fortunately, ggplot2
takes care of most of this by default if you make a prudent choice for the fill
aesthetic:
ggplot(tidy_data, aes(x = indicator, fill = year, y = value)) +
geom_col(position = "dodge")
If you prefer the classic r-graphics style (similar to your example) and you don't want to use colour you can do so using something like the following with theme_classic()
:
ggplot(tidy_data, aes(x = indicator, group = year, y = value)) +
geom_col(position = "dodge", colour = "white") +
theme_classic()
Upvotes: 1
Reputation: 101
Thank you to everyone for your help. After reshaping the data I was able to reach this solution, with input from the suggestions:
ggplot(long_df, aes(x = as.factor(indicator), y = value, fill = as.factor(year))) +
geom_bar(stat = "identity", position = "dodge")
Which has let me produce this figure, which was my goal:
Upvotes: 0