gpt4 book ai didi

r - Directlabels包装-标签不适合绘图区域

转载 作者:行者123 更新时间:2023-12-03 18:29:02 25 4
gpt4 key购买 nike

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

我可以想象可能还有其他使用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()

line chart with end labels

根据数据可视化原理,我想避免扩展x轴以使标签合适-这将意味着拥有没有数据的数据空间。相反,我希望标签朝着图表框/面板之外的空白区域延伸(如果可以的话)。

最佳答案

我认为,直接贴标签是可行的方法。实际上,我会将标签放置在行的开头和结尾,并使用expand()为标签创建空间。另请注意,使用标签时,不需要图例。

这类似于herehere的答案。

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

enter image description here

如果您更喜欢将标签插入绘图页边距,则直接标签可以做到这一点。但是,由于标签位于绘图面板的外部,因此需要关闭裁剪功能。
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)

enter image description here

也可以使用 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)

enter image description here

关于r - Directlabels包装-标签不适合绘图区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38088966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com