有条件地用facet_grid更改面板背景? [英] Conditionally change panel background with facet_grid?

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

问题描述

我使用 ggplot2 中的提示数据集。如果我这样做

pre $ $ $ $ c $ sp $ gplot(tips,aes(x = total_bill,y = tip / total_bill))+
geom_point(shape = 1)+
facet_grid(sex〜day)

没问题。但是我现在想在星期五下改变面板背景。有没有办法做到这一点?

更好的是,我可以通过传递参数来有条件地改变颜色吗?例如,如果超过3点低于0.1,那么将面板背景(仅适用于该面板)更改为某种颜色,而所有其他颜色保持为默认浅灰色?

解决方案

ggplot2 中执行任何操作的一般规则是,


  1. 创建一个数据框,用于对要绘制的信息进行编码

  2. 将该数据框传递给geom

在这种情况下,这会变得更复杂一些,因为您想要改变的情节的特定方面。被设计为 ggplot2 的权力以一种将绘图的数据元素(即几何元素)与非数据元素(即主题的)分开的方式,并且恰巧情节背景属于非-data类别。



总是可以选择修改底层网格对象,但这很乏味,细节可能会随 ggplot2 的不同版本而改变。相反,我们将采用哈利在这个问题中提到的黑客。

 #创建一个带有分面变量
#和一些虚拟数据(将被覆盖)的数据框
tp< - unique提示[,c('sex','day')])
tp $ total_bill < - tp $ tip < - 1

#Just Fri
ggplot(tips ,aes(x = total_bill,y = tip / total_bill))+
geom_rect(data = subset(tp,day =='Fri'),aes(fill = day),xmin = -Inf,xmax = Inf ,
ymin = -Inf,ymax = Inf,alpha = 0.3)+
geom_point(shape = 1)+
facet_grid(sex〜day)

 #每个面板
ggplot(tips,aes(x = total_bill,y = tip / total_bill))+
geom_rect(data = tp,aes(fill = day),xmin = -Inf,xmax = Inf,
ymin = -Inf,ymax = Inf,alpha = 0.3) +
geom_point(shape = 1)+
facet_grid(sex〜day)


I'm using the "tips" data set in ggplot2. If I do

sp = ggplot(tips,aes(x=total_bill, y = tip/total_bill)) + 
        geom_point(shape=1) + 
        facet_grid(sex ~ day)

The plot comes out fine. But I now want to change the panel background for just the plots under "Fri". Is there a way to do this?

Even better, can I conditionally change colors by passing parameters? For example if more than 3 points are below 0.1, then change panel background (for just that panel) to a certain color while all others remain the default light grey?

解决方案

The general rule for doing anything in ggplot2 is to,

  1. Create a data frame that encodes the information you want to plot
  2. Pass that data frame to a geom

This is made a bit more complicated in this case because of the particular aspect of the plot you want to alter. The Powers That Be designed ggplot2 in a way that separates data elements of the plot (i.e. geom's) from non-data elements (i.e. theme's), and it so happens that the plot background falls under the "non-data" category.

There is always the option of modifying the underlying grid object manually but this is tedious and the details may change with different versions of ggplot2. Instead, we'll employ the "hack" that Hadley refers to in this question.

#Create a data frame with the faceting variables
# and some dummy data (that will be overwritten)
tp <- unique(tips[,c('sex','day')])
tp$total_bill <- tp$tip <- 1

#Just Fri
ggplot(tips,aes(x=total_bill, y = tip/total_bill)) + 
        geom_rect(data = subset(tp,day == 'Fri'),aes(fill = day),xmin = -Inf,xmax = Inf,
            ymin = -Inf,ymax = Inf,alpha = 0.3) +
        geom_point(shape=1) + 
        facet_grid(sex ~ day) 

#Each panel
ggplot(tips,aes(x=total_bill, y = tip/total_bill)) + 
        geom_rect(data = tp,aes(fill = day),xmin = -Inf,xmax = Inf,
            ymin = -Inf,ymax = Inf,alpha = 0.3) +
        geom_point(shape=1) + 
        facet_grid(sex ~ day) 

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

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