I want to add the temperature and water annual data as lines behind the bar graphs for each 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()
I want something that looks like this..
I tried it with the secondary axis also, but I had very little luck. Here is the code and graph
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")
)



