英文:
Add different linetypes in ggplot2/ggroc in R
问题
我想在ggplot
中使用不同的线型,使用ggroc
- 但我无法使scale_linetype_manual
起作用。请参见下面的示例代码。
library(pROC)
library(ggplot2)
# 示例数据
# 完美
probs_1 <- seq(0, 1, length.out=10000)
roc_perfect <- roc(probs_1, probs_1)
# 随机
probs_1 <- rep(c(1, 0), 100)
probs_2 <- c(rep(c(0, 1), 50),
rep(c(1, 0), 50))
roc_random <- roc(probs_1, probs_2)
# AUC = 82
set.seed(144)
probs_82 <- seq(0, 1, length.out=10000)
truth_82 <- runif(10000)^1.21 < probs
roc_82 <- roc(truth_82, probs_82)
# 绘制ROC曲线
pROC::ggroc(list(roc_perfect, roc_82, roc_random),
legacy.axes = FALSE,
# linetype=1,
linewidth = 0.4) +
# 无效
scale_linetype_manual(values=c(2, 1, 2)) +
scale_colour_manual("",
labels = c("Perfect",
"Model",
"Random classification"),
values = c("gray",
"green",
"gray")) +
theme_classic()
英文:
I want to use different linetypes in ggplot
using ggroc
– but I can't get scale_linetype_manual
to work. Please see example code below.
library(pROC)
library(ggplot2)
# Example data
# Perfect
probs_1 <- seq(0, 1, length.out=10000)
roc_perfect <- roc(probs_1, probs_1)
# Random
probs_1 <- rep(c(1, 0), 100)
probs_2 <- c(rep(c(0, 1), 50),
rep(c(1, 0), 50))
roc_random <- roc(probs_1, probs_2)
# AUC = 82
set.seed(144)
probs_82 <- seq(0, 1, length.out=10000)
truth_82 <- runif(10000)^1.21 < probs
roc_82 <- roc(truth_82, probs_82)
# Plot ROC curves
pROC::ggroc(list(roc_perfect, roc_82, roc_random),
legacy.axes = FALSE,
# linetype=1,
linewidth = 0.4) +
# Does not work
scale_linetype_manual(values=c(2, 1, 2)) +
scale_colour_manual("",
labels = c("Perfect",
"Model",
"Random classification"),
values = c("gray",
"green",
"gray")) +
theme_classic()
答案1
得分: 2
根据文档,您需要指定在函数中使用哪些美学元素。在您的代码中,它应该如下所示:
pROC::ggroc(list(roc_perfect, roc_82, roc_random),
aes = c("colour", "linetype"),
legacy.axes = FALSE,
linewidth = 0.4) +
scale_linetype_manual("",
values=c(2, 1, 2),
labels = c("Perfect", "Model",
"Random classification")) +
scale_colour_manual("",
labels = c("Perfect", "Model",
"Random classification"),
values = c("gray",
"green",
"gray")) +
theme_classic()
结果,我得到了以下的图表。
英文:
According to the documentation, you need to indicate which aesthetics are used in the function. Over your code, it should look like this:
pROC::ggroc(list(roc_perfect, roc_82, roc_random),
aes = c("colour",
"linetype"),
legacy.axes = FALSE,
linewidth = 0.4) +
scale_linetype_manual("",
values=c(2, 1, 2),
labels = c("Perfect",
"Model",
"Random classification")) +
scale_colour_manual("",
labels = c("Perfect",
"Model",
"Random classification"),
values = c("gray",
"green",
"gray")) +
theme_classic()
As a result, I got the following plot.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论