Reputation: 1823
I'd like in my pred_avg
dataset to create a rule (pred_avg$canopycoverSDmin < pred_avg$covermin
) for color changes using fill
in geom_ribbon
using ggplot2
, but despite my variable pred_avg$groups
in fill = groups
is a factor
doesn't work and I have always Error: Aesthetics can not vary with a ribbon
as output.
In my example:
library(ggplot2)
#Open data set
pred_avg<- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/cc_mean_CI.csv")
str(pred_avg)
#'data.frame': 40323 obs. of 9 variables:
# $ X : int 1 2 3 4 5 6 7 8 9 10 ...
# $ DATE : chr "2021-06-04" "2021-06-04" "2021-06-04" "2021-06-04" ...
# $ canopycover : num 47 47 47 47 47 ...
# $ se.cc : num 1.6 1.57 1.54 1.54 1.54 ...
# $ n.cc : int 3073 3073 3073 3073 3073 3073 3073 3073 3073 3073 ...
# $ canopycoverSDmax: num 50.2 50.1 50 50 50 ...
# $ canopycoverSDmin: num 43.9 44 44 44 44 ...
# $ covermin : num 43.1 43.1 43.1 43.1 43.1 ...
# $ covermax : num 85.2 85.2 85.2 85.2 85.2 ...
# Create the variable (`groups`) for colours changes
pred_avg$groups <- ifelse(pred_avg$canopycoverSDmin < pred_avg$covermin, "A", "H")
pred_avg$groups <-as.factor(pred_avg$groups)
# Create the plot
ggplot(pred_avg, aes(x = DATE, y = canopycover, group = 1), fill=factor(groups)) +
geom_point () +
geom_line() +
geom_ribbon(data=pred_avg, aes(ymin = canopycoverSDmin, ymax = canopycoverSDmax, fill = groups), alpha = 0.1) +
scale_fill_manual(values=c("red", "green"), name="fill") +
geom_ribbon(aes(ymin = covermin, ymax = covermax), alpha = 0.1) +
scale_x_date(breaks = "1 week") +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
axis.text=element_text(size=10),
axis.title=element_text(size=12),
plot.title = element_text(size = 14, face = "bold"),
legend.title = element_blank(),
axis.text.x = element_text(angle = 90, hjust = 1),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
Error: Aesthetics can not vary with a ribbon
Run `rlang::last_error()` to see where the error occurred.
Please, any help with it?
Upvotes: 0
Views: 169
Reputation: 146224
When you specify something in the original ggplot()
it will be inherited by subsequent layers. Don't put group = 1
up there globally. If you need it for the line
layer, put group = 1
just in geom_line
. Similarly, don't put fill=factor(groups)
in the base ggplot()
call because you don't want it being used in the point
layer, the second ribbon
layer, etc. You only need it in the first ribbon
layer, so only put it there. (And you need to put it inside aes()
since it references a column name!)
pred_avg$DATE = as.Date(pred_avg$DATE)
# Create the plot
ggplot(pred_avg, aes(x = DATE, y = canopycover)) +
geom_point () +
geom_line(group = 1) +
geom_ribbon(
data = pred_avg,
aes(
ymin = canopycoverSDmin,
ymax = canopycoverSDmax,
fill = groups),
alpha = 0.1
) +
scale_fill_manual(values = c("red", "green"), name = "fill") +
geom_ribbon(aes(ymin = covermin, ymax = covermax), alpha = 0.1) +
scale_x_date(breaks = "1 week") +
theme_bw() + theme(
panel.border = element_blank(),
panel.grid.major = element_blank(),
axis.text = element_text(size = 10),
axis.title = element_text(size = 12),
plot.title = element_text(size = 14, face = "bold"),
legend.title = element_blank(),
axis.text.x = element_text(angle = 90, hjust = 1),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")
)
Upvotes: 2