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

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

Add different linetypes in ggplot2/ggroc in R

问题

我想在ggplot中使用不同的线型,使用ggroc - 但我无法使scale_linetype_manual起作用。请参见下面的示例代码。

  1. library(pROC)
  2. library(ggplot2)
  3. # 示例数据
  4. # 完美
  5. probs_1 <- seq(0, 1, length.out=10000)
  6. roc_perfect <- roc(probs_1, probs_1)
  7. # 随机
  8. probs_1 <- rep(c(1, 0), 100)
  9. probs_2 <- c(rep(c(0, 1), 50),
  10. rep(c(1, 0), 50))
  11. roc_random <- roc(probs_1, probs_2)
  12. # AUC = 82
  13. set.seed(144)
  14. probs_82 <- seq(0, 1, length.out=10000)
  15. truth_82 <- runif(10000)^1.21 < probs
  16. roc_82 <- roc(truth_82, probs_82)
  17. # 绘制ROC曲线
  18. pROC::ggroc(list(roc_perfect, roc_82, roc_random),
  19. legacy.axes = FALSE,
  20. # linetype=1,
  21. linewidth = 0.4) +
  22. # 无效
  23. scale_linetype_manual(values=c(2, 1, 2)) +
  24. scale_colour_manual("",
  25. labels = c("Perfect",
  26. "Model",
  27. "Random classification"),
  28. values = c("gray",
  29. "green",
  30. "gray")) +
  31. 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.

  1. library(pROC)
  2. library(ggplot2)
  3. # Example data
  4. # Perfect
  5. probs_1 &lt;- seq(0, 1, length.out=10000)
  6. roc_perfect &lt;- roc(probs_1, probs_1)
  7. # Random
  8. probs_1 &lt;- rep(c(1, 0), 100)
  9. probs_2 &lt;- c(rep(c(0, 1), 50),
  10. rep(c(1, 0), 50))
  11. roc_random &lt;- roc(probs_1, probs_2)
  12. # AUC = 82
  13. set.seed(144)
  14. probs_82 &lt;- seq(0, 1, length.out=10000)
  15. truth_82 &lt;- runif(10000)^1.21 &lt; probs
  16. roc_82 &lt;- roc(truth_82, probs_82)
  17. # Plot ROC curves
  18. pROC::ggroc(list(roc_perfect, roc_82, roc_random),
  19. legacy.axes = FALSE,
  20. # linetype=1,
  21. linewidth = 0.4) +
  22. # Does not work
  23. scale_linetype_manual(values=c(2, 1, 2)) +
  24. scale_colour_manual(&quot;&quot;,
  25. labels = c(&quot;Perfect&quot;,
  26. &quot;Model&quot;,
  27. &quot;Random classification&quot;),
  28. values = c(&quot;gray&quot;,
  29. &quot;green&quot;,
  30. &quot;gray&quot;)) +
  31. theme_classic()

答案1

得分: 2

根据文档,您需要指定在函数中使用哪些美学元素。在您的代码中,它应该如下所示:

  1. pROC::ggroc(list(roc_perfect, roc_82, roc_random),
  2. aes = c("colour", "linetype"),
  3. legacy.axes = FALSE,
  4. linewidth = 0.4) +
  5. scale_linetype_manual("",
  6. values=c(2, 1, 2),
  7. labels = c("Perfect", "Model",
  8. "Random classification")) +
  9. scale_colour_manual("",
  10. labels = c("Perfect", "Model",
  11. "Random classification"),
  12. values = c("gray",
  13. "green",
  14. "gray")) +
  15. 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:

  1. pROC::ggroc(list(roc_perfect, roc_82, roc_random),
  2. aes = c(&quot;colour&quot;,
  3. &quot;linetype&quot;),
  4. legacy.axes = FALSE,
  5. linewidth = 0.4) +
  6. scale_linetype_manual(&quot;&quot;,
  7. values=c(2, 1, 2),
  8. labels = c(&quot;Perfect&quot;,
  9. &quot;Model&quot;,
  10. &quot;Random classification&quot;)) +
  11. scale_colour_manual(&quot;&quot;,
  12. labels = c(&quot;Perfect&quot;,
  13. &quot;Model&quot;,
  14. &quot;Random classification&quot;),
  15. values = c(&quot;gray&quot;,
  16. &quot;green&quot;,
  17. &quot;gray&quot;)) +
  18. 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:

确定