R ggplot 添加新的 roc 曲线

R ggplot add new roc curve

我想将 ROC 曲线添加到 ggplot 图表,但是 return 出错了 代码。

  library(ggplot2)
  library(plotROC)

  set.seed(2529)
  D.ex <- rbinom(200, size = 1, prob = .5)
  M1 <- rnorm(200, mean = D.ex, sd = .65)
  M2 <- rnorm(200, mean = D.ex, sd = 1.5)

  test <- data.frame(D = D.ex, D.str = c("Healthy", "Ill")[D.ex + 1], 
                     M1 = M1, M2 = M2, stringsAsFactors = FALSE)
  plot<-ggplot(longtest, aes(d = D, m = M1 )) + geom_roc() + style_roc()
  plot

没问题,但如果我添加新的 ROC 行,它会出现 return 错误

plot<-ggplot(longtest, aes(d = D, m = M1 )) + geom_roc() + style_roc()
plot+ggplot(test, aes(d = D, m = M2)) + geom_roc()

Error in p + o : non-numeric argument to binary operator In addition: Warning message: Incompatible methods ("+.gg", "Ops.data.frame") for "+"

如何添加新行并为所有行着色不同的颜色,并添加图例

将数据框从宽格式融合到长格式,然后将变量名称映射到美学映射中的线条颜色:

ggplot(melt_roc(test, "D", c("M1", "M2")), 
       aes(d = D, m = M, color = name)) + 
    geom_roc() + 
    style_roc()


如果你愿意,你也可以这样做:

ggplot() + 
  geom_roc(aes(d = D, m = M1, color="roc1"), test) + 
  geom_roc(aes(d = D, m = M2, color="roc2"), test) + 
  scale_color_manual(values=c("roc1"="red", "roc2"="blue"), 
                     name="color legend", guide="legend") + 
  style_roc()