我有以下代碼來創建附加圖,基本上我想展示 5 月至 9 月的豐度。但五月、八月和九月沒有豐收。即使
我的資料框中沒有這些月份的資料,有沒有辦法可以將這些月份包含在我的情節中?
coef <- 625
HerringJCO <- JCOHattacksnoduplicates %>%
mutate(month = as.factor(month),
herringmean = herringmean/coef) %>%
pivot_longer(cols = c("herringmean","jcoaverage"), names_to = "species",
values_to = "mean") %>%
ggplot(aes(x = month, group = species))
geom_bar(aes(y = mean, fill = species), stat = "identity", position =
"dodge")
geom_line(aes(y = attacksum, group = 1))
geom_point(aes(y = attacksum))
geom_line(aes(y = passivesum, group = 1), color = "green")
geom_point(aes(y = passivesum), color = "green")
scale_y_continuous(expand = c(0,0), limits = c(0,35), sec.axis =
sec_axis(~.*coef, name = "Average Herring Abundance"))
ylab("Average Salmon Abundance and Sum of attacks")
xlab("")
theme_classic()
theme(axis.text.x = element_text(size = "14"))
scale_x_discrete(labels=c("4"="April", "5" = "May", "6" = "Jun", "7" =
"Jul", [![enter image description here][1]][1]"8" = "Aug", "9" = "Sept", "10" = "Oct"))
HerringJCO
uj5u.com熱心網友回復:
您始終可以將這些月份添加到資料中,但使您聚合的值等于 0,因此它會顯示在您的圖表上,值為 0。
我創建了一些假資料并留下了線條和點。但是,如果您只是添加月份,但沒有為您存盤的內容賦予它們任何價值mean,它將顯示沒有條形的月份。
library(tidyverse)
hjo <- data.frame(month = rep(5:9, 2) %>% as.factor(),
species = rep(c("herr", "jcoa"), 5),
values = c(0,5,8,0,0,0,4,5,0,0))
ggplot(hjo, aes(x = month, group = species, fill = species))
geom_col(aes(y = values), position = "dodge")
scale_y_continuous(expand = c(0,0), limits = c(0,35), sec.axis =
sec_axis(~.*coef, name = "Average Herring Abundance"))
ylab("Average Salmon Abundance and Sum of attacks")
xlab("")
theme_classic()
theme(axis.text.x = element_text(size = "14"))
scale_x_discrete(labels = month.abb[5:9])
看看這個:

但是,我確實想指出,@r2evans 提到的對于一種極其靈活的語言來說非常重要。查看您使用的資料的結構是快速獲得出色答案的最佳方式。
有很多關于如何實作這一點的資源,比如這個:制作 R 可重現的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/419380.html
標籤:
