如何将非生物变量(温度、水等)作为线条添加到 ggplot2 R 中的条形图中?
How to add abiotic variables (temperature, water etc) as lines to bar graphs in ggplot2 R?
我想在每个 facet_grid.
的条形图后面添加温度和水年度数据作为线条
sp.abd <- sample(1:48, replace = TRUE)
sp <- rep(c("A","B","temp","water"), each = 96)
plot <- as.factor(as.numeric(rep(c(1,2), time = 48)))
month <- as.factor(as.numeric(rep(c(1:12), time = 8)))
df <- data.frame(month, sp, sp.abd, plot)
ggplot(df, aes(x = month, y=sp.abd, fill = sp)) +
geom_bar(stat = "identity") +
facet_grid(plot~sp) + theme_classic()
我想要这样的东西..
我也用副轴试过,但运气很差。这是代码和图表
sp.abd <- sample(1:48, replace = TRUE)
sp <- rep(c("A","B"), each = 24)
plot <- as.factor(as.numeric(rep(c(1,2), time = 24)))
month <- as.factor(as.numeric(rep(c(1:12), time = 4)))
temp <- rnorm(48, 15, 5)
water <- rnorm(48, 15, 5)
df <- data.frame(month, sp, sp.abd, temp, water, plot)
coeff <- 1
ggplot(df, aes(x = month)) +
facet_grid(plot~sp) + theme_classic() +
geom_bar( aes(y=sp.abd), stat = "identity", size=2) +
geom_line( aes(y=temp), size=2, color="Red") +
geom_line( aes(y=water), size=2, color="Blue") +
scale_y_continuous(
# Features of the first axis
name = "Temperature (Celsius °)/ Moisture",
# Add a second axis and specify its features
sec.axis = sec_axis(~.*coeff, name="sp.abd")
)
这套适合吗?
library(tidyverse)
set.seed(123)
sp.abd <- sample(1:48, replace = TRUE)
sp <- rep(c("A","B"), each = 24)
plot <- as.factor(as.numeric(rep(c(1,2), time = 24)))
month <- as.factor(as.numeric(rep(c(1:12), time = 4)))
temp <- rnorm(48, 15, 5)
water <- rnorm(48, 15, 5)
df <- data.frame(month, sp, sp.abd, temp, water, plot)
df %>%
pivot_longer(-c(month, sp, sp.abd, plot)) %>%
ggplot() +
geom_bar(aes(x = month, y=sp.abd, fill = sp),
stat = "identity") +
geom_smooth(aes(x = month, y = value + 125,
group = name, color = name),
formula = "y ~ x", method = "loess",
se = FALSE) +
geom_smooth(aes(x = month, y = value + 125,
group = name, color = name),
formula = "y ~ x", method = "loess",
se = FALSE) +
facet_grid(plot~sp) +
scale_color_brewer(palette = "Set1") +
theme_classic()
我想在每个 facet_grid.
的条形图后面添加温度和水年度数据作为线条sp.abd <- sample(1:48, replace = TRUE)
sp <- rep(c("A","B","temp","water"), each = 96)
plot <- as.factor(as.numeric(rep(c(1,2), time = 48)))
month <- as.factor(as.numeric(rep(c(1:12), time = 8)))
df <- data.frame(month, sp, sp.abd, plot)
ggplot(df, aes(x = month, y=sp.abd, fill = sp)) +
geom_bar(stat = "identity") +
facet_grid(plot~sp) + theme_classic()
我想要这样的东西..
我也用副轴试过,但运气很差。这是代码和图表
sp.abd <- sample(1:48, replace = TRUE)
sp <- rep(c("A","B"), each = 24)
plot <- as.factor(as.numeric(rep(c(1,2), time = 24)))
month <- as.factor(as.numeric(rep(c(1:12), time = 4)))
temp <- rnorm(48, 15, 5)
water <- rnorm(48, 15, 5)
df <- data.frame(month, sp, sp.abd, temp, water, plot)
coeff <- 1
ggplot(df, aes(x = month)) +
facet_grid(plot~sp) + theme_classic() +
geom_bar( aes(y=sp.abd), stat = "identity", size=2) +
geom_line( aes(y=temp), size=2, color="Red") +
geom_line( aes(y=water), size=2, color="Blue") +
scale_y_continuous(
# Features of the first axis
name = "Temperature (Celsius °)/ Moisture",
# Add a second axis and specify its features
sec.axis = sec_axis(~.*coeff, name="sp.abd")
)
这套适合吗?
library(tidyverse)
set.seed(123)
sp.abd <- sample(1:48, replace = TRUE)
sp <- rep(c("A","B"), each = 24)
plot <- as.factor(as.numeric(rep(c(1,2), time = 24)))
month <- as.factor(as.numeric(rep(c(1:12), time = 4)))
temp <- rnorm(48, 15, 5)
water <- rnorm(48, 15, 5)
df <- data.frame(month, sp, sp.abd, temp, water, plot)
df %>%
pivot_longer(-c(month, sp, sp.abd, plot)) %>%
ggplot() +
geom_bar(aes(x = month, y=sp.abd, fill = sp),
stat = "identity") +
geom_smooth(aes(x = month, y = value + 125,
group = name, color = name),
formula = "y ~ x", method = "loess",
se = FALSE) +
geom_smooth(aes(x = month, y = value + 125,
group = name, color = name),
formula = "y ~ x", method = "loess",
se = FALSE) +
facet_grid(plot~sp) +
scale_color_brewer(palette = "Set1") +
theme_classic()