ggplot2绘制条形图报错:Cannot add objects together问题求助
问题排查与解决
报错信息
Error in
+.gg:
! Cannot add objects together.
ℹ Did you forget to add this object to a gg object?
Runrlang::last_trace()to see where the error occurred.
错误原因
- 核心语法错误:定义
p1时,scale_fill_manual()语句末尾遗漏了+号,导致后续的scale_x_discrete()、scale_y_continuous()等设置无法与ggplot对象正确拼接,R无法识别独立代码片段与gg对象的相加操作。 - 轴类型不匹配:
Avg是数值型变量,使用scale_x_discrete()会导致x轴渲染异常,应替换为scale_x_continuous();Year是离散型变量,原代码用scale_y_continuous()错误,需改用scale_y_discrete()。 - 兼容性问题:
legend.position.inside是ggplot2 3.5.0+的新参数,低版本不支持,改用legend.position和legend.justification组合实现图例内部放置。
修正后完整代码
library(ggplot2) library(cowplot) data <- data.frame( Year = c('2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024'), Class = c('Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta',"Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae"), Avg = c(0.733333,0,0,0,0,0,0,0,0, 17.8, 13.8, 35.2, 60.66667, 25, 81.53333, 82.26667, 38.2, 33.73333,0.2,0, 0, 0 , 0.2, 0,0.2, 0 , 0.2,1.3,0.6, 0.7,0,0,0,0,0,0,1.75,0.8,0.4,0,0.2 ,0,0,0,0.2,4.6, 6.066667 , 1.9, 0.666667, 1.3 , 0.4, 0.8, 1.533333 , 1.1,2.6 , 0.6,0, 0 ,0,0 ,0,0.2 ,0.2,11.9,3.133333, 4.933333 ,31.55556 ,24.13333, 60.2 , 62.8 ,33 ,4.7) ) data$Class <- factor(data$Class, levels = c("Chironomidae","Isopoda","Amphipoda","Bivalvia","Gastropda","Hirudinea","Oligochaeta", "Platyhelminthes")) p1 <- ggplot(data, aes(Avg, Year)) + geom_col(aes(fill = Class), width = 0.7, color = "grey30") + scale_fill_manual(values = c( "Platyhelminthes" = "#75150C","Oligochaeta"="#EF8634","Hirudinea"="#C0BE3D","Gastropda"= "#387D21","Bivalvia"="#258D93", "Amphipoda"="#5589D3", "Isopoda" = "#5549D1", "Chironomidae" = "#83350C" )) + # 补全遗漏的+号 scale_x_continuous(expand = expansion(add = c(1, 1))) + # 替换为数值型x轴 scale_y_discrete(expand = expansion(mult = c(0, 0.05))) + # 修正为离散型y轴 theme_classic(18) + theme( axis.line = element_line(color = "grey50"), axis.text.y = element_text(margin = margin(r = 10)), axis.text.x = element_text(margin = margin(t = 10)), axis.ticks.y = element_line(color = "grey50"), axis.ticks.x = element_line(color = "grey50"), axis.ticks.length = unit(7, "pt"), axis.title.y = element_text(margin = margin(r = 15)), legend.position = c(1.05, 0.7), # 调整图例位置实现内部放置 legend.justification = c(1, 0.7), legend.key.width = unit(30, "pt"), legend.key.height = unit(15, "pt"), legend.key.spacing.y = unit(5, "pt") ) + guides(fill = guide_legend(reverse = TRUE)) + labs( x = '', y = "Years", fill = NULL ) data2 <- data.frame( Year = c('2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024','2016','2017','2018','2019','2020','2021','2022','2023','2024'), Class = c('Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Platyhelminthes','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta','Oligochaeta',"Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Hirudinea","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Gastropda","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Bivalvia","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Amphipoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Isopoda","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae","Chironomidae"), Avg = c(0.6,0,0.2,0.333333 , 0.4 , 0 ,0.4, 0, 2.833333,47.06667 ,40.86667,142.3333 , 149.1111, 40.06667 ,94.66667 ,91.66667,38.2 , 82.4,0,0,0,0,0,0,0,0,0,1.866667, 0.6 ,0.2 ,0, 0.2, 0, 0.4, 0 ,0.2,2.2 ,2.9 ,8.133333,1.333333 , 2.1 ,1,2 , 2.4,1.766667,13.96667, 26.33333, 18.1 , 6.222222 , 5.2 ,6, 7.9,4.466667 ,16.9,8.266667 ,3.4, 5.1 ,1.833333, 1.7 , 0.2,1.566667 ,2.4 ,2.93334,2.5,1.4 ,2.933333,3.944444,3.233333 ,3.866667 ,3.733333 ,3.66666 ,2.266667) ) data2$Class <- factor(data2$Class, levels = c( "Chironomidae","Isopoda","Amphipoda","Bivalvia","Gastropda","Hirudinea","Oligochaeta", "Platyhelminthes")) p2 <- ggplot(data2, aes(Avg, Year)) + geom_col(aes(fill = Class), width = 0.7, color = "grey30") + scale_fill_manual(values = c( "Platyhelminthes" = "#75150C","Oligochaeta"="#EF8634","Hirudinea"="#C0BE3D","Gastropda"= "#387D21","Bivalvia"="#258D93", "Amphipoda"="#5589D3", "Isopoda" = "#5549D1", "Chironomidae" = "#83350C" )) + scale_x_continuous(expand = expansion(add = c(1, 1))) + # 替换为数值型x轴 scale_y_discrete(expand = expansion(mult = c(0, 0.06))) + # 修正为离散型y轴 theme_classic(18) + theme( axis.line = element_line(color = "grey50"), axis.text.y = element_text(margin = margin(r = 10)), axis.text.x = element_text(margin = margin(t = 10)), axis.ticks.y = element_line(color = "grey50"), axis.ticks.x = element_line(color = "grey50"), axis.ticks.length = unit(7, "pt"), axis.title.y = element_text(margin = margin(r = 15)), legend.position = "right", legend.key.width = unit(30, "pt"), legend.key.height = unit(15, "pt"), legend.key.spacing.y = unit(5, "pt") ) + guides(fill = guide_legend(reverse = TRUE)) + labs( x = 'Average / Ponar', y = "Years", fill = NULL ) p <- cowplot::plot_grid(p1, p2, nrow = 1, rel_widths = c(1, 1.5)) png(width = 14, height = 6, units = "in", res = 300); print(p); dev.off() # 添加print确保拼接图正确输出
额外说明
- 修正了轴类型与变量类型不匹配的问题,避免图表渲染异常。
- 在保存图片时添加
print(p),确保拼接后的组合图能正确输出。
内容的提问来源于stack exchange,提问作者Dylan Raney
相关产品推荐
相关产品推荐

