GGplot2中面板背景的条件格式 [英] Conditional formatting of panel background in GGplot2

查看:259
本文介绍了GGplot2中面板背景的条件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种直接方式将ggplot构面面板中的回归线的斜率与该面板的背景颜色相链接(即在大网格中将负斜率的正斜率视觉分离)。

我理解如何在GGplots中添加回归线 - 正如在R中用qplot添加回归线到facet_grid



如果您之前已将此信息添加到原始数据框中,我也知道如何更改背景 - 正如有条件地改变面板背景与facet_grid?



然而 - 有没有办法做到这一点在geom_rect公式中没有必须例如分别运行回归,将它们绑定到原始数​​据框,然后将其用作geom_rect()的变量? geom_rect()使用stat_smooth()中的信息吗?



Wouter



以下问题的简单回归曲线图:

  library(ggplot2)
x< - rnorm(100)$ (c(rep(A,50),rep(B,50)))$($) b $ b f2 < - as.factor(rep(c(rep(C,25),rep(D,25)),2))
df < - data.frame(cbind (x,y))
df $ f1 < - f1
df $ f2 < - f2

ggplot(df,aes(x = x,y = y) )+ geom_point()+ facet_grid(f1〜f2)+ stat_smooth(method =lm,se = FALSE)


解决方案

这不完全是解决方案,而是解决方法。但它似乎已经很好。链接到的两个帖子都有解决方案的各个部分。 James的解决方案告诉你如何从 stat_smooth Joran的解决方案告诉您如何使用 geom_rect



 #生成数据:set.seed用于重现性
#我还将乘法常数改为0.1以使
#至少有一个负斜率。
$ b要求(ggplot2)
set.seed(12)
x < - rnorm(100)
y < - + .1 * x + rnorm(100)
f1 < - as.factor(c(rep(A,50),rep(B,50)))
f2 < - as.factor(rep(c(rep (C,25),rep(D,25)),2))
df < - data.frame(cbind(x,y))
df $ f1 < f1
df $ f2 < - f2

#先以这种方式产生你的情节,然后从詹姆斯的帖子中运行它
#,部分outfit = fit < ..y ..将存储
#拟合值的输出在拟合中

g < - ggplot(df,aes(x = x,y = y))+ geom_point( )+ facet_grid(f1〜f2)
g< -g + stat_smooth(aes(outfit = fit< -... y ..),method =lm,se = FALSE)
#现在运行g来生成fit
g

#现在提取每个方面的斜率,
#构造geom_rect的data.frame(按照Joran的帖子)
#编辑:只需添加更多关于适合的信息。默认情况下,它包含每个方面的
#80值。因此,80 * 4 = 320

斜率 (tp,斜率=斜率,x = 1,y = 1)
tp < - c('f1','f2')])
tp< $ pos_neg< - ifelse(斜率> 0,1,0)
tp $ pos_neg< - 系数(tp $ pos_neg)

#现在再次绘图(但带有geom_rect)$ (数据= tp,aes(填充= pos_neg),xmin = -Inf,xmax = -bf),其中b =bg≤ggplot(df,aes(x = x,y = y))
g < Inf,ymin = -Inf,ymax = Inf,alpha = 0.5)
g <-g + geom_point()+ facet_grid(f1〜f2)+ stat_smooth(method =lm,se = FALSE)
g

输出看起来像。我不确定这是否是你期望的。尽管如此,严格地说,你计算两次拟合的值,但是这两次你用 stat_smooth 隐式计算它。就像我说的,它只是一个解决方法。


I was wondering whether there is a "direct" manner to link the slope of a regression line in a ggplot facet panel to the background colour of that panel (i.e. to visually seperate positive slopes from negative slopes in a large grid).

I understand how to add a regression line in GGplots - as was well explained on Adding a regression line to a facet_grid with qplot in R

I also understand how to change the background if you have previously added this information to the original dataframe - as explained on Conditionally change panel background with facet_grid?

However - is there a way to do this "in the geom_rect" formula without having to e.g. run the regression seperately, bind them to the original dataframe, and then use this as a variable for geom_rect()? is there a way for geom_rect() to use the information from stat_smooth()?

Wouter

good example of a simple regression line plot from earlier question:

library(ggplot2)
x <- rnorm(100)
y <-  + .7*x + rnorm(100)
f1 <- as.factor(c(rep("A",50),rep("B",50)))
f2 <- as.factor(rep(c(rep("C",25),rep("D",25)),2))
df <- data.frame(cbind(x,y))
df$f1 <- f1
df$f2 <- f2

ggplot(df,aes(x=x,y=y))+geom_point()+facet_grid(f1~f2)+stat_smooth(method="lm",se=FALSE)

解决方案

This is not exactly a solution, but a work-around. But it seems to have come out good. Both the posts you linked to had each part of the solution. James' solution here tells you how to extract the fitted values from stat_smooth. Joran's solution here tells how to use geom_rect to fill the background.

# generating data: Usage of set.seed for reproducibility 
# also I changed the multiplication constant to 0.1 to have 
# at least one negative slope.

require(ggplot2)
set.seed(12)
x <- rnorm(100)
y <-  + .1*x + rnorm(100)
f1 <- as.factor(c(rep("A",50),rep("B",50)))
f2 <- as.factor(rep(c(rep("C",25),rep("D",25)),2))
df <- data.frame(cbind(x,y))
df$f1 <- f1
df$f2 <- f2

# first generate your plot in this manner and run it
# from James' post, the part outfit=fit<<-..y.. will store 
# the output of fitted values in "fit"

g <- ggplot(df,aes(x=x,y=y)) + geom_point()+facet_grid(f1~f2) 
g <- g + stat_smooth(aes(outfit=fit<<-..y..), method="lm",se=FALSE)
# now run g to generate "fit"
g

# now extract the slope for each facet and 
# construct the data.frame for geom_rect (as per Joran's post)
# Edit: Just to add more info about "fit". By default it contains
# 80 values per facet. Hence the 80*4 = 320

slopes <- fit[seq(2, 320, by = 80)] - fit[seq(1, 320, by = 80)]
tp <- unique(df[, c('f1', 'f2')])
tp <- transform(tp, slopes=slopes, x=1, y=1)
tp$pos_neg <- ifelse(slopes > 0, 1, 0)
tp$pos_neg <- factor(tp$pos_neg)

# now plot again (but with geom_rect)
g <- ggplot(df,aes(x=x,y=y)) 
g <- g + geom_rect(data = tp, aes(fill = pos_neg), xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.5) 
g <- g + geom_point() + facet_grid(f1~f2) + stat_smooth(method = "lm",se = FALSE)
g

The output looks like . I'm not sure if this is what you expect though.. Strictly speaking, you do calculate the fitted values twice, but both times you calculate it implicitly with stat_smooth. Like I said, its just a work-around.

这篇关于GGplot2中面板背景的条件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆