我敢肯定,這是簡單的,但我無法弄清楚。如何添加標籤,在該段的開始geom_segment?
我有以下圖表:
library(data.table)
library(magrittr)
library(ggplot2)
cambodia <-
data.table(Period = c("Funan", "Chenla/Zhenla","Khmer Empire","Dark Ages of Cambodia"),
StartDate = c(-500,550,802,1431),
EndDate = c(550,802,1431,1863),
Color = c("lightblue","lightgreen","lightyellow","pink")) %>%
extract(order(-StartDate)) %>%
extract(, Period := factor(Period,levels = Period))
ggplot() +
geom_segment(data=cambodia, aes(x=StartDate, xend=EndDate, y=Period, yend=Period, color=Color),
linetype=1, size=2) +
scale_colour_brewer(palette = "Pastel1")+
xlab("Date")+
ylab("Ruler")+
theme_bw() +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +
theme(aspect.ratio = .2) +
theme(legend.position="none")
但我想的標籤是斷軸,並在網頁上。可以在行的左邊或中間的頂部。例如。
大多數geom_text的例子給我gobbledeegook。我似乎無法將它們應用於我在這裏的因子數據。你知道如何做到這一點? 謝謝
可避免硬編碼微移用類似如下:'GEOM_LABEL(AES(X = StartDate - 0.005 *(max(EndDate) - min(StartDate)), y = Period,label = Period),hjust = 1)' – eipi10