Hi there,
EDIT: sorry, I forgot to add one piece of code xstar=0.85
EDIT2: Removed quotations from alpha = "0.5"
I am struggling a little bit with troubleshooting and figuring out how to resolve the issue I am getting when I am particularly trying to add a geom_ribbon
to my plot in order to shade between the area of a
and b
. My plot works fine without the geom_ribbon
code but whenever I include it I get the following error:
"Error: Aesthetics must be either length 1 or the same as the data (20): ymin, ymax"
library(ggplot2)
library(magrittr)
library(scales)
library(tidyr)
x1 = c(0.8355500, 0.9195025, 0.8718606)
x2 = c(0.7020640, 0.8643428, 0.8718606)
x3 = c(0.7201364, 0.8718606, 0.8718606)
xstar = 0.85
a = c(0.30000, 0.83555, 0.83555, 0.83555)
b = c(0.9500000, 0.9500000, 0.8718606, 0.8718606)
acc = seq(from = 1, to = (length(x1)), by = 1)
totdat = data.frame(cbind(acc,x1,x2,x3,b,a))
totdat[[1]][(length(a))] = NA
totdat[[2]][(length(a))] = NA
totdat[[3]][(length(a))] = NA
totdat[[4]][(length(a))] = NA
totdat
adjtotdat = pivot_longer(totdat, c(x1,x2,x3,b,a))
adjtotdat
p = ggplot(adjtotdat, aes(x = acc, y = value, color = name))
p + geom_line(size=1) + geom_point(size=2) +
geom_ribbon(adjtotdat=subset(acc, 1 <= acc & acc <= (length(x1))), aes(ymin=a,ymax=b), fill="grey", alpha="0.5")+
scale_y_continuous(breaks=seq(0,1,0.1)) +
scale_x_continuous(breaks=seq(1,(length(x1)),1)) +
geom_hline(aes(yintercept = xstar, linetype ="Ex Ante Court Eff. Effort"), col = "green", size = 1.2)+
scale_linetype_manual(name = "", values = c(2), guide=guide_legend(override.aes = list(color=c("green"))))+
scale_colour_manual(values = c("blue","red","purple","#006633","#336600"), limits = c("x1","x2","x3","b","a"), name="", labels = c("Driver 1", "Driver 2", "Driver 3", "Upper Threshold","Lower Threshold")) +
ggtitle("Evolution of Legal Threshold and Effort Levels", subtitle="One Incomplete Rule with No Cost Uncertainty") + xlab("Accident") + ylab("Effort Level/Legal Threshold") +
# , scale_fill_discrete(name = "", labels = c("Driver 1", "Driver 2", "Driver 3", "Upper Threshold","Lower Threshold"))
theme(panel.background = element_rect(fill = "white"),panel.grid.major = element_line(color = "gray"),panel.border = element_rect(colour = "black", fill = NA, size = 2.75)) +
theme(legend.key.size = unit(1,"cm"), legend.text = element_text(color="black",size = 15)) +
theme(plot.title = element_text(size = 30, margin= margin(t = 0, r = 0, b = 10, l = 0),hjust=0.5, face = "bold"), plot.subtitle = element_text(size = 20, margin= margin(t = 5, r = 0, b = 10, l = 0),hjust=0.5),plot.margin = unit(c(0.5,0.5,0.5,0.5),"cm"), axis.text.y.left = element_text(size = 15, color = "black", margin = margin(t=0,r=5,b=0,l=0)), axis.title.y.left = element_text(size = 20, margin = margin(t = 0, r = 20, b = 0, l = 0)),axis.text.x = element_text(size = 15, color = "black", margin = margin(t=5,r=0,b=0,l=0)), axis.title.x.bottom = element_text(size = 20,margin = margin(t = 18, r = 0, b = 0, l = 0)))
Any suggestions or recommendations are welcome, thank you.