gpt4 book ai didi

r - 如何控制ggplot中轴线的长度?

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

这个问题在这里已经有了答案:





How to tweak the extent to which an axis is drawn in ggplot2? [duplicate]

(2 个回答)


5年前关闭。




这个问题不是关于控制轴限制(我认为),而是关于控制轴线本身的长度。我正在尝试制作一个类似于手动添加轴的基本 R 图的 ggplot(以便我可以利用其良好的分面功能)。这些轴仅延伸至最后一个轴标签。

一些设置数据和我试图模仿的基本 R 类型的图:

library("ggplot2")
library("cowplot")
library("grid")

set.seed(5)
x <- rnorm(10)
y <- rnorm(10)
D <- data.frame(x, y)

plot(x, y, axes = FALSE)
axis(1)
axis(2)

Base R plot
ggplot(D, aes(x, y)) +
geom_point()

默认更改为 ggplot 从 cowplot包非常接近:

enter image description here

但是我怎么能告诉 ggplot 只绘制到最后一个轴标签的线,即使点位于该值之外(如在基本 R 图中)?

最佳答案

好吧,@Gregor 在我研究这个时发表了他的评论,所以这是实现。仅出于说明目的,带注释的轴以红色呈现,以使其明显与标准轴线分开添加。如果您要制作一堆这样的图,您还可以添加一些逻辑来以编程方式确定 annotate 的 x 和 y 限制。和 coord_cartesian .

  my_theme = list(theme_bw(),
theme(panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank()),
labs(y="",x=""))

ggplot(D, aes(x,y)) +
geom_point() +
coord_cartesian(xlim=c(-1.4,1.4), ylim=c(-2.4,1.4)) +
my_theme +
annotate(x=-1.4, xend=-1.4, y=-2, yend=1, colour="red", lwd=0.75, geom="segment") +
annotate(x=-1, xend=1, y=-2.4, yend=-2.4, colour="red", lwd=0.75, geom="segment")

enter image description here

关于r - 如何控制ggplot中轴线的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33699650/

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