- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将数据作为有序因子,级别为 1、2、3、4、5。 (这是李克特量表数据。)我想使用 ggplot 创建计数条形图,但这必须包括所有级别,甚至是计数为零的级别。这是一个在级别等于 2 时计数为零的示例数据框。
library(ggplot2)
foo <- structure(list(feed.n.1.50..3. = structure(c(4L, 4L, 4L, 4L,
4L, 5L, 5L, 4L, 1L, 1L,1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 4L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 3L, 4L, 4L, 3L, 4L, 4L, 3L, 4L, 5L, 4L, 4L, 4L, 4L), .Label = c("1",
"2", "3", "4", "5"), class = c("ordered", "factor"))), .Names = "answers", row.names = c(NA,
-50L), class = "data.frame")
table(foo) # satisfy myself the level 2 has zero entries
ggplot(foo,aes(answers)) + geom_bar(stat="bin") # stat="bin" is not needed, but there for clarity
stat_bin()
有一个参数drop
,记录为
drop: If TRUE, remove all bins with zero counts
不过,默认值为 FALSE,因此我希望保持级别不变。有没有一种简单的方法可以使用 ggplot 来保留因子的所有级别?
最佳答案
级别的下降是由比例(默认)完成的,因此使用带有参数 drop=FALSE
的 scale_x_discrete()
来显示所有级别。
ggplot(foo,aes(answers)) + geom_bar()+
scale_x_discrete(drop=FALSE)
关于r - 如何使用 ggplot2 和 stat_bin 制作包含数字级别但没有观察值的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20016617/
对于因子变量,stat_bin 中的密度图似乎无法按预期工作。 y 轴上每个类别的密度为 1。 例如,使用钻石数据: diamonds_small <- diamonds[sample(nrow(di
构建直方图后,我想为我的绘图添加一个上边界/轮廓。我不想使用 geom_bar或 geom_col因为我不想要每个箱子的垂直边界。 我的尝试包括使用 geom_histogram和 stat_bin(
我需要绘制一个包含多个具有不同 bin 大小配置的直方图的图。为此,我使用了几个 stat_bin 层。情节没问题,但我不知道如何添加将每个直方图名称与填充颜色连接起来的图例。 我一直在尝试几种选择,
我将数据作为有序因子,级别为 1、2、3、4、5。 (这是李克特量表数据。)我想使用 ggplot 创建计数条形图,但这必须包括所有级别,甚至是计数为零的级别。这是一个在级别等于 2 时计数为零的示例
我想用上面的数据做一个直方图,数据显示了某个国家的人均收入。使用这个命令我不断得到那个 Error: stat_bin() must not be used with a y aesthetic. i
我想用ggplot绘制直方图(或使用 stat_bin 绘制阶梯图)并使用 geom_point 在其上叠加几个点. 这是一个 base执行: library(plotrix) set.seed(10
我的表是 data.combined 与以下结构: 'data.frame': 1309 obs. of 12 variables: $ Survived: Factor w/ 3 level
我正在尝试使用 ggplot2 中的 stat_bin 函数按月对几年观察中的时间序列数据进行分类。代码如下所示: month.breaks<-seq.Date(from=min(afg$DateOc
我是一名优秀的程序员,十分优秀!