使用ggplot2绘制时间线
我有类似的数据
data = as.data.frame( rbind( c("1492", "Columbus sailed the ocean blue"),
c("1976", "Americans listened to Styx"),
c("2008", "financial meltdown. great.")
))
,我想在 ggplot2 中构建一个图,它将显示时间箭头 aes(x=$V1) 和文本 aes(label =$V2)。听起来很简单,直到我尝试画它。
更新:我没有写它,但您需要执行 as.Date("1492", format="%Y")
才能正确重现。
注意:下面给出的解决方案仅处理在特定日期发生的事件,而不是具有“时期”或“时代”的时间线。
I have data like
data = as.data.frame( rbind( c("1492", "Columbus sailed the ocean blue"),
c("1976", "Americans listened to Styx"),
c("2008", "financial meltdown. great.")
))
and I want to build a plot in ggplot2
that will display an arrow for time aes(x=$V1)
and text for aes(label=$V2)
. It sounded pretty simple until I tried to draw it.
update: I didn't write it but you need to do as.Date("1492", format="%Y")
to reproduce correctly.
NB: Solutions given below only deal with events that occur at a specific date -- not timelines with "periods" or "eras".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有时,最简单的图形是最难在 ggplot2 中创建的,但这是可能的(而且很漂亮)。
注意:该图形完全是在 演绎器
Sometimes the simplest graphics are the most difficult to create in ggplot2, but it is possible (and pretty).
Note: this graphic was produced entirely in the ggplot2 Plot Builder in Deducer
上面的 ggplot2 版本有一点变化,使用了 ggalt 中的 geom_lollipop 并使用了 cowplot 来制作漂亮的背景主题。重要的是,将图形高度设置为漂亮且较小且宽度较长(在我的 RMarkdown 块中,我有
fig.height = 3
和fig.width = 10
)我已经还使用了 这个问题有助于移动x轴(改编后的函数使用
annotate
而不是geom_hline
。这允许我添加箭头)。抱歉,为了简洁起见,我在这里使用了自己的数据。我需要回去工作!
产生....
A little variation of the
ggplot2
version above making use ofgeom_lollipop
fromggalt
and usingcowplot
for the nice background theme. Important to set the figure height as nice and small with a longer width (in my RMarkdown chunk I havefig.height = 3
andfig.width = 10
)I've also used a (slightly adapted) function from this question which helps move the x-axis (the adapted function uses
annotate
rather thangeom_hline
. This allows me to add an arrow).Apologies, I have used my own data here for reasons of brevity. I need to get back to work!!
Produces....
这看起来还不错...
但它仍然缺少适当的“时间箭头”,背景看起来不正确,并且有标签
y
轴上的值。This looks kind of OK...
but it still lacks a proper "time arrow", the background doesn't look right, and it labels values on the
y
axis.对于 R 的基础图形来说,这似乎是更好的工作(实际上,这种东西可能更适合 Illustrator 或类似工具)。
产生如下内容:
This seems like a better job for R's base graphics (really, this kind of thing probably better fits with a tool like Illustrator or something of that ilk).
produces something like this: