R ggplot:facet中的不同geom_ablines [英] R ggplot: Different geom_ablines in facets
本文介绍了R ggplot:facet中的不同geom_ablines的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想绘制两个不同的
Y截距不受影响。
而不是 yintercept
。此外,您需要映射 aes
中的两个值,否则设置 slope
将覆盖您使用 AES
。这样做: $ b
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(截距= Z,斜率= 1))
I want to draw two different geom_abline
s in my two facets. Which seem to be working differently to the geom_hline
- which is answered here.
While
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))
.. works, this:
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)
does not:
Y-intercept is not affected.
解决方案
For geom_abline
you need intercept
and not yintercept
. Furthermore, you need to map both values inside aes
, otherwise setting slope
overrides what you set with aes
. This works:
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:facet中的不同geom_ablines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文