在R中的ggplot2/ggroc中添加不同的线型。

huangapple go评论64阅读模式
英文:

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 &lt;- seq(0, 1, length.out=10000)
roc_perfect &lt;- roc(probs_1, probs_1)

# Random 
probs_1 &lt;- rep(c(1, 0), 100)
probs_2 &lt;- c(rep(c(0, 1), 50), 
             rep(c(1, 0), 50))
roc_random &lt;- roc(probs_1, probs_2)

# AUC = 82
set.seed(144)
probs_82 &lt;- seq(0, 1, length.out=10000)
truth_82 &lt;- runif(10000)^1.21 &lt; probs
roc_82 &lt;- 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(&quot;&quot;, 
                      labels = c(&quot;Perfect&quot;, 
                                 &quot;Model&quot;,
                                 &quot;Random classification&quot;), 
                      values = c(&quot;gray&quot;,
                                 &quot;green&quot;, 
                                 &quot;gray&quot;)) +
  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()

结果,我得到了以下的图表。

在R中的ggplot2/ggroc中添加不同的线型。

英文:

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(&quot;colour&quot;, 
                    &quot;linetype&quot;),
            legacy.axes = FALSE,
            linewidth = 0.4) +
  scale_linetype_manual(&quot;&quot;,
                        values=c(2, 1, 2),
                        labels = c(&quot;Perfect&quot;, 
                                   &quot;Model&quot;,
                                   &quot;Random classification&quot;)) +
  scale_colour_manual(&quot;&quot;, 
                      labels = c(&quot;Perfect&quot;, 
                                 &quot;Model&quot;,
                                 &quot;Random classification&quot;), 
                      values = c(&quot;gray&quot;,
                                 &quot;green&quot;, 
                                 &quot;gray&quot;)) +
  theme_classic()

As a result, I got the following plot.

在R中的ggplot2/ggroc中添加不同的线型。

huangapple
  • 本文由 发表于 2023年6月6日 13:04:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定