10

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

我想可能会有另一种解决方案使用annotate或其他一些几何图形。但我想使用直接标签解决问题。请参阅下面的代码和图像。谢谢。

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 轴以使标签适合——这意味着有没有数据的数据空间。相反,我希望标签向图表框/面板之外的空白区域延伸(如果有意义的话)。

4

2 回答 2

13

在我看来,直接标签是要走的路。事实上,我会将标签放置在行首和行尾,使用expand(). 另请注意,使用标签,不需要图例。

这类似于此处此处的答案。

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)

在此处输入图像描述

于 2016-06-30T05:32:03.973 回答
2

由于您没有提供可重现的示例,因此很难说最好的解决方案是什么。但是,我建议尝试手动调整 x 比例。使用“缓冲区”增加绘图区域。

#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() 也可能在这里工作得很好。或者,annotate 或简单的 geom_text 也可以很好地工作。

于 2016-06-29T02:02:06.510 回答