how to create a stack histogram with actual Y values

111 views Asked by At

I have a dataset and want to create a stack histogram. I have the code below for both data and the histogram itself. However, when I run this code, I do get values of 200, which is not what I want. I understand my code is adding the values of the different groups. but what i do want is for them to reflect the actual values instead.

bar graph for total LE

 library
#library(ggplot2)
#library(viridis)
#library(hrbrthemes)

#dataset
group <- c(rep("Men 45" , 4) , rep("Men 65" , 4) , rep("Women 45" , 4) , rep("Women 65" , 4) )
pa <- rep(c("No activity" , "Inactive" , "Active", 'Very active') , 4)
value <- c(48.84,50.70, 51.06 , 51.15,51.11,52.38,52.80,52.90,29.17,30.44,30.86,30.99,30.24,31.69,32.17,32.29)
data <- data.frame(group,pa,value)

ggplot(data = data, aes(x=group, y=value, group = pa)) +
  geom_col(aes(fill = pa), position = position_stack(reverse = TRUE)) +
  geom_hline(yintercept = 0)
1

There are 1 answers

0
Kat On

You didn't tag your question with the programming language - you would have likely gotten an answer a lot sooner with that tag! (I added it now.)

You wrote that you were looking for a histogram that shows actual values, but a histogram bins a continuous value. Your code calls a bar graph which defaults to a total count when nothing else is specified. You wrote that you wanted the actual values, you could sum the values, or you could choose another type of plot.

Two options come to mind. The first is simply because you mentioned a histogram. I couldn't find a meaningful way to represent the three variables, though.

# this uses two libraries
library(tidyverse)
library(ggridges)

ggplot(data, aes(y = group,
                 x = value,
                 fill = group)) +
  geom_density_ridges() +
  theme_ridges() +
  ggtitle("ggridges package: geom_density_ridges()")

enter image description here

Another option that gets all of your variables and all of your values into a meaningful graph. This graph dodges, like an extreme version of geom_jitter().

# uses two libraries
library(tidyverse)
library(ggbeeswarm)

ggplot(data, aes(x = group,
                 y = value,
                 color = pa)) + 
  geom_quasirandom() + 
  theme_bw() +
  ggtitle("ggbeeswarm package: geom_quasirandom()")

enter image description here