Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected behviour from plotHistogram for different pairs of options #389

Open
pchelle opened this issue Oct 6, 2022 · 1 comment
Open
Labels
feedback required question Further information is requested

Comments

@pchelle
Copy link
Collaborator

pchelle commented Oct 6, 2022

Can you let me know if the expected behaviour of option frequency seems correct.

Code Example
library(tlf)
library(patchwork)

data <- data.frame(
Age = rlnorm(100), 
Gender = c(rep("Female", 60), rep("Male", 40))
)
dataMappingNoGroup <- HistogramDataMapping$new(x = "Age")
dataMappingGroup <- HistogramDataMapping$new(
x = "Age", 
fill = "Gender"
)

frequency = FALSE

stack = FALSE

The count of the total is higher than the count in each group

Code
wrap_plots(
setYAxis(
plotHistogram(data = data, dataMapping = dataMappingNoGroup, stack = FALSE, frequency = FALSE), 
limits = c(0, 50)), 
setYAxis(
plotHistogram(data = data, dataMapping = dataMappingGroup, stack = FALSE, frequency = FALSE), 
limits = c(0, 50)), 
ncol = 2
)

image

stack = TRUE

The count of the total is the same as the addition (stacking) of count in each group

Code
wrap_plots(
setYAxis(
plotHistogram(data = data, dataMapping = dataMappingNoGroup, stack = TRUE, frequency = FALSE), 
limits = c(0, 50)), 
setYAxis(
plotHistogram(data = data, dataMapping = dataMappingGroup, stack = TRUE, frequency = FALSE), 
limits = c(0, 50)), 
ncol = 2
)

image

frequency = TRUE

stack = FALSE

The relative frequency is calculated within each group aiming at having comparable distributions between groups.

In this example,

  • Total count is divided by 100
  • Male gender is divided by 40
  • Female gender is divided by 60
Code
wrap_plots(
setYAxis(
plotHistogram(data = data, dataMapping = dataMappingNoGroup, stack = FALSE, frequency = TRUE), 
limits = c(0, 0.5)), 
setYAxis(
plotHistogram(data = data, dataMapping = dataMappingGroup, stack = FALSE, frequency = TRUE), 
limits = c(0, 0.5)), 
ncol = 2
)

image

stack = TRUE

The relative frequency is calculated from the total count
So that the frequency of the total is the same as the addition (stacking) of frequencies of each group

Code
wrap_plots(
setYAxis(
plotHistogram(data = data, dataMapping = dataMappingNoGroup, stack = TRUE, frequency = TRUE), 
limits = c(0, 0.5)), 
setYAxis(
plotHistogram(data = data, dataMapping = dataMappingGroup, stack = TRUE, frequency = TRUE), 
limits = c(0, 0.5)), 
ncol = 2
)

image

@pchelle pchelle added question Further information is requested feedback required labels Oct 6, 2022
@Felixmil
Copy link
Contributor

Felixmil commented Apr 4, 2023

I think this is good. However, I would increase the alpha level for stack=FALSE plots to improve readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback required question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants