简体   繁体   中英

Plot multiple variables by year in the same bar plot

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

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

在此处输入图像描述

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

默认 ggplot2 样式的绘图

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

没有色彩的经典风格情节

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:

图表完成

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM