R中条形图数组中的恒定条形宽度 [英] Constant bar width in a barplot array in R

查看:58
本文介绍了R中条形图数组中的恒定条形宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个(1 行,3 列)条形图数组,所有条形图都具有相同的条形宽度.

I am trying to make a (1 row, 3 column) array of barplots that all have the same bar width.

所有三个条形图都有不同数量的观测值,因此每个图的条形宽度最终都不同(即具有最多观测值的图形具有最窄的条形,而具有最少观测值的图形具有最宽的条形).我从 barplot {graphics} R 文档中了解到,

All three bar plots have a different number of observations and hence the width of the bars end up different for each plot (i.e. plot with the most observations has the narrowest bars and the plot with the fewest observations has the widest bars). I understand from the barplot {graphics} R documentation that,

除非指定了xlim,否则指定单个值将没有可见效果"

"Specifying a single value will have no visible effect unless xlim is specified"

但是,我的 x 标签 是字符串,所以我不确定如何指定 xlim.我不介意我的图的宽度是否不同,但是我需要以某种方式指定它吗?

However, my x labels are strings, so I'm not sure how to specify xlim. I don't mind if my plots are different widths, but do I need to specify this somehow?

这是一些假数据和我使用的代码...谢谢你的帮助.

Here is some fake data and the code I am using... thank you for your help.

height4plot1 <- c(1,6,9,2,3,10,7,15)
names4plot1 <- c("P1","P2","P3","P4","P5","P6","P7","P8")

height4plot2 <- c(5,4,10,2)
names4plot2 <- c("M1","M2","M3","M4")

height4plot3 <- c(4,12)
names4plot3 <0 c("U1","U2")

par(mfrow=c(1,3),
    mar=c(10,5,2,1),
    cex.axis=0.7,
    mgp=c(3,0.5,0))
barplot(height4plot1,
    names.arg=names4plot1,
    las=3,
    axes=TRUE,
    axisnames=TRUE,
    ylab="YLAB",
    ylim=ylim,
    plot=TRUE,
    main="PLOT1",
    width=1)
barplot(height4plot2,
    names.arg=names4plot2,
    las=3,
    axes=TRUE,
    axisnames=TRUE,
    ylim=ylim,
    plot=TRUE,
    main="PLOT2",
    width=1)
barplot(height4plot3,
    names.arg=names4plot3,
    las=3,
    axes=TRUE,
    axisnames=TRUE,
    ylim=ylim,
    plot=TRUE,
    main="PLOT3",
    width=1)

推荐答案

如果没有 height4plot1 等变量,测试你的代码和修改会比较困难,但这里有一些可能性:

Without the variables like height4plot1 etc. it is harder to test your code and modifications, but here are some possibilities:

您可以为每个图和 xlim=c(0,maxnum) 指定 width=0.8,其中 maxnum 是不同图中的最大条数.这将在每个较小的条形图中留下一个空白部分.

You can specify width=0.8 for each plot and xlim=c(0,maxnum) where maxnum is the maximum numbers of bars in the different plots. This will leave an empty section in each of the smaller bar plots.

您可以使用 NA 填充较短的高度向量,直到它们的长度都相同(这仍然会产生空部分).

You could pad the shorter vectors of heights with NA until they are all the same length (this would still give the empty sections).

您可以将 3 个向量连接在一起并制作单个箱线图,可能会为 3 个组着色以帮助区分它们.abline 函数可用于在组之间添加分隔线(并查看 barplot 的 space 参数,或包含一个NA"作为分隔线以在组之间提供更多空间组).您可以使用返回值来帮助将标题置于每个组之上.

You could concatenate your 3 vectors together and make a single boxplot, possibly coloring the 3 groups differently to help distinquish them. The abline function can be used to add divider lines between the groups (and see the space argument to barplot, or include an 'NA' as divider to give more space between the groups). You can use the return value to help in placing titles above each group.

您可以使用 layout 而不是 par 来设置您的 3 个绘图区域以具有不同的宽度,但获得确切的宽度比率以提供完全相同的尺寸条不会很简单(虽然你可能会尝试和错误接近).如果你真的想要这条路线,那么 grconvertXoptim 可能会有所帮助.

You can use layout instead of par to set up your 3 plotting areas to have different widths, but getting the exact ratios of widths to give the exact same size bars will not be simple (you may get close with trial and error though). If you really want this route then grconvertX and optim may help.

我会建议上面的第二个选项.

I would suggest the 2nd option above.

这篇关于R中条形图数组中的恒定条形宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆