【发布时间】:2017-11-16 01:00:08
【问题描述】:
我有一些计数变量,我想在同一 y 轴上制作条形图,但我没有分组变量。类似于下面的情节
B <- 25
iter_M1
[1] 5 13 14 11 7 8 10 14 10 5 7 13 10 12 4 5 9 6 5 12 8 8 7 11 9
max_M1 <- max(iter_M1)
count_M1 <- integer(max_M1)
for(i in 1:max_M1)
{
for(j in 1:B)
{
if(iter_M1[j] == i)
count_M1[i] = count_M1[i] +1
}
}
count_M1
[1] 0 0 0 1 4 1 3 3 2 3 2 2 2 2
df <- data.frame(x = 1:max_M1, y = count_M1)
p_M1 <-ggplot(data=df, aes(x=x, y=y)) + geom_bar(stat="identity")
p_M1
这导致了这样的情节
和另一个类似的变量
iter_M2
[1] 3 1 3 2 6 3 4 4 3 7 4 2 2 3 4 3 4 4 1 3 7 3 2 4 2
max_M2 <- max( iter_M2)
count_M2 <- integer(max_M2)
for(i in 1:max_M2)
{
for(j in 1:B)
{
if(iter_M2[j] == i)
count_M2[i] = count_M2[i] +1
}
}
count_M2
[1] 2 5 8 7 0 1 2 df1 <- data.frame(x1 = 1:max_M2, y1 = count_M2)
p_M2 <-ggplot(data=df1, aes(x=x1, y=y1)) +
geom_bar(stat="identity") p_M2
导致第二个情节为
以及类似的变量...我如何并排绘制这些数据。也是我目前生成数据的方式,所有 x 轴都没有共同的 y 轴。是否有一些建议以其他格式生成这样的图或数据集以实现所需的图。
【问题讨论】:
-
试试
facet_wrapggplot2.tidyverse.org/reference/facet_wrap.html -
我没有课!
-
为什么不创建一个因子(类)然后使用 facet wrap?或者您可以创建单独的图并将它们与
grid.arrange并排粘贴。 -
当然,在同一个窗口中绘制多个图也是一种选择,但我不喜欢这样,因为我想通过绘制公共 y 轴来使条形具有可比性。跨度>
-
Facet wrap 可以给出一个共同的 y 轴。