我正在尝试构建一个看起来像
的堆叠条形图
我的 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轴值错误的图形。
有办法解决吗?
1条答案
按热度按时间d4so4syb1#
更新(删除基本答案):
使用
geom_col()
,我们可以在aes
中设置y
: