【问题标题】:Getting Bar in barplot all the same size across multi panel barplots在多面板条形图中获取条形图中的条形图大小相同
【发布时间】:2018-11-09 17:17:44
【问题描述】:

我有一个奇怪的问题,我正在使用 par() 制作多面板条形图,我注意到条形图的大小不同,我想知道是否可以使条形图的宽度(大小)相同每个面板?这将创建不同大小的面板,但这很好。任何 cmets 都会有所帮助。

我有这个通用的例子:

# create data
a<-c(1:100)
b<-c(1:200)
c<-c(1:300)
d<-c(1:400)
e<-c(1:500)

#make dataframes for barplots
test<-as.data.frame(cbind(a,b))
test1<-as.data.frame(cbind(a,b,c))
test2<-as.data.frame(cbind(a,b,c,d))
test3<-as.data.frame(cbind(a,b,c,d,e))

#gets means for each column
a1<-colMeans(test)
a2<-colMeans(test1)
a3<-colMeans(test2)
a4<-colMeans(test3)

#lets plot
pdf(file= "/Users/Highf_000/Desktop/prac.pdf");
par(mfrow = c(2, 4),     # 2 rows x 4 columns layout
      oma = c(2, 2, 0, 0), # two rows of text at the outer left and bottom margin
      mar = c(5, 5, 2, 1)+0.1, # space for one row of text at ticks and to separate plots
      mgp = c(2, 1, 0),    # axis label at 2 rows distance, tick labels at 1 row
      xpd = NA) 

barplot(mean(a))
barplot(a1)
barplot(a2)
barplot(a3)
barplot(a4)

dev.off()

This is the output

After Rebecca's suggestion

我们如何克服在多情节布局中情节消失的问题,以及如何将标题固定在条形而不是情节的中心?

【问题讨论】:

  • 这个问题已经有答案了,请看this post
  • @Rebecca,你能解释一下 xlim 和 width 命令是如何工作的吗?

标签: r plot par


【解决方案1】:

简化你的例子

a1 <- c(1,2)
a2 <- c(1,2,3)
a3 <- c(1,2,3,4)

barplot(a1) ## produces uneven bars
barplot(a2) ## produces uneven bars
barplot(a3) ## produces uneven bars

正如here 解释的那样,您至少可以执行以下操作之一:

选项 1

df <- data.frame(bar1 = c(1, 2, 0,0), 
                 bar2 = c(1, 2, 3, 0), 
                 bar3 = c(1, 2, 3, 4))

barplot(as.matrix(df), beside = TRUE)

选项 2

bar1 <- barplot(a1, xlim = c(0, 1), width = 0.1)
bar2 <- barplot(a2, xlim=c(0,1), width = 0.1)
bar3 <- barplot(a3, xlim=c(0,1), width = 0.1)

还有 ggplot 选项是你不喜欢 barplot。

【讨论】:

  • @Rebecca 和一般更有经验的 Rgrammers。使用 Rebecca 所做的工作,但这会导致其他问题。查看更新的图片。
  • 如果该建议解决了您的问题,请接受它作为答案。关于第二点,我不清楚您希望情节如何,也许您可​​以进一步解释一下?另外,考虑用你的(新)问题打开一个新帖子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 2011-07-21
  • 2020-09-18
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多