2017-08-17 56 views
1

我有以下數據小CSV製表符分隔的文件:如何無縫地繪製F1,PREC,召回從文件中的R

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 

我要繪製這樣的:

enter image description here

我在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() 

所以,每個情節我手動插入的數字,而我知道我能做到

data = read.table(file=file.choose(), sep="\t", header = TRUE) 

然後以某種方式排列的數據,使ggplot不會抱怨「美學」不幸的是,我不知道怎麼辦。繪製下面的文件表有沒有更好更簡單的方法?

+0

示例性測試數據是錯誤的。 – zx8754

+0

我們需要融化數據然後繪圖,看[示例](https://stackoverflow.com/questions/3427457/ggplot-and-r-two-variables-over-time) – zx8754

回答

1

試試這個:

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)) 
+0

這個例子比較好,因爲我可以加載文件,在加載的數據上應用'melt'並直接繪製,而無需任何手動輸入。 – minerals

2

這裏是你的解決方案:

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")) 

你需要首先使用reshape2包,並進一步使用創建的列(值和變量)爲y值,並且隨後的分組參數melt數據幀。

+0

我仍然必須**手動**將所有f1,prec,rec數字插入向量中。有沒有辦法只加載這個文件並繪製其數據? – minerals