想知道如何输出我在《经济学人》杂志上看到的图表
我最近在一位经济学家身上看到了这一点,我想知道是否有人有代码可以帮助使用 ggplot 复制它。 经济学家图表
谢谢!
I saw this in a recent economist and I was wondering if anyone has code that would help replicate it using ggplot. Economist Chart
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我只使用基本绘图功能进行了一些尝试。这是结果:
这是生成它的代码:
它可能不完全匹配(例如 i不知道如何在不直接调用文本的情况下右对齐),并且如果您尝试调整它的大小,文本会跳来跳去,因此您必须进一步调整参数以满足您的需求。但它表明,仅使用 R 中的基本绘图功能就可以走得更远。
编辑:正如评论的那样,白色条纹穿过条形。这是不可避免的,并且无法通过再次调用 barplot 来调整,因为这会重新绘制绘图区域。因此,我们必须查看 barplot 的源代码并为此目的对其进行自定义(喜欢这在 R 中是多么容易)。但现在我们已经脱离了 R 中舒适的基础知识(即使用内置的条形图)。这是另一个尝试:
它创建了这个:
I played around a little using only base plot functionality. This is the result:
Here is the code that produced it:
It might not be the exact match (ex. i don't know how to right align without calling text directly), and if you try to resize it the text will jump around, so you would have to tweak the parameters further to fit your needs. But it goes to show that you can get far with using only basic plot functionality in R.
EDIT: As was commented, the white stripes cross the bars. This is inevitable and cant be adjusted with another call to
barplot
since that would redraw the plot area. Thus we have to take a peek into the source-code ofbarplot
and customize it for this purpose (love how easy this is in R). But now we have moved outside of the comfy basics in R (i.e. using built in barplot). Here is another go at it:Which creates this:
latticeExtra 包有一个主题,其样式《经济学人》 杂志应该有助于作为一个开始。
然而,它使用了lattice,而现在所有的孩子都在嚷嚷着ggplot2 ...
The latticeExtra package has a theme styled after The Economist magazine which should help as a start.
However, that uses lattice whereas all the kids these days clamor for ggplot2 ...
我认为实现 R 图形精确排版的最佳(最快、最简单)方法是使用基本 R 函数来生成主要细节(在本例中使用 ggplot 和 geom_bar ,或基本图形 barplot ),然后将绘图保存到SVG 文件。在您最喜欢的编辑包中打开 SVG(每次使用 Inkscape 都会给我留下更深刻的印象),然后在那里进行最终的排版。
然后,您可以执行一些操作,例如抓取图例并将其四处移动、删除元素、在任何位置以任何格式、任何字体添加文本等。
I think the best (quickest, easiest) way to achieve precise typesetting of an R graphic is to use basic R functions to produce the main detail (use ggplot with geom_bar in this case, or base graphics barplot) and then save the plot to an SVG file. Open the SVG in your favourite editing package (Inkscape impresses me more every time I use it) and then do the final typesetting there.
You can then do things like grab a legend and move it around, delete elements, add text anywhere with any formatting in any font etc.