在R中绘制频率密度直方图 [英] Draw frequency density histogram in R

查看:596
本文介绍了在R中绘制频率密度直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R,任何人都可以告诉我如何绘制一个简单的直方图,在下列数据的bin之间没有空白: -

Class宽频率。 Dist

  0 <= x < 5 0.2 
5 <= x < 15 0.1
15 <= x < 20 1.2
20 <= x < 30 0.4
30 <= x < 40 0.4

所以我想让X轴从0-5,5-15,15-

预先感谢!

解决方案

这个怎么样?

  break <-c(0,5,15, 20,30,40)
计数<-c(0.2,0.1,1.2,0.4,0.4)
barplot(计数,
名称= sprintf([%g,%g) ,
break [-length(breaks)],bre​​aks [-1]
),
space = 0

这会给你等宽的条。另一方面,如果您想获得各种宽度的条形图,请输入:

  barplot(counts,diff ),
names = sprintf([%g,%g),中断[-length(breaks)],bre​​aks [-1]),
space = 0

此外,这将为您提供一个普通X轴:

  barplot(counts,diff(breaks),space = 0)
axis(1)

如果您想在 breaks 中的点精确地获取轴断点,请键入:

  axis(1,at = breaks)


Using R, can anyone show me how to draw a simple histogram with no gaps between the bins of the following data :-

Class Width Freq. Dist

0 <= x < 5          0.2 
5 <= x < 15         0.1
15 <= x < 20        1.2
20 <= x < 30        0.4
30 <= x < 40        0.4

So I want the X axis to go from 0-5,5-15,15-20,20-30 and 30-40 with the bars drawn appropriately.

Thanks in advance !

解决方案

How about this one?

breaks <- c(0,5,15,20,30,40)
counts <- c(0.2, 0.1, 1.2, 0.4, 0.4)
barplot(counts,
   names=sprintf("[%g,%g)",
      breaks[-length(breaks)], breaks[-1]
   ),
   space=0
)

This will give you bars of equal widths. On the other hand, If you'd like to obtain bars of various widths, type:

barplot(counts, diff(breaks),
    names=sprintf("[%g,%g)", breaks[-length(breaks)], breaks[-1]),
    space=0
)

Moreover, this will give you an "ordinary" X axis:

barplot(counts, diff(breaks), space=0)
axis(1)

And if you'd like to get axis breaks exactly at points in breaks, type:

axis(1, at=breaks)

这篇关于在R中绘制频率密度直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆