根据澄清的问题进行更新
set.seed(1)
dat2 <- data.frame(fac = factor(sample(LETTERS, 100, replace = TRUE)))
hist(table(dat2), xlab = "Frequency of Level Occurrence", main = "")
gives:
这里我们只需要申请hist()
直接到结果table(dat)
. table(dat)
提供因子每个级别的频率,以及hist()
生成这些数据的直方图。
Original
有几种可能性。您的数据:
dat <- data.frame(fac = rep(LETTERS[1:4], times = c(3,3,1,5)))
以下是三个,从第一列,从上到下:
- 类的默认绘图方法
"table"
,绘制数据和类似直方图的条形图
- 条形图 - 这可能就是您所说的直方图。请注意此处的墨水与信息比率较低
- 点图或点图;显示与其他图相同的信息,但每单位信息使用的墨水要少得多。首选。
生成它们的代码:
layout(matrix(1:4, ncol = 2))
plot(table(dat), main = "plot method for class \"table\"")
barplot(table(dat), main = "barplot")
tab <- as.numeric(table(dat))
names(tab) <- names(table(dat))
dotchart(tab, main = "dotchart or dotplot")
## or just this
## dotchart(table(dat))
## and ignore the warning
layout(1)
这会产生:
如果你的数据只是变量factor
(顺便说一句,名字选择不好)然后table(factor)
可以使用而不是table(dat)
or table(dat$fac)
在我的代码示例中。
为了完整性,打包lattice
在生成点图时更加灵活,因为我们可以获得您想要的方向:
require(lattice)
with(dat, dotplot(fac, horizontal = FALSE))
giving:
And a ggplot2
版本:
require(ggplot2)
p <- ggplot(data.frame(Freq = tab, fac = names(tab)), aes(fac, Freq)) +
geom_point()
p
giving: