gpt4 book ai didi

r - 将彩色箭头添加到ggplot2的轴(部分在绘图区域之外)

转载 作者:行者123 更新时间:2023-12-04 16:39:25 29 4
gpt4 key购买 nike

我想添加一个彩色箭头(轴的全长)以显示时间在一个方向上移动(可以假定,但是对于此绘图,没有数值,所以我希望箭头显示方向)。我可以使用geom_segment对其进行绘制,但是在绘制区域之外的部分丢失了。

我看过这篇文章:R & ggplot2: How to get arrows under the axis label?,但是这个解决方案是对轴标题的修改。这篇文章:https://stackoverflow.com/a/10542622/1000343显示了文本区域之外的行,但没有彩色箭头。

MWE

library(ggplot2); library(grid); library(scales)

dat <- data.frame(Time=0:5, y=0:5)

ggplot(dat, aes(x=Time, y=y)) +
geom_area(alpha=.1) + theme_bw() +
scale_y_continuous(expand = c(0, 0)) +
scale_x_continuous(expand = c(0, 0)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()
)

我尝试过的 :
ggplot(dat, aes(x=Time, y=y)) +
geom_area(alpha=.1) + theme_bw() +
scale_y_continuous(expand = c(0, 0)) +
scale_x_continuous(expand = c(0, 0)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()
) +
geom_segment(aes(x=0, xend = 5 , y=0, yend = 0), size=1.5,
arrow = arrow(length = unit(0.6,"cm")))

给予

但我想要

最佳答案

问题似乎只是剪切区域(如here回答)。尝试:

p1<-ggplot(dat, aes(x=Time, y=y)) +
geom_area(alpha=.1) + theme_bw() +
scale_y_continuous(expand = c(0, 0)) +
scale_x_continuous(expand = c(0, 0)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()
) +
geom_segment(aes(x=0, xend = 5 , y=0, yend = 0), size=1.5,
arrow = arrow(length = unit(0.6,"cm")))

gt <- ggplot_gtable(ggplot_build(p1))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

要得到

关于r - 将彩色箭头添加到ggplot2的轴(部分在绘图区域之外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27140058/

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