一种总是避开直方图的方法? [英] A way to always dodge a histogram?

查看:80
本文介绍了一种总是避开直方图的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ggplot2我用水平轴上的一个因子创建一个柱状图,并使用闪避位置创建填充颜色的另一个因子。我的问题是填充因子有时只取一个值作为横向因子的值,而没有任何东西可以闪避,因此它占据了整个宽度。有没有办法让它不闪避,因此所有的条宽都是一样的?或等价地将0绘制出来?



例如

  ggplot( data = mtcars,aes(x = factor(carb),fill = factor(gear)))+ 
geom_histogram(position =dodge)



这个答案有一些想法。在新版本发布之前也有人问到,所以可能有所改变?使用方面(也显示此处)我不喜欢我的情况,但我想编辑数据并使用 geom_bar 可以工作,但感觉不雅。此外,无论如何,当我尝试使用facetting时,无论如何,b / b

  ggplot(mtcars,aes(x =因子(碳水化合物) ))+ 
geom_bar()+ facet_grid(〜factor(carb))

错误layout_base(data,cols,drop = drop)中的错误:
至少有一个图层必须包含所有用于构图的变量



我想我可以生成计数的数据框,然后使用 geom_bar

  mtcounts <  -  ddply(子集(mtcars,select = c(carb,gear)),
.fun = count,.variables = c(carb,gear))

填写0不存在的关卡。有人知道这是否会奏效,或者有更好的方法吗?

geom_bar needs stat =identity



我不确定如果这对您来说太晚了,但请参阅最近发布的答案这里
也就是说,我会采用Joran的建议来预先计算 ggplot 调用之外的计数,并使用 geom_bar 。与其他文章的答案一样,这些计数分两步获得:首先,使用 dcast 获得计数的交叉表;然后秒,融化交叉制表。

  library(ggplot2)
library(reshape2)

dat = dcast(mtcars,因子(carb)〜因子(齿轮),fun.aggregate =长度)
dat.melt = melt(dat,id.vars =factor(carb),measure.vars = c(3, 4),5))
dat.melt

(p <-ggplot(dat.melt,aes(x =`factor(carb)`,y = value,fill =变量))+
geom_bar(stat =identity,position =dodge))

图表:


Using ggplot2 I'm creating a histogram with a factor on the horizontal axis and another factor for the fill color, using a dodged position. My problem is that the fill factor sometimes takes only one value for a value of the horizontal factor, and with nothing to dodge the bar takes up the full width. Is there a way to make it dodge nothing so that all bar widths are the same? Or equivalently to plot the 0's?

For example

ggplot(data = mtcars, aes(x = factor(carb), fill = factor(gear))) +
geom_histogram(position = "dodge")

This answer has a couple ideas. It was also asked before the new version was released, so maybe something changed? Using facets (also shown here) I don't like for my situation, though I suppose editing the data and using geom_bar could work, but it feels inelegant. Moreover, when I tried facetting anyway

ggplot(mtcars, aes(x = factor(carb), fill = factor(gear))) +
    geom_bar() + facet_grid(~factor(carb))

I get the error "Error in layout_base(data, cols, drop = drop): At least one layer must contain all variables used for facetting"

I suppose I could generate a data frame of counts and then use geom_bar,

mtcounts <- ddply(subset(mtcars, select = c("carb", "gear")),
    .fun = count, .variables = c("carb", "gear"))

filling out the levels that aren't present with 0's. Does anyone know if that would work or if there's a better way?

解决方案

Updated geom_bar needs stat = "identity"

I'm not sure if this is too late for you, but see the answer to a recent post here That is, I'd take Joran's advice to pre-calculate the counts outside the ggplot call and to use geom_bar. As with the answer to other post, the counts are obtained in two steps: first, a crosstabulation of counts is obtained using dcast; then second, melt the crosstabulation.

library(ggplot2)
library(reshape2)

dat = dcast(mtcars, factor(carb) ~ factor(gear), fun.aggregate = length)
dat.melt = melt(dat, id.vars = "factor(carb)", measure.vars = c("3", "4", "5"))
dat.melt

(p <- ggplot(dat.melt, aes(x = `factor(carb)`, y = value, fill = variable)) + 
  geom_bar(stat = "identity", position = "dodge"))

The chart:

这篇关于一种总是避开直方图的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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