Make the truncation axis of the y axis the same as for the histogram

We use dotplots in the classroom to introduce a bar chart because the binning concept confuses many students. So, we start from a point that is similar, but more intuitive:

x <- rnorm(100) qplot(x, geom = "bar") qplot(x, geom = "dotplot", method="histodot") 

dotplot

Since students do this according to their own data, the code should work without manual driving. However, geom_dotplot seems to use different default scaling values ​​than geom_bar . The y axis is not adjusted with the data, but, apparently, depends only on the size of the points. For instance:

 x <- runif(1000) qplot(x, geom = "bar") qplot(x, geom = "dotplot", method="histodot") 

dotplot2

How can I make geom_dotplot with stat_histodot scale the y axis exactly as it would for a histogram, or using smaller or overlapping points?

+5
source share
1 answer

I came up with the following workaround, which reduces the width of the bean until everything fits on the page:

 # This function calculates a default binwidth that will work better # for the dotplot with large n than the ggplot2 default. calculate_smart_binwidth <- function(x, aspect_ratio = 2/3){ x <- as.numeric(x) nbins <- max(30, round(sqrt(length(x)) / aspect_ratio)) range <- range(x, na.rm = TRUE, finite = TRUE) if(diff(range) == 0) return(NULL) repeat { message("trying nbins: ", nbins) binwidth <- diff(range)/nbins; highest_bin <- max(ggplot2:::bin(x, binwidth = binwidth)$count); if(highest_bin < aspect_ratio * nbins) return(binwidth) nbins <- ceiling(nbins * 1.03); } } 

Examples:

 x <- runif(1e4) qplot(x, geom="dotplot", binwidth=calculate_smart_binwidth(x)) 

plot1

 x <- rnorm(1e4) qplot(x, geom="dotplot", binwidth=calculate_smart_binwidth(x)) 

plot2

+3
source

Source: https://habr.com/ru/post/1203855/


All Articles