Home > Back-end >  Plot count of specific value across multiple variables
Plot count of specific value across multiple variables

Time:08-11

My dataset looks like this:

V1 V2 V3 V4
1 0 0 1
0 1 0 0
0 1 1 0
1 0 1 1
0 0 0 0
1 0 1 1

I want to plot a count of the occurrences of 1s per variable, but on one single bar chart (4 bars). I can plot this for each individual variable using ggplot:

ggplot(df,aes(x = v1))   
  geom_bar(stat = 'count')

, but to plot them all on one graph I seem to need to turn my dataframe sideways, but then I can't figure out how to calculate the count of 1s.

Any help would be much appreciated!

CodePudding user response:

We get the sum by column with summarise/across, reshape to long with pivot_longer and then use ggplot

library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
  summarise(across(everything(), sum)) %>%
  pivot_longer(cols = everything()) %>% 
  ggplot(aes(x = name, y = value, fill = name))   
     geom_col()   
     coord_flip()

data

df <- structure(list(V1 = c(1L, 0L, 0L, 1L, 0L, 1L), V2 = c(0L, 1L, 
1L, 0L, 0L, 0L), V3 = c(0L, 0L, 1L, 1L, 0L, 1L), V4 = c(1L, 0L, 
0L, 1L, 0L, 1L)), class = "data.frame", row.names = c(NA, -6L
))

CodePudding user response:

A base R option using barplot stack colSums

barplot(values ~ ind, stack(colSums(df)))

or a more concise way (thank @akrun for the comments)

barplot(colSums(df))

enter image description here

  • Related