如何在行尾上方的垂直线上放置标签?

时间:2019-12-22 21:53:23

标签: r ggplot2

我用 ggplot 做了一个图。我的目的是在我在图中竖线的顶部添加标签。问题在于这些标签看起来不够高。它们与垂直线相交。相反,我希望他们不要碰垂直线。

例如:

 date <- seq(2000, 2010, by = 1)
 value <- seq(1, 11, by=1)
 df <- data.frame(Date = date, Value = value) 

 ggplot(df, aes(x = df$Date)) + geom_line(aes(y = df$Value, colour = "Value"), size = 1) + theme_classic() + theme(axis.line = element_line(colour = "grey", size = 0.8)) + labs(y = "\n \n", x = "", colour = "") + theme(axis.title = element_text(family = "Times New Roman", face = "plain", size = 12, colour = "black")) +
  geom_vline(xintercept = df$Date[c(3)], linetype="dashed", colour = "black", size=1) + 
  geom_text(aes(x=df$Date[3], y= Inf, label = "Label"), colour = "black", vjust = 1, size = 5) 

如何以不与虚线相交的方式推动标签?

非常感谢您的帮助!

4 个答案:

答案 0 :(得分:2)

使用当前的方法很难做到这一点,因为geom_vline绘制了一条永远在两个方向上连续的线。不过,您可以在向geom_text的呼叫中向右或向左推送文本。如果您确实想使文本位于垂直线的正上方,建议您使用annotate来画线:

library(ggplot2)

date <- seq(2000, 2010, by = 1)
value <- seq(1, 11, by=1)
df <- data.frame(Date = date, Value = value) 

ggplot(df, aes(x = Date)) + 
  geom_line(aes(y = Value, colour = Value), size = 1) + 
  theme_classic() + theme(axis.line = element_line(colour = "grey", size = 0.8)) + 
  labs(y = "\n \n", x = "", colour = "") + 
  theme(
    axis.title = element_text(family = "Times New Roman", face = "plain", size = 12, colour = "black")) +
  geom_text(aes(x=df$Date[3], y=max(df$Value), label = "Label"), colour = "black", vjust = 1, size = 5, nudge_y = 1) +
  annotate("segment", x=df$Date[3], xend=df$Date[3], y=0, yend=max(df$Value), linetype="dashed", size=1)

看上面的最后两行:我在调用nudge_y的过程中使用geom_text来向上移动标签,然后使用annotate来绘制一条固定长度的线。 / p>

我还对代码的其他部分进行了一些小的编辑。值得注意的是,如果列名在您的df$调用中,则不必在列名前加上aes(),并且colour的{​​{1}}自变量应始终用引号引起来(否aes)列名。

答案 1 :(得分:1)

也许有更好的选择,但这是一种方法。

plot.margin中的theme中,在图上方添加额外的边距空间。

添加coord_cartesian以关闭剪切(允许在绘图面板范围之外进行绘图)。

使用annotation_custom添加文本。

library(ggplot2)

date <- seq(2000, 2010, by = 1)
value <- seq(1, 11, by=1)
df <- data.frame(Date = date, Value = value) 

ggplot(df, aes(x = Date)) + 
  geom_line(aes(y = Value, colour = Value), size = 1) + 
  theme_classic() + 
  theme(plot.margin = unit(c(2,1,1,1), "cm"), axis.line = element_line(colour = "grey", size = 0.8)) + 
  labs(y = "\n \n", x = "", colour = "") + 
  theme(axis.title = element_text(family = "Times New Roman", face = "plain", size = 12, colour = "black")) +
  geom_vline(xintercept = df$Date[c(3)], linetype="dashed", colour = "black", size=1) + 
  annotation_custom(grob = textGrob(label = "Label", hjust = .5, gp = gpar(cex = 1.2)),
                    ymin = 12, ymax = 12, xmin = df$Date[c(3)], xmax = df$Date[c(3)]) +
  coord_cartesian(clip = 'off')

ggplot with custom annotation text in margin

答案 2 :(得分:1)

我建议使用geom_segment。我也和变形虫一样对您的代码进行了类似的编辑

ggplot(df, aes(x = Date)) +
  geom_line(aes(y = Value, colour = "Value"), size = 1) + 
  labs(y = "\n \n", x = "", colour = "") + 
  theme_classic() + 
  theme(axis.line = element_line(colour = "grey", size = 0.8),
        axis.title = element_text(family = "Times New Roman", 
                                  face = "plain", 
                                  size = 12, 
                                  colour = "black")) +
  geom_segment(aes(x = df$Date[3],xend=df$Date[3],y=0,yend=max(value*0.97)), 
               linetype="dashed", colour = "black", size=1) + 
  geom_text(aes(x=df$Date[3], y= Inf, label = "Label"), colour = "black", vjust = 1, size = 5)+
  ylim(0,NA)

enter image description here

答案 3 :(得分:1)

也许正在寻找插入geom_segment的地方:

ggplot(df, aes(x = Date)) + 
  geom_line(aes(y = Value, colour = "Value"), size = 1) + 
  geom_segment(aes(x = 2005, y = 0, xend = 2005, yend = 9, colour = "segment"), data = df) + 
  geom_text(aes(x=df$Date[6], y= 10, label = "Label"), colour = "black", vjust = 1, size = 5) 

enter image description here