Kevin
Kevin

Reputation: 1233

R combine every two variables into one and plot them side by side with ggplot2

I have a data frame the has several rows that have a connection to each other

input:

Race           | Alabama  Alaska  Arizona    Arkansas
AIAN_Deaths    |    127     1763    46       620        
AIAN_Expected  |    0       444    1178      122    
Asian_Deaths   |    88      40     353       46 
Asian_Expected |    58      52     258       71 

I want to plot AIAN and Asian as one variable side by side like so:

enter image description here

where the red is the "Deaths" and the blue is the "Expected"

How can I achieve it with ggplot2?

Upvotes: 1

Views: 632

Answers (2)

AnilGoyal
AnilGoyal

Reputation: 26218

You may also reshape the data

df <- data.frame(
  stringsAsFactors = FALSE,
                    Race = c("AIAN_Deaths",
                             "AIAN_Expected","Asian_Deaths","Asian_Expected"),
           Alabama = c(127L, 0L, 88L, 58L),
            Alaska = c(1763L, 444L, 40L, 52L),
           Arizona = c(46L, 1178L, 353L, 258L),
          Arkansas = c(620L, 122L, 46L, 71L)
      )
library(tidyverse)
df %>% pivot_longer(!Race, names_to = 'country') %>%
  separate(Race, into=c('Race', 'Type'), sep = '_') %>%
  ggplot() +
  geom_col(aes(x = Race, y = value, fill = Type), position = 'dodge')

This way you'll have flexibility to show State-wise bars as well, if you wanna do it


df %>% pivot_longer(!Race, names_to = 'country') %>%
  separate(Race, into=c('Race', 'Type'), sep = '_') %>%
  ggplot(aes(x = Type, y = value, fill = country)) +
  geom_col(position = 'stack') +
  facet_wrap( ~ Race) + 
  theme(panel.spacing = grid::unit(-1.25, "lines"))

Created on 2021-06-06 by the reprex package (v2.0.0)

Upvotes: 3

Ronak Shah
Ronak Shah

Reputation: 388982

Separate Race data in two columns based on separator (_) and calculate sum of values in each State using rowSums.

library(dplyr)
library(ggplot2)
library(tidyr)

df %>%
  separate(Race, c('Race', 'Type'), sep = '_') %>%
  mutate(value = rowSums(select(., Alabama:Arkansas))) %>%
  ggplot(aes(Race, value, fill = Type)) + 
  geom_col(position = 'dodge')

enter image description here

Upvotes: 2

Related Questions