繁体   English   中英

R标签中的Directlabels包不适合绘图区域

[英]Directlabels package in R — labels do not fit in plot area

我想用ggplot探索directlabels包。 我试图在简单折线图的端点处绘制标签; 但是,标签面板会剪切标签。

我想可能有另一个解决方案使用annotate或其他一些geoms。 但我想用直接标签来解决。 请参阅下面的代码和图片。 谢谢。

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

带有结束标签的折线图

谢谢尼克(以下回复)。 xlim解决方案有效。

但是,我认为它在数据可视化原理上我想避免扩展x轴以使标签适合 - 这意味着数据空间没有数据。 相反,我希望标签向图表框/面板之外的白色空间延伸(如果这是有道理的)。

为了增加背景,我打算在一个情节中绘制大约10个金融时间序列,我认为直接标签是最好的解决方案,不是吗?

在我看来,直接标签是要走的路。 实际上,我会在行的开头和末尾放置标签,使用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)

在此输入图像描述

由于您没有提供可重现的示例,因此很难说最佳解决方案是什么。 但是,我建议尝试手动调整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()可能也适用于此处。 或者,注释或简单的geom_text也可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM