使线上的线条在背景中透明

时间:2019-05-03 20:35:45

标签: r ggplot2

我使用ggplot成功创建了一个线图。但是,该图包含41条单独的线,因此您可以想象它看起来有多混乱。也就是说,我想做的是以某种方式手动选择这些线中的37条以在背景中显示更多(即使它们更透明),而理想的其余四(4)条将在外观上更厚而不透明。这可能吗?另外,对于剩下的四(4)个地块,是否可以仅包括图例中的那些地块?

这是我目前拥有的:

#To create large dataframe:

#1pctCO2:

CanESM1pctCO2<-data.frame(get, Column4) 
IPSLLR1pctCO2<-data.frame(IPSL, Column5)
IPSLMR1pctCO2<-data.frame(IPSLMR, Column6)
IPSLLRB1pctCO2<-data.frame(IPSL5, Column7)
MIROC1pctCO2<-data.frame(MIROC, Column8)
HadGEM1PctCO2<-data.frame(subsetprime, Column9) 
MPILR1pctCO2<-data.frame(MPI, Column10)
MPIMR1pctCO2<-data.frame(MPI5, Column11)
GFDLG1pctCO2<-data.frame(GFDL, Column12)
GFDLM1pctCO2<-data.frame(GFDL5, Column13)


#RCP4.5:

CanESMRCP4.5<-data.frame(get2.teratons, Column14) 
IPSLLRRCP4.5<-data.frame(get5.teratons, Column15)
IPSLMRRCP4.5<-data.frame(get8.teratons, Column16)
IPSLLRBRCP4.5<-data.frame(get11.teratons, Column17)
MIROCRCP4.5<-data.frame(get14.teratons, Column18)
HadGEMRCP4.5<-data.frame(get17.teratons, Column19)
MPILRRCP4.5<-data.frame(get20.teratons, Column20)
GFDLGRCP4.5<-data.frame(get26.teratons, Column21)
GFDLMRCP4.5<-data.frame(get29.teratons, Column22)


#RCP8.5:

CanESMRCP8.5<-data.frame(get3.teratons, Column23) 
IPSLLRRCP8.5<-data.frame(get6.teratons, Column24)
IPSLMRRCP8.5<-data.frame(get9.teratons, Column25)
IPSLLRBRCP8.5<-data.frame(get12.teratons, Column26)
MIROCRCP8.5<-data.frame(get15.teratons, Column27)
HadGEMRCP8.5<-data.frame(get18.teratons, Column28)
MPILRRCP8.5<-data.frame(get21.teratons, Column29)
GFDLGRCP8.5<-data.frame(get27.teratons, Column30)
GFDLMRCP8.5<-data.frame(get30.teratons, Column31)


#Historical:

CanESMHist<-data.frame(get4.teratons, Column32) 
IPSLLRHist<-data.frame(get7.teratons, Column33)
IPSLMRHist<-data.frame(get10.teratons, Column34)
IPSLLRBHist<-data.frame(get13.teratons, Column35)
MIROCHist<-data.frame(get16.teratons, Column36)
HadGEMHist<-data.frame(subsetprime1, Column37) 
MPILRHist<-data.frame(get22.teratons, Column38)
GFDLGHist<-data.frame(get28.teratons, Column39)
GFDLMHist<-data.frame(get31.teratons, Column40)


newestdataULTRA <- melt(list(onepctCO2mean=onepctCO2mean, 
CanESM1pctCO2=CanESM1pctCO2, IPSLLR1pctCO2=IPSLLR1pctCO2, 
IPSLMR1pctCO2=IPSLMR1pctCO2, IPSLLRB1pctCO2=IPSLLRB1pctCO2, 
MIROC1pctCO2=MIROC1pctCO2, HadGEM1PctCO2=HadGEM1PctCO2, 
MPILR1pctCO2=MPILR1pctCO2, MPIMR1pctCO2=MPIMR1pctCO2, 
GFDLG1pctCO2=GFDLG1pctCO2, GFDLM1pctCO2=GFDLM1pctCO2, 
RCP4.5mean=RCP4.5mean, CanESMRCP4.5=CanESMRCP4.5, 
IPSLLRRCP4.5=IPSLLRRCP4.5, IPSLMRRCP4.5=IPSLMRRCP4.5, 
IPSLLRBRCP4.5=IPSLLRBRCP4.5, MIROCRCP4.5=MIROCRCP4.5, 
HadGEMRCP4.5=HadGEMRCP4.5, MPILRRCP4.5=MPILRRCP4.5, 
GFDLGRCP4.5=GFDLGRCP4.5, GFDLMRCP4.5=GFDLMRCP4.5, RCP8.5mean=RCP8.5mean, 
CanESMRCP8.5=CanESMRCP8.5, IPSLLRRCP8.5=IPSLLRRCP8.5, 
IPSLMRRCP8.5=IPSLMRRCP8.5, IPSLLRBRCP8.5=IPSLLRBRCP8.5, 
MIROCRCP8.5=MIROCRCP8.5, HadGEMRCP8.5=HadGEMRCP8.5, 
MPILRRCP8.5=MPILRRCP8.5, GFDLGRCP8.5=GFDLGRCP8.5, GFDLMRCP8.5=GFDLMRCP8.5, 
Historicalmean=Historicalmean, CanESMHist=CanESMHist, 
IPSLLRHist=IPSLLRHist, IPSLMRHist=IPSLMRHist, IPSLLRBHist=IPSLLRBHist, 
MIROCHist=MIROCHist, HadGEMHist=HadGEMHist, MPILRHist=MPILRHist, 
GFDLGHist=GFDLGHist, GFDLMHist=GFDLMHist), id.vars="x")

cols <- c("green", "black", "blue", "red", "green", "black", "blue", 
"red", "green", "black", "blue", "red", "green", "black", "blue", "red",  
"black", "green", "green", "black", "blue", "red", "black", "blue", "red",  
"green", "black", "blue", "red", "green", "black", "blue", "red", "green", 
"black", "blue", "red", "green", "green", "blue", "red")

ggplot(newestdataULTRA, aes(x,value,colour=L1)) + geom_line() +   
scale_color_manual(values=cols) + theme(panel.background = 
element_blank(), axis.line = element_line()) + ggtitle("Global model mean 
one-day maximum precipitation for various scenarios") + xlab("Cumulative 
emissions (TtC)") + ylab("Precipitation (mm/day)")

使用此方法,这41行会自动放置在图例中,但我只希望其中四行包含在图例中。有可能吗?

谢谢,对此将提供任何帮助!

1 个答案:

答案 0 :(得分:4)

Android17,这种类型的操作具体取决于您的目标。这是一个循序渐进的示例,在此示例中,我试图证明美洲国家几年中的出生时预期寿命。在这个玩具示例中,我使用了gapminder包。

随着视觉特性的变化,请参见绘制图表,直到尝试实现所需的目标为止。

library(ggplot2)
library(gapminder)
data(gapminder)
# View(gapminder)
ggplot2::ggplot(gapminder) +
  geom_line(aes(x = year, y = lifeExp, 
                group = country, color = continent)) +
  theme_classic()

enter image description here

请注意,它可能按照我想要的方式进行着色。

ggplot2::ggplot(gapminder) +
  geom_line(aes(x = year, y = lifeExp, 
                group = country, color = continent)) +
  scale_color_manual(values = c("darkorchid", "red2",
                                "cyan3", "gold3",
                                "chartreuse")) +
  theme_classic()

enter image description here

ggplot2::ggplot(gapminder) +
  geom_line(aes(x = year, y = lifeExp, 
                group = country, color = continent)) +
  scale_color_manual(values = c("black", "red2",
                                "black", "black",
                                "black")) +
  theme_classic()

enter image description here

ggplot2::ggplot(gapminder) +
  geom_line(aes(x = year, y = lifeExp, 
                group = country, color = continent,
                alpha = continent),
            size = 1) +
  scale_color_manual(values = c("black", "red2",
                                "black", "black",
                                "black")) +
  scale_alpha_manual(values = c(0.1, 0.9, rep(0.1, 3))) +
  theme_classic()

enter image description here