Подтвердить что ты не робот

Нарисуйте хронологическую шкалу времени с помощью 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). Это звучало довольно просто, пока я не попытался его нарисовать.

update: Я не писал его, но вам нужно сделать as.Date("1492", format="%Y") для правильного воспроизведения.

NB: Решения, приведенные ниже, относятся только к событиям, которые происходят в определенную дату, а не к срокам с "периодами" или "эрами".

4b9b3361

Ответ 1

Иногда простейшая графика сложнее создать в 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

Примечание: этот графический файл был полностью создан в построителе построений ggplot2 в Deducer

Ответ 2

Это выглядит как ОК... enter image description here

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.

Ответ 3

Это похоже на лучшую работу для базовой графики 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))

создает что-то вроде этого:

enter image description here