将线图转换为条形图
我生成了三个线图,但数据并不是真正连续的,所以我想生成等效的条形图,但我的 R 知识极其缺乏。
我有以下数据。
> 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'))
这是输出(点击放大)。
由于我所有其他图都是用直接 R 生成的,从外观和感觉的角度来看,我更喜欢一个不使用像 ggplot 这样的库的解决方案>,尽管我会接受我能得到的任何建议。
I've generated three line plots, but the data isn't really continuous, so I'd like to generate equivalent barplots, but my R knowledge is extremely lacking.
I have the following data.
> 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
I use the following code to generate a plot.
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'))
This is the output (click to enlarge).
As all my other plots have been generated with straight R, from a look and feel point of view I'd prefer a solution that doesn't use libraries like ggplot
, though I'll take any advice I can get.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,如果您决定使用 ggplot2 包:
此代码
将生成:
且此代码:
将生成这个:
Well, if you decide to use the ggplot2 package:
This code
Will produce:
and this code:
will produce this:
感谢@k_jacko,除了x轴上的标签之外,这就是我想要的。
Thanks to @k_jacko, besides the labels on the x-axis, this is what I was after.