首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >想知道如何输出我在《经济学人》杂志上看到的图表

想知道如何输出我在《经济学人》杂志上看到的图表
EN

Stack Overflow用户
提问于 2010-10-15 06:31:51
回答 4查看 3K关注 0票数 16

我在最近的一位经济学家中看到了这一点,我想知道是否有人有代码可以帮助使用ggplot复制它。Economist Chart

谢谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-10-16 03:06:35

我只是使用了一些基本的绘图功能。这就是结果:

下面是生成它的代码:

代码语言:javascript
复制
bigmacprice <- data.frame(
    country = c("Switzerland", "Brazil", "Euro area",
        "Canada", "Japan", "United States",
        "Britain", "Singapore", "South Korea",
        "South Africa", "Mexico", "Thailand",
        "Russia", "Malaysia", "China"),
    price = c(6.78, 5.26, 4.79, 4.18, 3.91, 3.71,
              3.63, 3.46, 3.03, 2.79, 2.58, 2.44,
              2.39, 2.25, 2.18)
)


plotbigmac <- function(mac, base = "United States", xlim = c(-40, 100)) {
    mac <- mac[order(mac$price),]
    base = which(mac$country == base)
    height <- (mac$price / mac[base, "price"] - 1) * 100
    par(bg = "#d0e0e7", col.main = "#262324", col.axis = "#393E46",
        mar = c(8, 8, 6, 6), las = 1)
    barplot(height, width = .1, space = .4,
        names.arg = mac$country, #cex.names = .8,
        col = "#01516c", border = "#7199a8", # border = "#577784",
        horiz = TRUE, xlim = c(-40, 100), axes = FALSE)
    axis(3, lty = 0)
    title(main = "Bunfight\nBig Mac index", col = "#393E46")

    abline(v = seq(-100, 100, 10), col = "white", lwd = 2)
    abline(v = 0, col = "#c8454e", lwd = 2)
    par(xpd = TRUE)
    for (i in 1:nrow(mac)) {
        rect(105, (i - 1) / 7, 118, i / 7 - 0.05,
        col = "white", border = "#7199a8")
        text(112, (i - 1) / 7 + 0.05, mac$price[i], cex = 0.8, col = "#393E46")
    }
    rect(-120, 2.5, -90, 3, col = "#c8454e", border = "#c8454e")
    text(-68, -.2, "Sources:", col = "#393E46")
    text(-64, -.3, "McDonald's;", col = "#393E46")
    text(-60, -.4, "The Economist", col = "#393E46")
}

plotbigmac(bigmacprice)

它可能不完全匹配(例如,我不知道如何在不直接调用文本的情况下右对齐),如果你试图调整它的大小,文本会跳来跳去,所以你必须进一步调整参数来满足你的需要。但它表明,在R中只使用基本的绘图功能是可以做到的。

编辑:正如所评论的,白色条纹横跨条形图。这是不可避免的,并且不能通过再次调用barplot来调整,因为这将重新绘制绘图区域。因此,我们必须看一眼barplot的源代码,并为此对其进行定制(喜欢在R中实现这一点是多么容易)。但现在我们已经超越了R中的comfy基础(即使用内置的barplot)。这是另一个尝试:

代码语言:javascript
复制
plotBigMac <- function(mac, base = "United States") {
    old.par <- par(no.readonly = TRUE)
    on.exit(par(old.par))
    # Create data:
    mac <- mac[order(mac$price),]
    base = which(mac$country == base)
    height <- (mac$price / mac[base, "price"] - 1) * 100
    # Costume 'barplot'
    NN <- length(height)
    width <- rep(1, length.out = NN)
    delta <- width / 2
    w.r <- cumsum(width + 0.5)
    w.m <- w.r - delta
    w.l <- w.m - delta
    xlim <- c(range(-.01 * height, height)[1], 100)
    ylim <- c(min(w.l), max(w.r))
    par(bg = "#d0e0e7", col.main = "#262324", col.axis = "#393E46",
        mar = c(8, 8, 6, 6), las = 1, cex = 0.9)
    plot.new()
    plot.window(xlim, ylim)
    abline(v = seq(-100, 100, 20), col = "white", lwd = 2)
    rect(0, w.l, height, w.r, col = "#01516c", border = "#7199a8", lwd = 1)

    # Lines and axis
    abline(v = 0, col = "#c8454e", lwd = 2)
    axis(3, axTicks(3), abs(axTicks(3)), lty = 0)
    axis(2, labels = mac$country, at = w.m, lty = 0)

    # Move outside of plot area
    par(xpd = TRUE)

    # Text misc.
    text(5, (w.l[base] + w.r[base]) / 2, "nil", font = 3)
    text(8, w.r[NN] + 2.3, "+")
    text(-8, w.r[NN] + 2.3, "-")

    # Create price boxes:
    rect(105, w.l, 125, w.r,
        col = "white", border = "#7199a8", lwd = 1)
    text(115, (w.r + w.l)/2, mac$price, cex = 0.8, col = "#393E46")

}

这将创建以下内容:

票数 24
EN

Stack Overflow用户

发布于 2010-10-15 07:33:35

latticeExtra包有一个模仿The Economist杂志的主题,这应该是一个很好的开始。

然而,这使用了lattice,而现在所有的孩子都在叫嚣着要ggplot2……

票数 9
EN

Stack Overflow用户

发布于 2010-10-15 14:40:22

我认为实现R图形精确排版的最好(最快、最简单)方法是使用基本的R函数来生成主要细节(在本例中使用geom_bar或基本图形barplot),然后将绘图保存到SVG文件中。在您最喜欢的编辑包中打开SVG (Inkscape每次使用它都会给我留下更深刻的印象),然后在那里进行最终的排版。

然后你可以做一些事情,比如抓取一个图例并移动它,删除元素,以任何字体在任何地方添加任何格式的文本等等。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3938126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档