2011-03-31 5 views
0

3 개의 라인 플롯이 생성되었지만 데이터가 실제로 연속적이지 않으므로 이에 상응하는 바 플롯을 생성하고 싶지만 R 지식은 극도로 부족합니다.라인 플롯을 바 plot으로 변환

다음 데이터가 있습니다.

> 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 같은 라이브러리를 사용하지 않는 솔루션을 선호하는 것 관점을 느낄 수있다 내가 얻을 수있는 조언. 당신이 ggplot2 패키지를 사용하기로 결정한 경우

답변

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

음 :

이 코드

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) 
,369을

이를 생성합니다 :

enter image description here

+0

감사합니다. 참조 용으로 좋습니다. ggplot은 훨씬 더 매력적인 플롯을 생성합니다. 그러나 그것은 수십개의 다른 플롯을 다시 생성하는 것을 의미 할 것이고, 지금은 그냥 끝내기를 원할 것입니다. 그게 내 설정과 함께 뭔가를 확신하지만, 위의 코드와 오류가 발생합니다 aesdefaults (data,. $ geom $ default_aes(), mapped_vars)의 오류 : "as_df"함수를 찾을 수 없습니다. – michaeltwofish

관련 문제