Android17
Android17

Reputation: 105

Making lines on line plot transparent in background

I successfully created a line plot using ggplot. However, the plot contains 41 separate lines, so you can imagine how cluttered it appears. That said, what I would like to do is somehow manually select 37 of these lines to show more in the background (i.e. making them more transparent), while the remaining four (4) would ideally be thicker in appearance and not transparent. Would this be possible? Also, for those four (4) remaining plots, would it be possible to include only those in the legend?

This is what I currently have:

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

Using this, the 41 lines are automatically placed in the legend, but I only want four of those lines to be included in the legend. Is that possible?

Thank you, and any help with this would be extremely appreciated!

Upvotes: 1

Views: 1174

Answers (1)

bbiasi
bbiasi

Reputation: 1599

Android17, this type of manipulation depends specifically on your goal. Here is a step-by-step example where I try to evidence life expectancy at birth in countries in the Americas for some years. In this toy example I used the gapminder package.

See plot a plot as the visual characteristics change until you try to achieve something you want.

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

Note that it is possíver put colors the way I want.

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

Upvotes: 4

Related Questions