修复了堆叠条形图ggplot2的y轴"setup_params()“中的错误所导致的错误

0x6upsns  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(980)

我正在尝试构建一个看起来像

的堆叠条形图
我的 Dataframe 如下所示:

df = structure(list(Year_Publication = c(1989L, 1994L, 2001L, 2002L, 
                                         2002L, 2004L, 2006L, 2007L, 2009L, 2011L, 2012L, 2013L, 2014L, 
                                         2015L, 2016L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2019L, 
                                         2019L, 2019L, 2020L, 2020L, 2020L, 2020L, 2021L, 2021L, 2022L, 
                                         2022L, 2022L, 2022L), Taxa = c("Cervidae", "Teleostei", "Chondrichtyes", 
                                                                        "Chondrichtyes", "Gastropoda", "Teleostei", "Malacostraca", "Teleostei", 
                                                                        "Teleostei", "Teleostei", "Teleostei", "Teleostei", "Teleostei", 
                                                                        "Teleostei", "Teleostei", "Chondrichtyes", "Teleostei", "Chondrostei", 
                                                                        "Teleostei", "Chondrichtyes", "Decapoda", "Teleostei", "Gastropoda", 
                                                                        "Chondrichtyes", "Chondrostei", "Teleostei", "Bivalvia", "Tetrapoda", 
                                                                        "Teleostei", "Orthoptera", "Chondrichtyes", "Teleostei", "Reptilia", 
                                                                        "Bovidae"), Total_Species_Per_Taxa = c(1L, 2L, 1L, 1L, 3L, 1L, 
                                                                                                               1L, 1L, 2L, 4L, 2L, 1L, 1L, 2L, 4L, 2L, 4L, 1L, 7L, 3L, 1L, 6L, 
                                                                                                               1L, 3L, 1L, 7L, 2L, 1L, 1L, 1L, 4L, 4L, 1L, 1L), Total_Species_Per_Pub = c(1L, 
                                                                                                                                                                                          2L, 1L, 4L, 4L, 1L, 1L, 1L, 2L, 4L, 2L, 1L, 1L, 2L, 4L, 7L, 7L, 
                                                                                                                                                                                          7L, 11L, 11L, 11L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 2L, 2L, 
                                                                                                                                                                                          10L, 10L, 10L, 10L)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                    -34L))

我试过这个代码:

ggplot(data = taxa_diversity, aes(x = `Year_Publication`, fill = Taxa)) +
  geom_bar(aes(y = `Total_Species_Per_Pub`), stat = "count")

AND

ggplot(data = taxa_diversity, aes(x = `Year_Publication`, y = `Total_Species_Per_Pub`, fill = Taxa)) +
  geom_bar(stat = "count")

但我一直收到这个错误

Error in `geom_bar()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `setup_params()`:
! `stat_count()` must only have an x or y aesthetic.
Run `rlang::last_error()` to see where the error occurred.

我知道这是y轴的错误,因为我可以在不包含y轴命令的情况下让它工作...但它给了我一个y轴值错误的图形。
有办法解决吗?

d4so4syb

d4so4syb1#

更新(删除基本答案):
使用geom_col(),我们可以在aes中设置y

my_color <- c("#41859f", "#404040", "#ea9f91","#bfd2d9",
              "#981f26", "#575a7b", "#ce2a0a", "#eddca3",
              "#2c6049", "#41859f", "#404040", "#ea9f91")
library(ggplot2)
ggplot(data =df, aes(x = `Year_Publication`, y = `Total_Species_Per_Pub`, fill = Taxa)) +
  geom_col()+
  scale_x_continuous("Year_Publication", labels = as.character(df$Year_Publication), breaks = df$Year_Publication)+
  scale_fill_manual(values=my_color)+
  theme_classic()

相关问题