ggplot时间轴 - 希望数据垂直显示

时间:2016-10-20 02:00:35

标签: r ggplot2

这与earlier post

有关

给出的例子是美国和德国总统。

当我使用ggplot创建自己的时间轴时。时间线标签重叠。请帮忙。我要么喜欢沿x轴的间距更长(我的事件只发生一天),或者水平线上方和下方交替的标签或倾斜45度的标签。

这就是我所拥有的:

x <- data.frame(Entity = rep(c("Samsung", "Consumer", "Other"), each=3),
                event = c("Note7 unveiled", "Sales in 10 markets", 
                          "First battery fire", "Delay of Note7 shipped",
                          "Note7 China sales", "Global recall",
                          "FAA warns passengers", "Formal recall",
                          "Samsung being sued re burns"), 
                Year2016 = as.Date(c("2016-08-02", "2016-08-19", "2016-08-24", 
                                     "2016-08-31", "2016-09-01", "2016-09-02", 
                                     "2016-09-08","2016-09-15", "2016-09-16")))
library(ggplot2)

ggplot(x, aes(x=Year2016, y=Entity)) + 
geom_line() + 
geom_point() + 
geom_text(aes(label=event), hjust=0, vjust=0) + 
xlim(c(min(x$Year2016), max(x$Year2016)+10*0.25)) 

这是情节:enter image description here

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

正如hrbrmstr建议的那样,ggrepel包在这种情况下可以很好地工作。你想尝试这样的事情。

library(ggplot2)
library(ggrepel)

ggplot(x, aes(x=Year2016, y=Entity)) +
geom_line() +
geom_point() +
geom_text_repel(aes(label = event),
                segment.size = 0.5,
                size = 2,
                arrow = arrow(length = unit(0.02, 'npc')),
                nudge_x = -1,
                nudge_y = 0.5) +
xlim(c(min(x$Year2016), max(x$Year2016)+10*0.25)) 

enter image description here