简体   繁体   English

R直方图X轴不均匀分布

[英]R Histograms X axis not equal distributed

I woould like to display a histogram with the allocation of school notes. 我想显示带有学校笔记分配的直方图。

The dataframe looks like: 数据框如下所示:

> print(xls)
# A tibble: 103 x 2
    X__1 X__2 
   <dbl> <chr>
 1     3 w    
 2     1 m    
 3     2 m    
 4     1 m    
 5     1 w    
 6     0 m    
 7     3 m    
 8     1 w    
 9     0 m    
10     5 m  

I create the histogram with: 我用以下方法创建直方图:

hist(xls$X__1, main='Notenverteilung', xlab='Note (0 = keine Beurteilung)', ylab='Anzahl')

It looks like: 看起来像: 在此处输入图片说明 Why are there spaces between 1,2,3 but not between 0 & 1? 为什么1,2,3之间有空格,但0和1之间却没有空格?

Thanks, BR Bernd 谢谢,伯恩德

Use ggplot2 for that, and your bars will be aligned ggplot2使用ggplot2 ,您的条形将对齐

library(ggplot2)
ggplot(xls, aes(x = X__1)) + geom_histogram(binwidth = 1)

在此处输入图片说明

You can try 你可以试试

barplot(table(xls$X__1))

在此处输入图片说明

or try 或尝试

h <- hist(xls$X__1, xaxt = "n", breaks = seq(min(xls$X__1), max(xls$X__1)))
axis(side=1, at=h$mids, labels=seq(min(xls$X__1), max(xls$X__1))[-1])

在此处输入图片说明

and using ggplot 并使用ggplot

ggplot(xls, aes(X__1)) + 
   geom_histogram(binwidth = 1, color=2) +
   scale_x_continuous(breaks = seq(min(xls$X__1), max(xls$X__1)))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM