可变宽度条形图 [英] Variable Width Bar Plot

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

问题描述

我想在R中产生一个面积/条形图,与以下类似:
(来自David MacKay的(优秀)可持续能源一书)

我真的无法找到像这样的情节的专有名称。它似乎是一个可变宽度条形图。确定性是一个强大的交流工具。

解决方案

首先我们指定一些宽度和高度:

  widths = c(0.5,0.5,1 / 3,1 / 4,1 / 5,3.5,0.5)
heights = c(25,10,5,4.5,4,2,0.5)

然后我们使用标准 barplot 命令,但是指定块之间的空间为零:

  ##同时指定颜色
barplot(高度,宽度,空间= 0,
col =颜色()[1:6])

由于我们指定了宽度,我们需要指定轴标签:

  axis(1,0:6)

添加网格线,使用网格函数:

  ##看看?网格以更多地控制网格线
grid()

并且您可以添加箭头和文本手动:

 箭头(1,10,1.2,12,code = 1)
text(1.2 ,13,A country)

要在右上角添加广场,请使用 polygon function:

  polygon(c(4,4,5,5),c( 20,25,25,20),col =antiquewhite1)
text(4.3,22.5,Hi there,cex = 0.6)

全部给出:




  par(mar = c(3,3,2,1),
mgp = c(2,0.4,0),tck = - 。01,
cex.axis = 0.9,las = 1)


I'd like to produce an area/bar graph in R similar to this: (plot from David MacKay's (excellent) book "Sustainable Energy")

I honestly can't even find the proper name for a plot like this. It seems to be a bar graph with variable width bars. Certainty a powerful communication tool.

解决方案

You can do this with base graphics. First we specify some widths and heights:

widths = c(0.5, 0.5, 1/3,1/4,1/5, 3.5, 0.5)
heights = c(25, 10, 5,4.5,4,2,0.5)

Then we use the standard barplot command, but specify the space between blocks to be zero:

##Also specify colours
barplot(heights, widths, space=0, 
        col = colours()[1:6])

Since we specified widths, we need to specify the axis labels:

axis(1, 0:6)

To add grid lines, use the grid function:

##Look at ?grid to for more control over the grid lines
grid()

and you can add arrows and text manually:

arrows(1, 10, 1.2, 12, code=1)
text(1.2, 13, "A country") 

To add your square in the top right hand corner, use the polygon function:

polygon(c(4,4,5,5), c(20, 25, 25, 20), col="antiquewhite1")
text(4.3, 22.5, "Hi there", cex=0.6)

This all gives:


Aside: in the plot shown, I've used the par command to adjust a couple of aspects:

par(mar=c(3,3,2,1), 
    mgp=c(2,0.4,0), tck=-.01,
    cex.axis=0.9, las=1)

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

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