【发布时间】:2018-04-16 16:48:50
【问题描述】:
我希望能够通过针对两个因素绘制连续变量的条形图来改变条形图的宽度。并非所有的因子组合都有值,我希望它们要么不被绘制,要么具有更小的条形宽度。这是为了避免在图中浪费空间。 This is the kind of chart I am making 它的点叠加在透明条上。
我已经将基本 R 用于一系列条形图,如果可能的话,我宁愿不转向 ggplot2。
我尝试了不同的“宽度”和“空间”,但无法使其正常工作。宽度向量在前两项之后被回收。
示例代码
mydata=data.frame(y=rnorm(50,1,1))
group1=c(rep(1, times=30), rep(rep(c(1,2), each = 5), times=2))
group2=rep(c(c("A", "B", "C", "D", "E")), each=10)
cbind(mydata, group1, group2)
xbar=with(mydata, tapply(y, list(group1, group2), mean, na.rm=TRUE))
barplot(xbar, beside = TRUE)
legend("topleft", legend=c("Group 1", "Group 2"),
text.col=c("black", "gray50"))
谁能建议一种方法来删除没有条形的空格,因为该因素组合中没有数据?非常感谢。
Barplot showing empty space for empty factor combination
这些是我尝试过的宽度 `宽度=0.5
xlim=c(0,width*11)
w=c(width, 0, width, 0, width, 0, width, width, width, width)
barplot(xbar, beside=TRUE, width=w, xlim=xlim)`
我认为这仅使用向量的前两个值。 Trying to vary width as a vector
我也尝试将它作为数据框(即使文档要求提供向量) - 这与上面的向量尝试给出相同的结果
single=c(width, 0)
double=c(width, width)
w=cbind(single, single, single, double, double)
barplot(xbar, beside=TRUE, width=w, xlim=xlim)
【问题讨论】:
-
barplot有一个width参数;向我们展示您对这个无效的论点的尝试。 -
嗨,迈克尔,感谢您的回复,我已经用我尝试过的内容编辑了我的帖子
-
每个条形应该有一个宽度。 space 是控制条形之间间隙的参数。