R ggplot堆叠条形图(比例)缩放为x变量

R ggplot堆叠条形图(比例)缩放为x变量,r,ggplot2,R,Ggplot2,我需要创建一个相对比例的堆叠条形图,缩放到x变量。这就是我的意思 使用类似以下内容的数据帧: df <- data.frame(foo = rep(1:5,500), bar = as.factor(c(rep("a",100), rep("b",100), rep("c",100), rep("d",100), rep("e",100))), baz = c(rep("R", 5*250), rep("CRAN", 5*250)), val = rbinom(5*500,1,0.1))

我需要创建一个相对比例的堆叠条形图,缩放到x变量。这就是我的意思

使用类似以下内容的数据帧:

df <- data.frame(foo = rep(1:5,500), bar = as.factor(c(rep("a",100), rep("b",100), rep("c",100), rep("d",100), rep("e",100))), baz = c(rep("R", 5*250), rep("CRAN", 5*250)), val = rbinom(5*500,1,0.1))
最终结果应该类似于左侧的刻面(下图)。

如何在ggplot2中执行此操作?

如何

 library(gridExtra)
 grid.arrange(p1, p2, ncol=1)
其中,
p1
p2
是两个GG图

p1 <- ggplot(subset(df, val == 1), aes(x = foo)) + geom_bar(aes(fill = bar), 
       position = "fill") + scale_fill_brewer(type = "div", palette = 8, 
       direction = 1) +  facet_wrap(~baz) + geom_density(aes(foo))

p2 <- ggplot(subset(df, val == 1), aes(x = foo, y = as.factor(foo))) + 
       geom_col(position = "identity") + scale_fill_brewer(type = "div", 
       palette = 8, direction = 1) +  facet_wrap(~baz)

p1也许类似的东西会起作用:在
geom_bar()
中添加
stat=“identity”
并删除
fill=“position”
。你可以用直方图代替密度(基本上是相同的密度)


这是我提出的解决方案:

df <- data.frame(foo = rep(1:5,500), bar = as.factor(c(rep("a",100), rep    ("b",100), rep("c",100), rep("d",100), rep("e",100))), baz = c(rep("R", 5*250), rep("CRAN", 5*250)), val = rbinom(5*500,1,0.1))

p <- ggplot(subset(df, val == 1), aes(x = foo)) + geom_bar(aes(fill = bar), position = "fill") + scale_fill_brewer(type = "div", palette = 8, direction = 1) +  facet_wrap(~baz)
p1 <- ggplot(subset(df, val == 1), aes(x = foo, y = as.factor(foo))) + geom_col(position = "identity") + scale_fill_brewer(type = "div", palette = 8, direction = 1) +  facet_wrap(~baz)

z <- ggplot_build(p)
z1 <-ggplot_build(p1)
z$data[[1]]$ymin <- z$data[[1]]$ymin*z$data[[1]]$x
z$data[[1]]$ymax <- z$data[[1]]$ymax*z$data[[1]]$x
z$data[[1]]$y <- z$data[[1]]$y*z$data[[1]]$x
z$layout$panel_ranges <- z1$layout$panel_ranges

plot(ggplot_gtable(z))

df GridExtra和cow plot可以像这样并排放置绘图。我想要的是一个情节,将这两个情节融合在一起。p2的形状(从1步到5步)与p1的堆叠比例。我会尝试制作一个示例情节…哦,好的。请这样做。对不起,我就是不明白问题出在哪里。“缩放到第二个图”是什么意思?你不能用密码吗?或者你想把它们放在一起?@PoGibas,问题末尾的情节是我在GIMP中制作的,不是R的输出。左手面是我想要的,右手面是第二个情节的结果。现在更清楚了吗?请让我知道我的解决方案是否对您有效,或者即使它的方向正确。不幸的是,这不会起作用。@Simon我如何改进情节?我以前也遇到过类似的情况。你看到你的情节与问题中的模拟例子有什么不同吗?因此,第一个图给出了“foo”的每个值/类别的比例总和为1。如果我们在该图中取每个条(1:5),然后乘以它的foo值,那么我们将得到模拟示例中的阶梯图。
ggplot(subset(df, val == 1), aes(foo)) + 
    geom_bar(aes(y = foo, fill = bar), stat = "identity") + 
    geom_histogram(aes(foo), color = "black") +
    facet_wrap( ~ baz) +
    scale_fill_brewer(type = "div", palette = 8, direction = 1) +
    labs(x = NULL, y = NULL) +
    theme(legend.position = "bottom")
df <- data.frame(foo = rep(1:5,500), bar = as.factor(c(rep("a",100), rep    ("b",100), rep("c",100), rep("d",100), rep("e",100))), baz = c(rep("R", 5*250), rep("CRAN", 5*250)), val = rbinom(5*500,1,0.1))

p <- ggplot(subset(df, val == 1), aes(x = foo)) + geom_bar(aes(fill = bar), position = "fill") + scale_fill_brewer(type = "div", palette = 8, direction = 1) +  facet_wrap(~baz)
p1 <- ggplot(subset(df, val == 1), aes(x = foo, y = as.factor(foo))) + geom_col(position = "identity") + scale_fill_brewer(type = "div", palette = 8, direction = 1) +  facet_wrap(~baz)

z <- ggplot_build(p)
z1 <-ggplot_build(p1)
z$data[[1]]$ymin <- z$data[[1]]$ymin*z$data[[1]]$x
z$data[[1]]$ymax <- z$data[[1]]$ymax*z$data[[1]]$x
z$data[[1]]$y <- z$data[[1]]$y*z$data[[1]]$x
z$layout$panel_ranges <- z1$layout$panel_ranges

plot(ggplot_gtable(z))