gpt4 book ai didi

r - 使用 ggplot2 绘制按时间顺序排列的时间线

转载 作者:行者123 更新时间:2023-12-03 21:20:18 25 4
gpt4 key购买 nike

我有这样的数据

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")正确再现。

注意:下面给出的解决方案只处理发生在特定日期的事件——而不是带有“时期”或“时代”的时间线。

最佳答案

有时最简单的图形在 ggplot2 中最难创建,但它是可能的(而且很漂亮)。

data =data.frame( V1=c(1492,1976,2008),V2=c("Columbus sailed the ocean blue","Americans listened to Styx","financial meltdown"),disloc=c(-1,1,-.5))
dev.new()
ggplot() +
geom_segment(aes(x = V1,y = disloc,xend = V1),data=data,yend = 0) +
geom_segment(aes(x = 900,y = 0,xend = 2050,yend = 0),data=data,arrow = arrow(length = unit(x = 0.2,units = 'cm'),type = 'closed')) +
geom_text(aes(x = V1,y = disloc,label = V2),data=data,hjust = 1.0,vjust = 1.0,parse = FALSE) +
geom_point(aes(x = V1,y = disloc),data=data) +
scale_x_continuous(breaks = c(1492,1976,2008),labels = c("1492","1976","2008")) +
theme_bw() +
opts(axis.text.x = theme_text(size = 12.0,angle = 90.0),axis.text.y = theme_blank(),axis.ticks = theme_blank(),axis.title.x = theme_blank(),axis.title.y = theme_blank())

enter image description here

注意:此图形完全在 Deducer 中的 ggplot2 Plot Builder 中生成

关于r - 使用 ggplot2 绘制按时间顺序排列的时间线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7492274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com