gpt4 book ai didi

R ggplot : Different geom_ablines in facets

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

我想画两个不同的geom_abline在我的两个方面。这似乎与 geom_hline 的工作方式不同 - 已回答 here .

同时

library(ggplot2)
dummy1 <- expand.grid(X = factor(c("A", "B")), Y = rnorm(10))
dummy1$D <- rnorm(nrow(dummy1))
dummy2 <- data.frame(X = c("A", "B"), Z = c(1, 0))
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_hline(data = dummy2, aes(yintercept = Z))

.. 有效,这个:

library(ggplot2)
dummy1 <- expand.grid(X = factor(c("A", "B")), Y = rnorm(10))
dummy1$D <- rnorm(nrow(dummy1))
dummy2 <- data.frame(X = c("A", "B"), Z = c(1, 0))
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_abline(data = dummy2, aes(yintercept = Z), slope = 1)

不会:

abline using facets

Y 截距不受影响。

最佳答案

对于geom_abline,您需要intercept 而不是yintercept。此外,您需要将这两个值都映射到 aes 中,否则设置 slope 会覆盖您使用 aes 设置的值。这有效:

library(ggplot2)
dummy1 <- expand.grid(X = factor(c("A", "B")), Y = rnorm(10))
dummy1$D <- rnorm(nrow(dummy1))
dummy2 <- data.frame(X = c("A", "B"), Z = c(1, 0))

ggplot(dummy1, aes(x = D, y = Y)) +
geom_point() +
facet_grid(~X) +
geom_abline(data = dummy2, aes(intercept = Z, slope = 1))

关于R ggplot : Different geom_ablines in facets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43696806/

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