Reputation: 8480
I am trying to recreate the attached graphic with my own dataset. The attached R script is 90% there, however I cannot seem to find a way to stylize the line according to the figure. Specifically, I need the line features that start out solid, end with a point and continue as a dashed line. I prefer to use ggplot2 in R.
How can I make each of the lines in my figure solid until x = 5
then end with a point and finally continue as a dashed line?
# Sample Dataset
Samples = c(1,2,3,4,5,6,7,8,9,10)
SestYB = c(2.1,3.89,5.42,6.73,7.87,8.86,9.75,10.56,11.3,12)
CILowYB = c(1.03,2.01,2.92,3.76,4.52,5.21,5.82,6.37,6.86,7.3)
CIUpYB = c(3.17,5.77,7.91,9.7,11.21,12.51,13.68,14.74,15.74,16.7)
SestEH = c(1.3,2.29,3.06,3.68,4.19,4.63,5.03,5.38,5.7,6)
CILowEH = c(0.34,0.73,1.13,1.5,1.84,2.13,2.39,2.61,2.79,2.95)
CIUpEH = c(2.26,3.85,4.98,5.85,6.54,7.14,7.66,8.15,8.61,9.05)
data.frame(Samples,SestYB,CILowYB,CIUpYB,SestEH,CILowEH,CIUpEH)
require(ggplot2)
mydata = data.frame(Samples,SestYB,CILowYB,CIUpYB,SestEH,CILowEH,CIUpEH)
# Variables
EH = mydata$SestEH
YB = mydata$SestYB
yb_lower = mydata$CILowYB
yb_upper = mydata$CIUpYB
eh_lower = mydata$CILowEH
eh_upper = mydata$CIUpEH
# Plot data
ggplot(mydata, aes(Samples)) +
xlab("Samples") +
ylab("Species") +
geom_line(aes(y=YB),
colour="blue", size = 1.25) +
geom_line(aes(y=EH),
colour="red", size = 1.25) +
geom_ribbon(aes(ymin=yb_lower, ymax=yb_upper),
fill = "blue",
alpha=0.2) +
geom_ribbon(aes(ymin=eh_lower, ymax=eh_upper),
fill = "red",
alpha=0.2) +
theme(panel.background = element_blank()) +
theme(panel.grid = element_blank()) +
theme(axis.line = element_line(size = 1, colour = "dark gray", linetype = "solid"))
Upvotes: 5
Views: 2774
Reputation: 98529
Here is one very long solution. I used names of variables as they are in data frame mydata
.
Idea is to plot separately is to plot line segment till value 5 and then after value 5. For this reason used subset()
function. For both line segment value 5 is included to ensure that segments are joined. To add points use geom_point() and subset data just for value 5.
ggplot() +
geom_line(data=subset(mydata,Samples<=5),aes(Samples,y=SestYB),
colour="blue", size = 1.25,linetype="solid") +
geom_line(data=subset(mydata,Samples>=5),aes(Samples,y=SestYB),
colour="blue", size = 1.25,linetype="dashed") +
geom_line(data=subset(mydata,Samples<=5),aes(Samples,y=SestEH),
colour="red", size = 1.25,linetype="solid") +
geom_line(data=subset(mydata,Samples>=5),aes(Samples,y=SestEH),
colour="red", size = 1.25,linetype="dashed")+
geom_ribbon(data=mydata,aes(Samples,ymin=CILowYB, ymax=CIUpYB),
fill = "blue",
alpha=0.2) +
geom_ribbon(data=mydata,aes(Samples,ymin=CILowEH, ymax=CIUpEH),
fill = "red",
alpha=0.2)+
geom_point(data=subset(mydata,Samples==5),aes(Samples,SestYB),size=5)+
geom_point(data=subset(mydata,Samples==5),aes(Samples,SestEH),size=5)
Upvotes: 9