1

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).example of desired graph

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
2
  • 1
    First, make your data tidy. ggplot2 (and the rest of the tidyverse) is designed to work with tidy data. Your data is not tidy because there is information in your column names. pivot_longer() will be your friend. Commented May 10, 2021 at 15:07
  • 1
    You'll want to reshape your data so the values to plot in y are in a single column instead of 2 columns. See related example here: stackoverflow.com/questions/42820677/… Commented May 10, 2021 at 15:09

3 Answers 3

2

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")

enter image description here

Or:

ggplot(df, aes(x = as.factor(indicator), y = percent, fill = as.factor(year))) +
        geom_bar(stat = "identity", position = "dodge")

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Make Your Data Tidy

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")

Plot With Colour

In your example plot there are some issues.

  • Axes are not properly labelled
  • There is nothing to distinguish between the bars in each group
  • The x axis text does not match any column in your data

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")

Default ggplot2-style plot

Classic-Style Plot

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()

Classic-style plot without colour

Comments

0

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:

graph completed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.