ggplot2 geom_col() not working for non-integer x values

Viewed 153

This question is about making a simple bar (column) chart using ggplot2 in R. When I try to shift the x positions of the bars, everything falls apart. Here's what I've done: I have loaded up ggplot2 and have created the following data frame.

library(ggplot2)
df_SNR <- 
  structure(list(
    SNR = c(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
               12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26), 
    count = c(16L, 27L, 174L, 448L, 1582L, 1715L, 1639L, 1304L, 
              853L, 778L, 612L, 473L, 374L, 275L, 210L, 168L, 140L, 101L, 
              101L, 85L, 82L, 61L, 63L, 44L, 41L, 36L, 26L, 23L, 20L, 60L
    )), 
    row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"))

I can make a decent bar chart with this data frame, as follows:

ggplot(df_SNR)+
  aes(x = SNR, y = count)+
  geom_col()

enter image description here

But I actually need to shift the x positions over by 0.5. So I do this:

ggplot(df_SNR)+
  aes(x = SNR+0.5, y = count)+
  geom_col()

enter image description here

The plot is wrecked.

First troubleshooting: adjust the x position by 1 instead of 0.5:

ggplot(df_SNR)+
  aes(x = SNR+1, y = count)+
  geom_col()

enter image description here

It works!

What about adjusting by 1.5?

ggplot(df_SNR)+
  aes(x = SNR+1.5, y = count)+
  geom_col()

enter image description here

Wrecked. it doesn't like non-integer x positions.

Is it failing because the x position is changed in the ggplot code? I tried to change the x position variable (SNR) in the actual data frame, so that it wouldn't require any adjustment in the code.

df_SNR2 <- df_SNR
df_SNR2$SNR <- df_SNR2$SNR + 0.5

ggplot(df_SNR2)+
  aes(x = SNR, y = count)+
  geom_col()

enter image description here

Still wrecked.

The code with the +0.5 adjustment worked before I updated R to 4.0.0. Is anyone having this problem? Does anyone know how to fix it? is it a bug?

Here's my session info:

R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[6] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[6] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[6] ggplot2_3.3.1

loaded via a namespace (and not attached):
 [6] rstudioapi_0.11  magrittr_1.5     tidyselect_1.1.0 munsell_0.5.0    colorspace_1.4-1
 [6] R6_2.4.1         rlang_0.4.6      fansi_0.4.1      dplyr_1.0.0      tools_4.0.0     
[11] grid_4.0.0       gtable_0.3.0     cli_2.0.2        withr_2.2.0      ellipsis_0.3.1  
[16] digest_0.6.25    assertthat_0.2.1 tibble_3.0.1     lifecycle_0.2.0  crayon_1.3.4    
[21] purrr_0.3.4      farver_2.0.3     vctrs_0.3.0      glue_1.4.1       labeling_0.3    
[26] compiler_4.0.0   pillar_1.4.4     generics_0.0.2   scales_1.1.1     pkgconfig_2.0.3 
1 Answers

Thank you Henrik for posting the answer! I need to add

orientation = "x" 

to

geom_col()
Related