我想在R中生成一个面积/条形图,如下所示:

(情节来自David MacKay(优秀)的书“可持续能源”)
老实说,我甚至找不到合适的名字来描述这样的情节。它似乎是一个条形图,带有可变宽度的条形图。确定性是一个强大的沟通工具。
发布于 2013-01-30 04:10:03
您可以使用基础图形来执行此操作。首先,我们指定一些宽度和高度:
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命令,但将块之间的间距指定为零:
##Also specify colours
barplot(heights, widths, space=0,
col = colours()[1:6])由于我们指定了宽度,因此需要指定轴标签:
axis(1, 0:6)要添加网格线,请使用grid函数:
##Look at ?grid to for more control over the grid lines
grid()您还可以手动添加箭头和文本:
arrows(1, 10, 1.2, 12, code=1)
text(1.2, 13, "A country") 要在右上角添加正方形,请使用polygon函数:
polygon(c(4,4,5,5), c(20, 25, 25, 20), col="antiquewhite1")
text(4.3, 22.5, "Hi there", cex=0.6)这一切都给了我们:

旁白:在所示的图中,我使用par命令调整了几个方面:
par(mar=c(3,3,2,1),
mgp=c(2,0.4,0), tck=-.01,
cex.axis=0.9, las=1)发布于 2013-01-30 04:12:45
受我上面提到的the blog post代码的启发,
df <- data.frame(x = c("Alpha", "Beta", "Gamma", "Delta"), width = c(25, 50, 75, 100), height = c(100, 75, 50, 25))
df$w <- cumsum(df$width)
df$wm <- df$w - df$width
df$wt <- with(df, wm + (w - wm)/2)
library(ggplot2)
p <- ggplot(df, aes(ymin = 0))
p1 <- p + geom_rect(aes(xmin = wm, xmax = w, ymax = height, fill = x))
library(grid) # needed for arrow function
p1 + geom_text(aes(x = wt, y = height * 0.8, label = x)) +
theme_bw() + labs(x = NULL, y = NULL) +
theme(axis.ticks = element_blank(),axis.text.x = element_blank(),
axis.text.y = element_blank(), legend.position = "none") +
annotate("text", x = 120, y = 83, label = "a Beta block") +
geom_segment(aes(x = 100, y = 80, xend = 80, yend = 75),
arrow = arrow(length = unit(0.5, "cm")))

https://stackoverflow.com/questions/14591167
复制相似问题