如何使用barplot函数在R中的plot window中制作相同大小的barplot条

how to make barplot bars same size in plot window in R using barplot function

我想在同一个 window 中绘制 3 个地块。每个都有不同数量的条形图。如果不在较小的 barplots 中做 NA,我怎么能让它们大小相同并靠在一起(彼此之间的距离相同)。下面的示例代码。我确实想指出我的真实数据将绘制来自 dataframes$columns 的数字,而不是数字向量,如下所示。我确信有神奇的方法可以做到这一点,但似乎无法在网上找到有用的信息。谢谢

pdf(file="PATH".pdf");
par(mfrow=c(1,3));
par(mar=c(9,6,4,2)+0.1);
barcenter1<- barplot(c(1,2,3,4,5));
mtext("Average Emergent", side=2, line=4);
par(mar=c(9,2,4,2)+0.1);
barcenter2<- barplot(c(1,2,3));
par(mar=c(9,2,4,2)+0.1);
barcenter3<- barplot(c(1,2,3,4,5,6,7));

或者有没有一种方法可以代替使用 par(mfrow....) 来绘制图 window,我们可以将 barcenter 数据分组在一个空的图上吗 space 酒吧之间?这样一切都是 spaced 并且看起来一样?

使用参数 xlimwidth

par(mfrow = c(1, 3))
    par(mar = c(9, 6, 4, 2) + 0.1)
    barcenter1 <- barplot(c(1, 2, 3, 4, 5), xlim = c(0, 1), width = 0.1)
    mtext("Average Emergent", side = 2, line = 4)
    par(mar = c(9, 2, 4, 2) + 0.1)
    barcenter2 <- barplot(c(1, 2, 3), xlim = c(0, 1), width = 0.1)
    par(mar = c(9, 2, 4, 2) + 0.1)
    barcenter1 <- barplot(c(1, 2, 3, 4, 5, 6, 7), xlim = c(0, 1), width = 0.1)

引入零:

df <- data.frame(barcenter1 = c(1, 2, 3, 4, 5, 0, 0), 
                 barcenter2 = c(1, 2, 3, 0, 0, 0, 0), 
                 barcenter3 = c(1, 2, 3, 4, 5, 6, 7))
barplot(as.matrix(df), beside = TRUE)

使用 ggplot2 你可以得到这样的东西:

df <- data.frame(x=c(1, 2, 3, 4, 5,1, 2, 3,1, 2, 3, 4, 5, 6, 7),
y=c(rep("bar1",5), rep("bar2",3),rep("bar3",7)))
library(ggplot2)
ggplot(data=df, aes(x = x, y = x)) +
  geom_bar(stat = "identity")+
  facet_grid(~ y)

对于您在第二条评论中提到的选项,您需要:

x <- c(1, 2, 3, 4, 5, NA, 1, 2, 3, NA, 1, 2, 3, 4, 5, 6, 7)
barplot(x)