ggplot2 geom_col()不适用于非整数x值 [英] ggplot2 geom_col() not working for non-integer x values

查看:57
本文介绍了ggplot2 geom_col()不适用于非整数x值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于在R中使用ggplot2创建一个简单的柱状图.当我尝试移动柱状图的x位置时,一切都崩溃了.这是我所做的:我已经加载了ggplot2并创建了以下数据框.

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()

但是我实际上需要将x位置偏移0.5.所以我这样做:

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()

剧情被破坏了.

第一次故障排除:将x位置调整为1而不是0.5:

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

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

有效!

调整1.5怎么办?

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

坏了.它不喜欢非整数x位置.

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

是否由于ggplot代码中的x位置更改而失败?我尝试更改实际数据帧中的x位置变量(SNR),以便不需要对代码进行任何调整.

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()

还是毁了.

在将R更新为4.0.0之前,具有+0.5调整的代码可以正常工作.有人遇到这个问题吗?有谁知道如何修理它?是一个错误吗?

这是我的会话信息:

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 

推荐答案

感谢Henrik发布答案!我需要添加

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

orientation = "x" 

geom_col()

这篇关于ggplot2 geom_col()不适用于非整数x值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆