简体   繁体   English

在R中创建直方图箱

[英]creating histogram bins in r

I have this code. 我有这个代码。

a = c("a", 1)
b = c("b",2)
c = c('c',3)
d = c('d',4)
e = c('e',5)
z = data.frame(a,b,c,d,e)
hist = hist(as.numeric(z[2,]))

I am trying to have a histogram such that the bins would be a,b,c,d,e 我正在尝试使用直方图,以使垃圾箱为a,b,c,d,e

and the freq values would be 1,2,3,4,5 . 和频率值将是1,2,3,4,5

However, it gives me an empty screen(no bins at all for histogram model) 但是,它给了我一个空白的屏幕(直方图模型根本没有垃圾箱)

You are plotting the factor levels of each column for row 2, which is in this case always 1. 您正在为第2行绘制每列的因子水平,在这种情况下始终为1。

When creating the dataframe you add stringsAsFactors=FALSE to avoid converting the numbers to factors. 创建数据stringsAsFactors=FALSE时,请添加stringsAsFactors=FALSE以避免将数字转换为因数。 This should work: 这应该工作:

z = data.frame(a,b,c,d,e,stringsAsFactors=FALSE)
hist(as.numeric(z[2,]))

Perhaps this would work for you: it creates a data frame with the x elements being the letters a through 'e', and the y elements being the numbers 1 through 5. It then renders a histogram and tells ggplot not to perform any binning. 也许这对您有用:它创建一个数据帧,其中x元素是字母a到'e', y元素是数字1到5。然后呈现直方图,并告诉ggplot不要执行任何合并。

library(ggplot2)
tmp <- data.frame(x = letters[1:5], y = 1:5)
ggplot(tmp, aes(x = x, y = y)) + geom_histogram(stat = "identity")

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

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