'Tried scale_linetype_manual to add legend but not successful

Following the thread from Tried p + scale_fill_discrete(name = "New Legend Title") but legend title still not changing, I now only want to generate line plot with legend based on the line type but has been unsuccessful.

Dataframe as below from my previous thread for reproduciblitiy:

#For lowW dataset:

SurfaceCoverage <- c(0.04,0.08,0.1,0.12,0.15,0.2,0.04,0.08,0.1,0.12,0.15,0.2)
TotalSurfaceEnergy <- c(139.31449,105.17776,105.38411,99.27608,92.29064,91.55114,84.44251,78.40453,74.66656,73.33242,72.42429,77.08666)
sample <- c(1,1,1,1,1,1,2,2,2,2,2,2)

lowW <- data.frame(sample,SurfaceCoverage,TotalSurfaceEnergy)

lowW$sample <- sub("^", "Wettable", lowW$sample)
lowW$RelativeHumidity <- "Low relative humidity"; lowW$group <- "Wettable"
lowW$sR <- paste(lowW$sample,lowW$RelativeHumidity)

dflowW <- data.frame(
  "y"=c(lowW$TotalSurfaceEnergy),
  "x"=c(lowW$SurfaceCoverage),
  "b"=c(lowW$sample),
  "sR"=c(lowW$sR)
)

mixed.lme <- lme(y~log(x),random=~1|b,data=dflowW)
pred.mmlowW <- ggpredict(mixed.lme, terms = c("x"))

#For highW dataset: 
  
SurfaceCoverage <- c(0.02,0.04,0.06,0.08,0.1,0.12,0.02,0.04,0.06,0.08,0.1,0.12)
TotalSurfaceEnergy <- c(66.79554,61.46907,57.56855,54.00953,54.28361,55.15855,50.72314,48.55892,47.41811,43.70885,42.13757,40.55924)
sample <- c(1,1,1,1,1,1,2,2,2,2,2,2)

highW <- data.frame(sample,SurfaceCoverage,TotalSurfaceEnergy)

highW$sample <- sub("^", "Wettable", highW$sample)
highW$RelativeHumidity <- "High relative humidity"; highW$group <- "Wettable"
highW$sR <- paste(highW$sample,highW$RelativeHumidity)

dfhighW <- data.frame(
  "y"=c(highW$TotalSurfaceEnergy),
  "x"=c(highW$SurfaceCoverage),
  "b"=c(highW$sample),
  "sR"=c(highW$sR)
)

mixed.lme <- lme(y~log(x),random=~1|b,data=dfhighW)
pred.mmhighW <- ggpredict(mixed.lme, terms = c("x")) 

For generating the line plot:

p <- ggplot() +
  #lowa plot
  geom_line(data=pred.mmlowW, aes(x = x, y = predicted)) +          
       
 
  #higha plot
  geom_line(data=pred.mmhighW, aes(x = x, y = predicted)) +          
 
  
  xlim(0.01,0.2) + 
  ylim(30,150) +
  labs(title = "") + 
  ylab(bquote('Total Surface Energy ' (mJ/m^2))) +
  xlab(bquote('Surface Coverage ' (n/n[m]) )) +
  theme_minimal()

print(p)

What I have tried on adding the legend but has been unsuccessful:

p1 <- p + scale_linetype_manual(name = "New Legend Title", 
                                values = c("solid","dashed"),
                                breaks=c("pred.mmlowW",
                                        "pred.mmhighW", 
                                        ),
                                labels=c("wettable soil, 0% relative humidity",
                                       "water-repellent soil, 0% relative humidity"
                                       ))
print(p1)

enter image description here

Can someone help me with the syntax? Why is it not generating the legend for me?



Solution 1:[1]

To get a legend you have to map on an aesthetic, i.e. in your case you have to map on linetype:

library(nlme)
library(ggeffects)
library(ggplot2)

library(ggplot2)
ggplot() +
  geom_line(data = pred.mmlowW, aes(x = x, y = predicted, linetype = "pred.mmlowW")) +
  geom_line(data = pred.mmhighW, aes(x = x, y = predicted, linetype = "pred.mmhighW")) +
  xlim(0.01, 0.2) +
  ylim(30, 150) +
  labs(title = "") +
  ylab(bquote("Total Surface Energy "(mJ / m^2))) +
  xlab(bquote("Surface Coverage "(n / n[m]))) +
  theme_minimal() +
  scale_linetype_manual(
    name = "New Legend Title",
    values = c("solid", "dashed"),
    breaks = c(
      "pred.mmlowW",
      "pred.mmhighW"
    ),
    labels = c(
      "wettable soil, 0% relative humidity",
      "water-repellent soil, 0% relative humidity"
    )
  )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stefan