3

我正在尝试使用我自己的数据集重新创建附加的图形。附加的 R 脚本在那里有 90%,但是我似乎找不到根据该图对线条进行样式化的方法。具体来说,我需要以实线开始、以点结束并以虚线继续的线特征。我更喜欢在 R 中使用 ggplot2。

我怎样才能使我的图中的每条线都是实心的,直到x = 5最后以一个点结束,最后继续作为虚线?

在此处输入图像描述

# 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"))
4

1 回答 1

7

这是一个很长的解决方案。我使用了变量的名称,因为它们在数据框中mydata

想法是单独绘制是将线段绘制到值5,然后在值5之后绘制。因此使用subset()函数。对于两条线段,都包含值 5 以确保连接线段。要添加点,请使用 geom_point() 和子集数据,仅用于值 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)

在此处输入图像描述

于 2013-05-28T17:50:37.650 回答