How to add label to geom_segment at the start of the segment?

浪尽此生 提交于 2021-02-18 06:25:31

问题


I'm sure this is simple but I can't figure it out.

I have the following chart:

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")

But I would like the labels to be off the axis and on the page. Either to the left or on top of the middle of the line. E.g.

Most of the examples of geom_text give me gobbledeegook. I can't seem to apply them to the factor data I have here. Do you know how to do this? Thank you


回答1:


ggplot() +
  geom_segment(data=cambodia, aes(x=StartDate, xend=EndDate, y=Period, yend=Period, color=Color), 
               linetype=1, size=2) +
  geom_label(data=cambodia, aes(x=StartDate, y=Period,  label = Period),
             nudge_x = c(-300, -200, -200, -100)) +
  scale_colour_brewer(palette = "Pastel1")+
  xlab("Date")+
  ylab("")+
  theme_bw() + 
  theme(legend.position="none") +
  theme(aspect.ratio = .2) +
  theme(panel.grid.minor = element_blank(), panel.grid.major =   element_blank(),
        axis.line.y = element_blank(), axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

You need to use element_blank() to remove the y axis elements and then use nudge_x argument in geom_label to offset the labels appropriately.




回答2:


Having the labels on the end of the segments might distort the visual mapping of segment length and location to year-range. You could put the labels in the middle of the segments instead.

library(data.table)
library(magrittr)
library(ggplot2)
library(stringr)

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(cambodia, aes(x=StartDate, xend=EndDate, y=Period, colour=Period)) +
  geom_segment(aes(xend=EndDate, yend=Period), linetype=1, size=2) +
  geom_label(aes(label=str_wrap(Period,12), x=(StartDate + EndDate)/2), size=3) +
  scale_colour_brewer(palette = "Set1") +
  xlab("Date")+ ylab("Ruler")+
  theme_bw() + 
  theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(), 
        aspect.ratio = .2,
        legend.position="none",
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

Or what about going minimal:

ggplot(cambodia, aes(x=StartDate, y=1)) +
  geom_rect(aes(xmin=StartDate, xmax=EndDate, ymin=0.97, ymax=1.03, fill=Period), 
            show.legend=FALSE, colour="white", size=0.5) +
  geom_label(aes(label=str_wrap(Period,12), x=(StartDate + EndDate)/2), size=3.5) +
  geom_text(aes(label=StartDate, y=0.96), size=3.5) +
  geom_text(aes(label=ifelse(EndDate==max(EndDate), EndDate,""), x=EndDate, y=0.96), size=3.5) +
  scale_colour_brewer(palette = "Set1") +
  scale_y_continuous(limits=c(0.95,1.05)) +
  theme_void() 



来源:https://stackoverflow.com/questions/45112019/how-to-add-label-to-geom-segment-at-the-start-of-the-segment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!