我有像
这样的数据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")
才能正确重现。
注意:以下解决方案仅处理在特定日期发生的事件,而不是处理“句号”或“时代”的时间表。
答案 0 :(得分:12)
有时最简单的图形在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())
注意:此图片完全在Deducer
中的ggplot2 Plot Builder中生成答案 1 :(得分:6)
这看起来很好......
dislocations <- c(-1,1,-.5)
ggplot( data )
+ geom_text( aes(x = V1, y=dislocations, label = V2), position="jitter" )
+ geom_hline( yintercept=0, size=1, scale="date" )
+ geom_segment( aes(x = V1, y=dislocations, xend=V1, yend=0, alpha=.7 ))
但它仍缺少正确的“时间箭头”,背景看起来不正确,并在y
轴上标记了值。
答案 2 :(得分:5)
对于R的基本图形来说这似乎是一个更好的工作(实际上,这种东西可能更适合像Illustrator这样的工具或类似的东西)。
dat = as.data.frame(rbind(c("1492", "Columbus sailed the ocean blue"),
c("1976", "Americans listened to Styx"),
c("2008", "Financial meltdown")))
dat$V1 <- as.Date(dat$V1,"%Y")
dat$val <- c(-1,1,-0.5)
plot(dat$V1,dislocations, type = "n",xaxt = "n",bty = "n",
xlab = "Time", ylab = "Dislocations")
u <- par("usr")
arrows(u[1], 0, u[2], 0, xpd = TRUE)
points(dat$V1,dat$val,pch = 20)
segments(dat$V1,c(0,0,0),dat$V1,dat$val)
text(x=dat$V1,y=dat$val,labels=dat$V2,pos=c(4,2,2))
产生这样的东西:
答案 3 :(得分:3)
以上ggplot2
版本的一小部分变体,使用geom_lollipop
中的ggalt
并使用cowplot
作为精美的背景主题。重要的是将图形高度设置为漂亮,将较小的宽度设置为较小(在我的RMarkdown块中,我有fig.height = 3
和fig.width = 10
)
我还使用了来自this question的(略微改编的)函数,它有助于移动x轴(改编后的函数使用annotate
而非geom_hline
。这样我就可以了添加箭头)。
道歉,我在这里使用了我自己的数据,原因很简洁。我需要重新开始工作!!
library(ggplot2)
library(dplyr)
library(ggalt)
library(cowplot)
library(tibble)
library(lubridate)
#Create data to plot
data <- tribble( ~start_date, ~event, ~displ,
ymd(20160201), "Initial meeting with Renfrewshire", 1,
ymd(20160430), "UBDC RAC submission", 0.7,
ymd(20160524), "College Ethics Approval", 0.5,
ymd(20160601), "UBDC RAC approval", -0.5,
ymd(20161101), "Agreeement in Principal", 0.3,
ymd(20170906), "DSA signed", 0.5,
ymd(20170921), "Data transferred", -0.5,
ymd(20180221), "Analysis complete", 0.5)
#Function to shift x-axis to 0 adapted from link shown above
shift_axis <- function(p, xmin, xmax, y=0){
g <- ggplotGrob(p)
dummy <- data.frame(y=y)
ax <- g[["grobs"]][g$layout$name == "axis-b"][[1]]
p + annotation_custom(grid::grobTree(ax, vp = grid::viewport(y=1, height=sum(ax$height))),
ymax=y, ymin=y) +
annotate("segment", y = 0, yend = 0, x = xmin, xend = xmax,
arrow = arrow(length = unit(0.1, "inches"))) +
theme(axis.text.x = element_blank(),
axis.ticks.x=element_blank())
}
#Conditionally set whether text will be above or below the point
vjust = ifelse(data$displ > 0, -1, 1.5)
#plot
p1 <- data %>%
ggplot(aes(start_date, displ)) +
geom_lollipop(point.size = 1) +
geom_text(aes(x = start_date, y = displ, label = event), data = data,
hjust = 0, vjust = vjust, size = 2.5) +
theme(axis.title = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line = element_blank(),
axis.text.x = element_text(size = 8)) +
expand_limits(x = c(ymd(20151201), ymd(20180501)), y = 1.2) +
scale_x_date(breaks = scales::pretty_breaks(n = 9))
#and run the function from above
timeline <- shift_axis(p1, ymd(20151201), ymd(20180501))
...可生产