R:ggplot 图例中未显示线型和颜色手册
R: linetype and color manual not showing in ggplot legend
我正在使用 ggplot
将以下数据(存储为 data.frame
对象)绘制为 ROC-AUC 图。我想以独特的线型和颜色显示每个数据,并将它们排列到图中的图例中,所以我引用了这个 thread,但我的代码(在下面)似乎不起作用。线型和颜色只是没有显示在图例上。如果有人能指出我的代码中的错误或建议替代方法来实现预期结果,我将不胜感激。
library(ggplot2)
df1 <- readRDS(url("https://www.dropbox.com/s/rtkyjs4af9h0qhx/df1.rds?dl=1"))
df2 <- readRDS(url("https://www.dropbox.com/s/r3qlvm36ybxqpcz/df2.rds?dl=1"))
df3 <- readRDS(url("https://www.dropbox.com/s/ebd8vu3l8iefzcz/df3.rds?dl=1"))
df4 <- readRDS(url("https://www.dropbox.com/s/cud2g0xijcc5cql/df4.rds?dl=1"))
df5 <- readRDS(url("https://www.dropbox.com/s/pzaznhozuxywnuc/df5.rds?dl=1"))
df6 <- readRDS(url("https://www.dropbox.com/s/n8ryyboznmxf1dq/df6.rds?dl=1"))
ggplot() +
geom_line(data = df1, aes(X3, X2, col = "Model 1: 76.9", linetype = "Model 1: 76.9"), size = 1.1, alpha=0.8) +
geom_line(data = df2, aes(X3, X2, col = "Model 2: 78.3", linetype = "Model 2: 78.3"), size = 1.1, alpha=0.8) +
geom_line(data = df3, aes(X3, X2, col = "Model 3: 77.2", linetype = "Model 3: 77.2"), size = 1.1, alpha=0.6) +
geom_line(data = df4, aes(X3, X2, col = "Model 4: 77.3", linetype = "Model 4: 77.3"), size = 1.1, alpha=0.6) +
geom_line(data = df5, aes(X3, X2, col = "Model 5: 78.5", linetype = "Model 5: 78.5"), size = 1.1, alpha=0.4) +
geom_line(data = df6, aes(X3, X2, col = "Model 6: 78.5", linetype = "Model 6: 78.5"), size = 1.1, alpha=0.4) +
scale_color_manual(name = " Model AUC",
values = c("Model 1: 76.9" = "#F8766D", "Model 2: 78.3" = "#F8766D", "Model 3: 77.2" = "#00BFC4", "Model 4: 77.3" = "#00BFC4", "Model 5: 78.5" = "#00BFC4", "Model 6: 78.5" = "#00BFC4")) +
scale_linetype_manual(name = " Model AUC", values = c("Model 1: 76.9" = "solid", "Model 2: 78.3" = "dotted", "Model 3: 77.2" = "solid", "Model 4: 77.3" = "dotted", "Model 5: 78.5" = "dashed", "Model 6: 78.5" = "dotdash")) +
labs(title="ROC AUC", x ="1 - Specificity (%)", y = "Sensitivity (%)") +
theme_bw() + scale_x_continuous(labels = c(0, 25, 50, 75, 100)) +
theme(plot.title = element_text(hjust = 0.5), legend.position = c(0.80, 0.35))
如果这仅发生在您的绘图面板中,请尝试 ggsave
例如。
在我的绘图面板中,它看起来和你的图片一样:
但是,将其保存在文件夹中后,我得到了颜色和线型:
我正在使用 ggplot
将以下数据(存储为 data.frame
对象)绘制为 ROC-AUC 图。我想以独特的线型和颜色显示每个数据,并将它们排列到图中的图例中,所以我引用了这个 thread,但我的代码(在下面)似乎不起作用。线型和颜色只是没有显示在图例上。如果有人能指出我的代码中的错误或建议替代方法来实现预期结果,我将不胜感激。
library(ggplot2)
df1 <- readRDS(url("https://www.dropbox.com/s/rtkyjs4af9h0qhx/df1.rds?dl=1"))
df2 <- readRDS(url("https://www.dropbox.com/s/r3qlvm36ybxqpcz/df2.rds?dl=1"))
df3 <- readRDS(url("https://www.dropbox.com/s/ebd8vu3l8iefzcz/df3.rds?dl=1"))
df4 <- readRDS(url("https://www.dropbox.com/s/cud2g0xijcc5cql/df4.rds?dl=1"))
df5 <- readRDS(url("https://www.dropbox.com/s/pzaznhozuxywnuc/df5.rds?dl=1"))
df6 <- readRDS(url("https://www.dropbox.com/s/n8ryyboznmxf1dq/df6.rds?dl=1"))
ggplot() +
geom_line(data = df1, aes(X3, X2, col = "Model 1: 76.9", linetype = "Model 1: 76.9"), size = 1.1, alpha=0.8) +
geom_line(data = df2, aes(X3, X2, col = "Model 2: 78.3", linetype = "Model 2: 78.3"), size = 1.1, alpha=0.8) +
geom_line(data = df3, aes(X3, X2, col = "Model 3: 77.2", linetype = "Model 3: 77.2"), size = 1.1, alpha=0.6) +
geom_line(data = df4, aes(X3, X2, col = "Model 4: 77.3", linetype = "Model 4: 77.3"), size = 1.1, alpha=0.6) +
geom_line(data = df5, aes(X3, X2, col = "Model 5: 78.5", linetype = "Model 5: 78.5"), size = 1.1, alpha=0.4) +
geom_line(data = df6, aes(X3, X2, col = "Model 6: 78.5", linetype = "Model 6: 78.5"), size = 1.1, alpha=0.4) +
scale_color_manual(name = " Model AUC",
values = c("Model 1: 76.9" = "#F8766D", "Model 2: 78.3" = "#F8766D", "Model 3: 77.2" = "#00BFC4", "Model 4: 77.3" = "#00BFC4", "Model 5: 78.5" = "#00BFC4", "Model 6: 78.5" = "#00BFC4")) +
scale_linetype_manual(name = " Model AUC", values = c("Model 1: 76.9" = "solid", "Model 2: 78.3" = "dotted", "Model 3: 77.2" = "solid", "Model 4: 77.3" = "dotted", "Model 5: 78.5" = "dashed", "Model 6: 78.5" = "dotdash")) +
labs(title="ROC AUC", x ="1 - Specificity (%)", y = "Sensitivity (%)") +
theme_bw() + scale_x_continuous(labels = c(0, 25, 50, 75, 100)) +
theme(plot.title = element_text(hjust = 0.5), legend.position = c(0.80, 0.35))
如果这仅发生在您的绘图面板中,请尝试 ggsave
例如。
在我的绘图面板中,它看起来和你的图片一样:
但是,将其保存在文件夹中后,我得到了颜色和线型: