已尝试 scale_linetype_manual 添加图例但未成功
Tried scale_linetype_manual to add legend but not successful
根据Tried p + scale_fill_discrete(name = "New Legend Title") but legend title still not changing的帖子,我现在只想根据线型生成带图例的线图,但一直没有成功。
Dataframe 如下来自我之前的可重现线程:
#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"))
用于生成线图:
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)
我尝试添加图例但没有成功:
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)
谁能帮我解决语法问题?为什么它没有为我生成图例?
要获得图例,您必须在美学上进行映射,即在您的情况下,您必须在 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"
)
)
根据Tried p + scale_fill_discrete(name = "New Legend Title") but legend title still not changing的帖子,我现在只想根据线型生成带图例的线图,但一直没有成功。
Dataframe 如下来自我之前的可重现线程:
#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"))
用于生成线图:
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)
我尝试添加图例但没有成功:
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)
谁能帮我解决语法问题?为什么它没有为我生成图例?
要获得图例,您必须在美学上进行映射,即在您的情况下,您必须在 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"
)
)