Search code examples
rggplot2booleanvisualizationboolean-logic

Using R and ggplot2 is there a better way to visualize TRUE and FALSE Boolean Value columns?


So I've got a larger data set, but for simplicity's sake I've done my best to offer a simplified version of my problem/dataset:

So there are a total of 3 Little Pigs. One of the pigs owns just one house I want to plot on the Y axis the total home values and then a faceted view of how much each pig owns.

HOUSES HOUSEVALUE PIG1 PIG2 PIG3
Hay 30000 TRUE FALSE FALSE
Sticks 70000 TRUE TRUE FALSE
Bricks 100000 TRUE TRUE TRUE

And here's a quick sketch of what I'd like this to look like: enter image description here

I'm very rusty with my R usage as well as my GGPLOT2 usage. I'm doing all kinds of crazy stuff with this data, such as:

library(ggplot2)
library(readr)
piggies <- read_csv("piggies.csv")
ggplot(piggies, aes(x=PIG1, fill=as.factor(HOUSEVALUE)))+geom_bar(position='dodge')
ggplot(piggies, aes(x=PIG2, fill=as.factor(HOUSEVALUE)))+geom_bar(position='dodge')

enter image description here

enter image description here

I understand the above ggplot2 visualizations are borderline insane, but I'm having the hardest time tracking down solid resources for columns that are boolean values and making the Y axis represent something other than "Count"

(Edited the example GGPlot formulae to be slightly less insane than my original example)


Solution

  • I'm not sure if this is what you are looking for, it would give the total amount each pig owns of each type stacked. Similar to what @Akrun said, it uses pivot_longer before plotting:

    dat<-data.frame("HOUSES" = c("Hay", "Sticks", "Bricks"), "HOUSEVALUE" = c(30000, 70000, 100000), "PIG1" = c(T,T,T), "PIG2" = c(F,T,T), "PIG3" = c(F,F,T))
    
    
    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    dat%>%
      pivot_longer(cols = starts_with("PIG"))%>%
      filter(value)%>%
      ggplot()+
      aes(name, HOUSEVALUE, fill = HOUSES)+
      geom_bar(stat = "identity")