[英]Why i can't plot a smoothing curve in ggplot2
Good afternoon,下午好,
Assume we have the following code where i'm trying to plot ggplot2 smoothing curve
:假设我们有以下代码,我正在尝试 plot
ggplot2 smoothing curve
:
library(ggplot2)
library(dplyr)
melted=structure(list(Var1 = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Var2 = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L,
7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L,
10L, 10L, 11L, 11L, 11L, 11L, 11L), .Label = c("Sensitivity",
"Specificity", "Pos Pred Value", "Neg Pred Value", "Precision",
"Recall", "F1", "Prevalence", "Detection Rate", "Detection Prevalence",
"Balanced Accuracy "), class = "factor"), value = c(0, 0.85,
0.85, 0.86, 0.86, 0, 0.188235294117647, 0.188235294117647, 0.188235294117647,
0.188235294117647, 0, 0.711297071129707, 0.711297071129707, 0.713692946058091,
0.713692946058091, 0, 0.347826086956522, 0.347826086956522, 0.363636363636364,
0.363636363636364, 0, 0.711297071129707, 0.711297071129707, 0.713692946058091,
0.713692946058091, 0, 0.85, 0.85, 0.86, 0.86, 0, 0.774487471526196,
0.774487471526196, 0.780045351473923, 0.780045351473923, 0, 0.701754385964912,
0.701754385964912, 0.701754385964912, 0.701754385964912, 0, 0.596491228070175,
0.596491228070175, 0.603508771929825, 0.603508771929825, 0, 0.83859649122807,
0.83859649122807, 0.845614035087719, 0.845614035087719, 0, 0.519117647058823,
0.519117647058823, 0.524117647058824, 0.524117647058824)), row.names = c(NA,
-55L), groups = structure(list(Var2 = structure(1:11, .Label = c("Sensitivity",
"Specificity", "Pos Pred Value", "Neg Pred Value", "Precision",
"Recall", "F1", "Prevalence", "Detection Rate", "Detection Prevalence",
"Balanced Accuracy "), class = "factor"), .rows = structure(list(
1:5, 6:10, 11:15, 16:20, 21:25, 26:30, 31:35, 36:40, 41:45,
46:50, 51:55), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -11L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
ROC<-function(melted){
v=length(which(melted[,2]=="Sensitivity"))
melted= melted %>%
group_by(Var2) %>%
arrange(Var1) %>%
filter(row_number()==n())
x=c(0,1-filter(melted,Var2=="Specificity")$value,1)
y=c(0,filter(melted,Var2=="Sensitivity")$value,1)
x=as.numeric(x)
y=as.numeric(y)
dput(x)
dput(y)
df<-data.frame(x=x,y=y)
ggplot(df, aes(x=x, y=y)) +
geom_point() +
geom_smooth()
print(melted)
}
ROC(melted)
When i call the ROC(melted)
function on the melted data, i don't get the ROC plot
( Although the R code seems to be correct ).当我在熔化的数据上调用
ROC(melted)
function 时,我没有得到the ROC plot
(尽管 R 代码似乎是正确的)。 I tried to convert x
and y
from lists to numeric but the pb is not solved !我试图将
x
和y
从列表转换为数字,但 pb 没有解决!
However with the same steps ( using raw data and not a function call
) i got the desired plot:但是使用相同的步骤(
using raw data and not a function call
)我得到了所需的 plot:
library(ggplot2)
library(dplyr)
x=c(0, 0.811764705882353, 1)
y=c(0, 0.86, 1)
df<-data.frame(x=x,y=y)
ggplot(df, aes(x=x, y=y)) +
geom_point() +
geom_smooth()
print(melted)
Thank you for help !谢谢你的帮助 !
ROC(melded) will work, when you dont use "print(melted)" at the end of your function.当您不在 function 末尾使用“print(melted)”时,ROC(melded) 将起作用。 Instead, just let the ggplot command be the last command in the function ROC<-function(melted).
相反,只需让 ggplot 命令成为 function ROC<-function(melted) 中的最后一个命令。 Then the ggplot will be the output.
那么 ggplot 将是 output。
library(ggplot2)
library(dplyr)
melted=structure(list(Var1 = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Var2 = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L,
7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L,
10L, 10L, 11L, 11L, 11L, 11L, 11L), .Label = c("Sensitivity",
"Specificity", "Pos Pred Value", "Neg Pred Value", "Precision",
"Recall", "F1", "Prevalence", "Detection Rate", "Detection Prevalence",
"Balanced Accuracy "), class = "factor"), value = c(0, 0.85,
0.85, 0.86, 0.86, 0, 0.188235294117647, 0.188235294117647, 0.188235294117647,
0.188235294117647, 0, 0.711297071129707, 0.711297071129707, 0.713692946058091,
0.713692946058091, 0, 0.347826086956522, 0.347826086956522, 0.363636363636364,
0.363636363636364, 0, 0.711297071129707, 0.711297071129707, 0.713692946058091,
0.713692946058091, 0, 0.85, 0.85, 0.86, 0.86, 0, 0.774487471526196,
0.774487471526196, 0.780045351473923, 0.780045351473923, 0, 0.701754385964912,
0.701754385964912, 0.701754385964912, 0.701754385964912, 0, 0.596491228070175,
0.596491228070175, 0.603508771929825, 0.603508771929825, 0, 0.83859649122807,
0.83859649122807, 0.845614035087719, 0.845614035087719, 0, 0.519117647058823,
0.519117647058823, 0.524117647058824, 0.524117647058824)), row.names = c(NA,
-55L), groups = structure(list(Var2 = structure(1:11, .Label = c("Sensitivity",
"Specificity", "Pos Pred Value", "Neg Pred Value", "Precision",
"Recall", "F1", "Prevalence", "Detection Rate", "Detection Prevalence",
"Balanced Accuracy "), class = "factor"), .rows = structure(list(
1:5, 6:10, 11:15, 16:20, 21:25, 26:30, 31:35, 36:40, 41:45,
46:50, 51:55), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -11L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
ROC<-function(melted){
v=length(which(melted[,2]=="Sensitivity"))
melted= melted %>%
group_by(Var2) %>%
arrange(Var1) %>%
filter(row_number()==n())
x=c(0,1-filter(melted,Var2=="Specificity")$value,1)
y=c(0,filter(melted,Var2=="Sensitivity")$value,1)
x=as.numeric(x)
y=as.numeric(y)
dput(x)
dput(y)
df<-data.frame(x=x,y=y)
ggplot(df, aes(x=x, y=y)) +
geom_point() +
geom_smooth()
}
ROC(melted)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.