简体   繁体   English

用Rgplot2绘制R中的ROC曲线

[英]Plot an ROC curve in R with ggplot2

I have two variables (Condition1 & Condition 2) for which I have calculated false alarm rates and hit rates and I would like to plot these on the same graph with separate curves for Condition1 and Condition 2. Both of these conditions have three points and both use the same false alarm rates. 我有两个变量(条件1和条件2),我已经计算了误报率和命中率,我想在条件1和条件2的单独曲线上绘制这些变量。这两个条件都有三个点,两者都有使用相同的误报率。 Here is what my data frame looks like: 这是我的数据框架的样子:

measure <- c('False_Alarm','False_Alarm','False_Alarm', 'Hit_Rate_Condition1',
 'Hit_Rate_Condition1','Hit_Rate_Condition1', 'Hit_Rate_Condition2','Hit_Rate_Condition2',
  'Hit_Rate_Condition2')
point_on_curve<- c(1, 2, 3, 1, 2, 3, 1, 2, 3)
percentage <- c(0.11, 0.1, 0.01, 0.80, 0.50, 0.20, 0.80, 0.55, 0.25)

ROC_data <- data.frame(measure,point_on_curve, percentage)

False alarms should go on the x axis, and hit rates should go on the y axis. 虚警应该在x轴上,并且命中率应该在y轴上。 Please note that I specifically am not interested in ROC packages such as ROCR, but rather I think it should be possible to plot this using just ggplot. 请注意,我特别对ROCR等ROC包不感兴趣,但我认为应该可以使用ggplot进行绘图。

Many thanks in advance! 提前谢谢了!

ps Just to show that I have at least been trying something and genuinely don't want to get stack exchange to do my PhD for me, I've tried recasting the DF and separating the two to make two plots as so: ps只是为了表明我至少已经尝试了一些东西并且真的不想让堆栈交换为我做博士学位,我已经尝试重铸DF并将两者分开以制作两个图:

 Condition_1 <- dcast(ROC_data, point_on_curve ~ measure)
 Condition_1 <- Condition_1[which(!Condition_1$measure == Hit_Rate_Condition2),]
p <- ggplot(data=Condition_1, aes(x=False_Alarm, y=Hit_Rate)) +     
  geom_point() +    
  stat_smooth(method = "lm", formula = y ~ splines::bs(x, 1), col = "red") +
  expand_limits(x = c(0, 1))

This results in some absurd line which isn't a smooth ROC (not what I need), and I would still need to combine the two.... in any case, there must be an easy way to make this plot (which I need to do another 18 times) with the original DF in one go... 这导致一些荒谬的线条,这不是一个平滑的ROC(不是我需要的),我仍然需要将两者结合起来....无论如何,必须有一个简单的方法来制作这个情节(我需要另外做18次)与原来的DF一起去......

This is an example of how the curved line should be (I'm not looking for something aesthetically the same, just the same sort of curve!) 这是曲线应该如何的一个例子(我不是在寻找美学上相同的东西,只是同样的曲线!) 在此输入图像描述

I think you need to reformat your data so that your x and y variables (the false positive rate and the true positive rate) are in separate columns and then use geom_step . 我认为你需要重新格式化你的数据,以便你的x和y变量(误报率和真正的正率)在不同的列中,然后使用geom_step Take a look at the code and output below and let me know if this is what you were aiming for: 看看下面的代码和输出,让我知道这是你的目标:

ROC_data <- data.frame(measure, point_on_curve, percentage)

ROC_data = cbind(ROC_data[rep(1:3,2),], ROC_data[4:nrow(ROC_data),])
ROC_data = ROC_data[,c(3,4,6)]
names(ROC_data) = c("FP_Rate","condition","TP_Rate")

ROC_data
  FP_Rate condition TP_Rate 1 0.11 Hit_Rate_Condition1 0.80 2 0.10 Hit_Rate_Condition1 0.50 3 0.01 Hit_Rate_Condition1 0.20 1.1 0.11 Hit_Rate_Condition2 0.80 2.1 0.10 Hit_Rate_Condition2 0.55 3.1 0.01 Hit_Rate_Condition2 0.25 
ggplot(ROC_data, aes(FP_Rate,TP_Rate,colour=condition)) + 
  geom_step() +
  coord_cartesian(xlim=c(0,1), ylim=c(0,1)) +
  theme_bw()

在此输入图像描述

If you want to connect the points with a straight line, you can use geom_line instead: 如果要使用直线连接点,可以使用geom_line代替:

ggplot(ROC_data, aes(FP_Rate,TP_Rate,colour=condition)) + 
  geom_line() +
  geom_point() +
  coord_cartesian(xlim=c(0,1), ylim=c(0,1)) +
  theme_bw()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM