如何从R中的文件无缝绘制F1、prec、recall
我有一个以csv制表符分隔的小文件,其中包含以下数据:如何从R中的文件无缝绘制F1、prec、recall,r,plot,ggplot2,plotly,R,Plot,Ggplot2,Plotly,我有一个以csv制表符分隔的小文件,其中包含以下数据: alg f1 prec recall rf 0.85891 0.808976 0.915413 svm 0.927857 0.988347 0.874345 knn 0.653483 0.611013 0.702298 nb 0.372421 0.253795 0.699256 我想这样画: 我是R方面的新手,因此我通过以下方式加载数据: library(ggplot2) libra
alg f1 prec recall
rf 0.85891 0.808976 0.915413
svm 0.927857 0.988347 0.874345
knn 0.653483 0.611013 0.702298
nb 0.372421 0.253795 0.699256
我想这样画:
我是R方面的新手,因此我通过以下方式加载数据:
library(ggplot2)
library(plotly)
# performance of various algs
test <- data.frame(header <- c("F-1", "Precision", "Recall"),
alg1 <- c(0.66381, 0.523659, 0.906397),
alg2 <- c(0.909586, 0.951798, 0.87096),
alg3 <- c(0.402166, 0.282086, 0.700253),
alg4 <- c(0.141439, 0.078692, 0.698064)
)
# plotting
ppl <- function() {
ggplot(test, aes(header, colour = "alg", group = 4)) +
geom_line(aes(y = alg1, colour = "rf"), size=1) +
geom_line(aes(y = alg2, colour = "svm"), size=1) +
geom_line(aes(y = alg3, colour = "knn"), size=1) +
geom_line(aes(y = alg4, colour = "nb"), size=1) +
xlab("measures") +
ylab("score") +
labs(title = "") +
theme(legend.justification = c(1, 1), legend.position = c(1, 1))
}
ppl()
然后以某种方式整理数据,这样
ggplot
就不会抱怨“美学”,不幸的是,我不知道怎么做。有没有更好、更简单的方法来绘制以下文件表?以下是您的解决方案:
library(ggplot2)
library(reshape2)
# performance of various algs
header <- c("F-1", "Precision", "Recall")
alg1 <- c(0.66381, 0.523659, 0.906397)
alg2 <- c(0.909586, 0.951798, 0.87096)
alg3 <- c(0.402166, 0.282086, 0.700253)
alg4 <- c(0.141439, 0.078692, 0.698064)
test <- data.frame(header,alg1,alg2,alg3,alg4)
test2 <- melt(test,id="header")
# plotting
ggplot(test2, aes(x=header,y=value,color=variable,group=variable)) +
geom_line(size=1) +
xlab("measures") +
ylab("score") +
labs(title = "") +
theme(legend.justification = c(1, 1), legend.position = c(1, 1)) +
scale_x_discrete(labels = c("F-1", "Precision", "Recall"))
库(ggplot2)
图书馆(E2)
#各种ALG的性能
头试试这个:
library(ggplot2)
library(reshape)
# example data
df1 <- read.table(text = "
alg f1 prec recall
rf 0.85891 0.808976 0.915413
svm 0.927857 0.988347 0.874345
knn 0.653483 0.611013 0.702298
nb 0.372421 0.253795 0.699256", header = TRUE)
# melt the data, wide-long
df1_melt <- melt(df1)
# then plot
ggplot(df1_melt, aes(x = variable, y = value, colour = alg, group = alg)) +
geom_line(size = 1) +
# prettify
scale_y_continuous(breaks = seq(0.25,0.75, 0.25), limits = c(0, 1)) +
xlab("measures") +
ylab("score") +
labs(title = "") +
theme(legend.justification = c(1, 1), legend.position = c(1, 1))
库(ggplot2)
图书馆(重塑)
#示例数据
df1示例测试数据是错误的。我们需要融合数据然后绘图,看我仍然需要手动将所有f1、prec、rec数字插入向量。是否有方法加载此文件并打印其数据?此示例更好,因为我可以加载文件,对加载的数据应用melt
,并直接打印,而无需任何手动键入。
library(ggplot2)
library(reshape)
# example data
df1 <- read.table(text = "
alg f1 prec recall
rf 0.85891 0.808976 0.915413
svm 0.927857 0.988347 0.874345
knn 0.653483 0.611013 0.702298
nb 0.372421 0.253795 0.699256", header = TRUE)
# melt the data, wide-long
df1_melt <- melt(df1)
# then plot
ggplot(df1_melt, aes(x = variable, y = value, colour = alg, group = alg)) +
geom_line(size = 1) +
# prettify
scale_y_continuous(breaks = seq(0.25,0.75, 0.25), limits = c(0, 1)) +
xlab("measures") +
ylab("score") +
labs(title = "") +
theme(legend.justification = c(1, 1), legend.position = c(1, 1))