df <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L), .Label = c("1",
"2", "3", "4", "5", "6", "7"), class = "factor"), TYPE = structure(c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L,
1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L,
5L, 6L, 1L, 2L, 3L), .Label = c("1", "2", "3", "4", "5", "6",
"7", "8"), class = "factor"), TIME = structure(c(2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L), .Label = c("1", "5", "15"), class = "factor"), VAL = c(0.937377670081332,
0.522220720537007, 0.278690102742985, 0.967633064137772, 0.116124767344445,
0.0544306698720902, 0.470229141646996, 0.62017166428268, 0.195459847105667,
0.732876230962574, 0.996336271753535, 0.983087373664603, 0.666449476964772,
0.291554537601769, 0.167933790013194, 0.860138458199799, 0.172361251665279,
0.833266809117049, 0.620465772924945, 0.786503327777609, 0.761877260869369,
0.425386636285111, 0.612077651312575, 0.178726130630821, 0.528709076810628,
0.492527724476531, 0.472576208412647, 0.0702785139437765, 0.696220921119675,
0.230852259788662, 0.359884874196723, 0.518227979075164, 0.259466265095398,
0.149970305617899, 0.00682218233123422, 0.463400925742462, 0.924704828299582,
0.229068386601284)), .Names = c("ID", "TYPE", "TIME", "VAL"), row.names = c(NA,
-38L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
如果我创建以下图:
ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
facet_wrap(~ TIME, ncol=1) +
geom_bar(position="stack") +
coord_flip()
Run Code Online (Sandbox Code Playgroud)

然后,我决定理想的是,我希望将任何因素都显示在他们没有任何数据的方面.我已经引用了各种问题和答案,说scale="free"方法是要走的路(相反,drop=TRUE它会丢弃与未使用的值相对应的空面TIME),所以接下来:
ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
facet_wrap(~TIME, ncol=1, scale="free") +
geom_bar(position="stack") +
coord_flip()
Run Code Online (Sandbox Code Playgroud)

我的问题是如何防止对于具有4个小节的小平面与具有3个小节的小平面发生的小节的重新缩放.在这个人为的例子中效果是微妙的,更糟糕的是我的实际数据.理想的输出将具有在垂直轴上具有ID因子1,4和6的底面,其中条与顶面具有相同的宽度,因此小面的整体垂直尺寸将减小.
如果你可以帮我解决为什么计算堆积而不是数值(现在修复)的加分点
赏金更新:
正如我提到的后续问题,它看起来像一个更好的解决方案可能会涉及使用ggplot_build和ggplot_table和修改gtable对象.我很确定我能算出时间,但我希望赏金可能会激励别人帮助我.Koshke发布了一些这方面的例子.
jor*_*ran 15
这个怎么样:
df$w <- 0.9
df$w[df$TIME == 5] <- 0.9 * 3/4
ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
facet_wrap(~TIME, ncol=1, scale="free") +
geom_bar(position="stack",aes(width = w),stat = "identity") +
coord_flip()
Run Code Online (Sandbox Code Playgroud)

不确定我是否在那里得到算术,但你明白了.
nas*_*ddd 11
如果您对垂直条纹没问题,那么facet_grid可以完美地运行:
ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
facet_grid(.~TIME, scale="free_x", space = "free_x") +
geom_bar(position="stack")
Run Code Online (Sandbox Code Playgroud)

为了更进一步,您可以旋转图表的所有元素.如果你把头转90°,你现在有了你想要的东西:-)
ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
facet_grid(.~TIME, scale="free_x", space = "free_x") +
geom_bar(position="stack") +
opts(legend.text = theme_text(angle=90),
legend.title = theme_text(angle=90),
strip.text.x = theme_text(angle=90),
axis.text.x = theme_text(angle=90),
axis.text.y = theme_text(angle=90),
axis.title.x = theme_text(angle=90)
)
Run Code Online (Sandbox Code Playgroud)

自原始问题起五年多以来,所以认为它应该提供更新,更清洁的版本.这大致遵循这里给出的建议:http://ggplot2.tidyverse.org/reference/facet_grid.html
两个关键参数space="free_y"允许每个面板的高度与刻度的长度成比例,scales="free_y"并允许每个刻度的刻度变化.
coord_flip()允许水平绘制条形图,并且作为个人偏好,小平面的标签在theme选项内旋转.
ggplot(df, aes(x=ID, y=VAL, fill=TYPE)) +
geom_bar(stat="identity") +
coord_flip() +
facet_grid(TIME~., scales = "free_y", space = "free_y") +
theme(strip.text.y = element_text(angle = 0))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6666 次 |
| 最近记录: |