如何获得分组的barplot?位置“道奇”不起作用[关闭]

问题描述 投票:-2回答:1
我正在尝试创建一个分组的条形图,其中我的x轴显示月份,而我的y轴显示每月的平均检测次数。我将数据分为4个区域,并希望将其用作条形图的子组,其中每个月都有4条表示不同区域的条形图。现在,当我使用功能position = "dodge"时,我的条形图会堆叠区域。我还注意到颜色是渐变色,并且我不需要渐变色,但是当我尝试使用函数scale_fill_manual(values=c("red","blue", "green", "black"))时,会收到错误消息Error: Continuous value supplied to discrete scale

我已使用此代码对检测区域进行了平均并分配了特定区域:

#Getting total counts per month dets_per_month = true_dets %>% group_by(deploy_long, deploy_lat, month, yearcollected, animal_id) %>% summarize(total_dets = sum(count)) #apply Zone's for specific latitude values dets_per_month = dets_per_month %>% mutate(zone = if_else(deploy_lat <= 44.47833 & deploy_lat >= 44.16253, 1, if_else(deploy_lat <= 44.15687 & deploy_lat >= 43.67051, 2, if_else(deploy_lat <= 43.66313 & deploy_lat >= 43.25501, 3, 4) ))) #Average over month and Zone dets_per_month = dets_per_month %>% group_by(zone, deploy_long, deploy_lat, month, yearcollected, animal_id, total_dets) %>% summarise(ave = mean(total_dets))

而且我已经将此代码用于ggplot条形图:

##Barplot for 3 Years of total detections per month ggplot(dets_per_month, aes(x = dets_per_month$month, y = dets_per_month$ave)) + geom_bar(aes(fill = dets_per_month$zone), position = "dodge", stat = "identity") + xlab("Month") + ylab("Total Detections") + ylim(0, 1750) + facet_wrap(vars(yearcollected)) + facet_grid(yearcollected ~ ., scales="free") + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))

有人知道如何解决此问题吗?

我在下面添加了我的图表enter image description here

r ggplot2 graph
1个回答
0
投票
我弄清楚出了什么问题。我需要在区域列中使用as.factor
© www.soinside.com 2019 - 2024. All rights reserved.