Directlabels 包——标签不适合绘图区域

Directlabels package-- labels do not fit in plot area

我想用 ggplot 探索 directlabels 包。我正在尝试在简单折线图的端点绘制标签;但是,标签被绘图面板剪裁了。 (我打算在一个图中绘制大约 10 个金融时间序列,我认为 directlabels 将是最好的解决方案。)

我想可能有另一种解决方案使用 annotate 或其他一些 geom。但我想使用 directlabels 来解决这个问题。请参阅下面的代码和图像。谢谢。

library(ggplot2)
library(directlabels)
library(tidyr)

#generate data frame with random data, for illustration and plot:
x <- seq(1:100)
y <- cumsum(rnorm(n = 100, mean = 6, sd = 15))
y2 <- cumsum(rnorm(n = 100, mean = 2, sd = 4))
data <- as.data.frame(cbind(x, y, y2))
names(data) <- c("month", "stocks", "bonds")
tidy_data <- gather(data, month)
names(tidy_data) <- c("month", "asset", "value")
p <- ggplot(tidy_data, aes(x = month, y = value, colour = asset)) + 
geom_line() + 
geom_dl(aes(colour = asset, label = asset), method = "last.points") + 
theme_bw()

关于数据可视化原则,我想避免扩展 x 轴以使标签适合——这意味着有数据 space 而没有数据。相反,我希望标签向图表 box/panel 之外的白色 space 延伸(如果这有意义的话)。

由于您没有提供可重现的示例,因此很难说最佳解决方案是什么。但是,我建议尝试手动调整 x 比例。使用"buffer"增加绘图面积。

#generate data frame with random data, for illustration and plot:
p <- ggplot(tidy_data, aes(x = month, y = value, colour = asset)) + 
geom_line() + 
geom_dl(aes(colour = asset, label = asset), method = "last.points") + 
theme_bw() +
xlim(minimum_value, maximum_value + buffer)

如果您想使用直接标签包,使用 scale_x_discrete() 或 scale_x_continuous() 也可能在这里工作得很好。或者,注释或简单的 geom_text 也可以很好地工作。

在我看来,直接标签是可行的方法。事实上,我会在行的开头和结尾放置标签,使用 expand() 为标签创建 space。另请注意,有了标签,就不需要图例了。

这与答案 and here 相似。

library(ggplot2)
library(directlabels)
library(grid)
library(tidyr)

x <- seq(1:100)
y <- cumsum(rnorm(n = 100, mean = 6, sd = 15))
y2 <- cumsum(rnorm(n = 100, mean = 2, sd = 4))
data <- as.data.frame(cbind(x, y, y2))
names(data) <- c("month", "stocks", "bonds")
tidy_data <- gather(data, month)
names(tidy_data) <- c("month", "asset", "value")

ggplot(tidy_data, aes(x = month, y = value, colour = asset, group = asset)) + 
     geom_line() + 
     scale_colour_discrete(guide = 'none')  + 
     scale_x_continuous(expand = c(0.15, 0)) +
     geom_dl(aes(label = asset), method = list(dl.trans(x = x + .3), "last.bumpup")) +
     geom_dl(aes(label = asset), method = list(dl.trans(x = x - .3), "first.bumpup")) + 
     theme_bw() 

如果您更喜欢将标签推入图边距,直接标签即可。但是因为标签位于绘图面板之外,所以需要关闭裁剪。

p1 <- ggplot(tidy_data, aes(x = month, y = value, colour = asset, group = asset)) + 
     geom_line() + 
     scale_colour_discrete(guide = 'none')  + 
     scale_x_continuous(expand = c(0, 0)) +
     geom_dl(aes(label = asset), method = list(dl.trans(x = x + .3), "last.bumpup")) +
     theme_bw() +
     theme(plot.margin = unit(c(1,4,1,1), "lines")) 

# Code to turn off clipping
gt1 <- ggplotGrob(p1)
gt1$layout$clip[gt1$layout$name == "panel"] <- "off"
grid.draw(gt1)

这个效果也可以使用geom_text(可能还有annotate)来实现,也就是说,不需要直接标注。

p2 = ggplot(tidy_data, aes(x = month, y = value, group = asset, colour = asset)) +
  geom_line() + 
  geom_text(data = subset(tidy_data, month == 100), 
      aes(label = asset, colour = asset, x = Inf, y = value), hjust = -.2) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_colour_discrete(guide = 'none')  +  
  theme_bw() +  
  theme(plot.margin = unit(c(1,3,1,1), "lines"))  

# Code to turn off clipping
gt2 <- ggplotGrob(p2)
gt2$layout$clip[gt2$layout$name == "panel"] <- "off"
grid.draw(gt2)