2011-03-31 41 views
0

我已经生成了三条线图,但数据并非真正连续,所以我想生成等效的线条图,但是我的R知识极其缺乏。将线图转换为barplot

我有以下数据。

> dat 
    classifier depth average 
1   bayes  0 3.5639098 
2   bayes  1 6.0000000 
3   bayes  2 3.0253165 
4   bayes  3 5.2250000 
5   bayes  4 1.7931034 
6   bayes  5 2.6800000 
7   bayes  6 3.6551724 
8  adaboost  0 9.2857143 
9  adaboost  1 0.9733333 
10  adaboost  2 0.4050633 
11  adaboost  3 0.4750000 
12  adaboost  4 0.3448276 
13  adaboost  5 0.6000000 
14  adaboost  6 0.4137931 
15 randomforest  0 7.0375940 
16 randomforest  1 0.8000000 
17 randomforest  2 0.7341772 
18 randomforest  3 1.2750000 
19 randomforest  4 0.3103448 
20 randomforest  5 0.3600000 
21 randomforest  6 0.3448276 

我使用下面的代码来生成一个图。

dat <- read.table('depth_errors.data', sep=',',header=T) 
plot(0,0,xlim=c(0,6),ylim=c(0,10),xlab='depth', 
    ylab='average misclassifications',type='n') 

# Change the stroke 
lines(dat[dat$classifier=='bayes',][,-1]) 
lines(dat[dat$classifier=='adaboost',][,-1],lty='dashed') 
lines(dat[dat$classifier=='randomforest',][,-1],lty='dotted') 

legend('topright', c('Naive Bayes', 'AdaBoost', 'Random Forest'), 
     lty=c('solid','dashed','dotted')) 

这是输出(点击放大)。

average misclassifications

由于所有我的其他地块已与直[R产生的,从外观和感觉的观点,但我宁愿不使用库,例如​​ggplot一个解决方案,但我会采取任何我可以得到的建议。

回答

0

感谢@k_jacko,除了在x轴的标签,这是我后。

mat <- matrix(dat$average, nrow=3, byrow=T) 

barplot(
    mat, 
    ylim=c(0,10), 
    xlab='depth', 
    names.arg=levels(factor(dat$depth)), 
    ylab='average misclassifications', 
    beside=TRUE, 
    legend.text=c('Naive Bayes', 'AdaBoost', 'Random Forest') 
) 
1

好吧,如果你决定使用GGPLOT2包:

此代码

library(ggplot2) 
dat$depth <- factor(dat$depth) 

p <- ggplot(dat, aes(x=depth, y=average, fill=classifier)) + geom_bar() 
print(p) 

会产生:

enter image description here

和验证码:

p <- ggplot(dat, aes(x=depth, y=average, fill=classifier)) + geom_bar(stat="identity", position="dodge") 
print(p) 

会产生这样的:

enter image description here

+0

谢谢,这是很好的参考,ggplot肯定会产生更多有吸引力的情节。但这意味着需要重新生成几十个不同的地块,我现在只想完成它。我确定这是与我的设置,但我也得到上述代码错误,aesdefaults错误(数据,。$ geom $ default_aes(),mapped_vars):无法找到函数“as_df” – michaeltwofish 2011-03-31 04:07:31