I ran into a missing graph issue while developing histograms and geometry bar plots in r using the ggplot function. The issue is intermittent and occurs across multiple data sets. One data set, the largest, is described here.
My initial code, used to generate randomized data, is this:
Nruns <- 15
# output structure as a list for convenience
out_structure <- vector( "list", Nruns )
for ( i_dice in seq_len( Nruns ) ) {
# Number of Successes for y rolls -- rolls = 1
Num.Dice <- i_dice # number of dic
sr. <- as.numeric() # create empty vector -- ShadowRun 15 dice
for(i in 1:500000) {
sr.[(i:i)] <- sum(sample(1:6, Num.Dice, replace=TRUE)>=5)
}
out_structure[[i_dice]] <- sr.
}
sr.1to15 <- as.data.frame(out_structure,
col.names = paste0("sr.", seq_len( Nruns )))
This generates 15 variables based on 500000 samples counting the results 1 through 6.
My ggplot code is this:
library(ggplot2)
library(scales)
ggplot(sr.1to15) +
aes(sr.15) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
scale_x_continuous(breaks = seq(1:15), lim = c(0, 15)) +
scale_y_binned(labels = percent_format(accuracy = 1))
Or this; depending on the other variables I check out (pictured here are two successive passes over the same code):
Attempts to fix this:
(1) Exited R. Reopened R.
(2) Turned laptop off. Turned laptop back on again.
(3) Attempted to remove all objects and packages.
(4) Cleaned up global environment via rstudio interface.
remove(list = ls())
None of these attempts rectified the error.
Note: The following code does work consistently:
hist(sr.1to15$sr.15) [not pictured here]
My current libraries are:
> my_packages <- library()$results[,1]
> head(my_packages, 10)
[1] "abind" "ade4" "askpass" "assertthat" "backports"
[6] "base64enc" "BH" "blob" "broom" "callr"
How do I get ggplot to work on a consistent basis?
The issue is that you are using scale_y_binned()
. The rest of the code works fine for me, except when I add this particular line to the plot. The "binning" is working (your y axis has %'s), but you see no geoms because ultimately, ggplot2
is plotting the histogram using the same geom used for geom_bar
/geom_col
. This geom requires a continuous y axis, and scale_y_binned()
is designed to "bin" or "discretize" the y axis. So... the bars are being plotting on both charts, but once binned, ggplot2
has no idea how to draw your geom.
As for why you're seeing the inconsistency... not sure. Sometimes there is a time component to executing the code. When I run your code, it consistently gives me the second chart (no bars).
To fix, you need to use scale_y_continuous()
and set the labels.
ggplot(sr.1to15) +
aes(sr.15) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
scale_x_continuous(breaks = seq(1:15), lim = c(0, 15)) +
scale_y_continuous(labels = percent_format(accuracy = 1))