简体   繁体   English

在ggplot2中仅将stat_smooth添加到1个facet中

[英]Adding stat_smooth in to only 1 facet in ggplot2

I have some data for which, at one level of a factor, there is a significant correlation. 我有一些数据,在一个因素的一个层面上,存在显着的相关性。 At the other level, there is none. 在另一个层面,没有。 Plotting these side-by-side is simple. 并排绘制这些很简单。 Adding a line to both of them with stat_smooth, also straightforward. 使用stat_smooth向两个行添加一行,也很简单。 However, I do not want the line or its fill displayed in one of the two facets. 但是,我不希望在两个方面之一中显示该行或其填充。 Is there a simple way to do this? 有一个简单的方法吗? Perhaps specifying a blank color for the fill and colour of one of the lines somehow? 也许以某种方式为其中一条线的填充和颜色指定一个空白颜色?

Don't think about picking a facet, think supplying a subset of your data to stat_smooth: 不要考虑选择一个方面,考虑将一部分数据提供给stat_smooth:

ggplot(df, aes(x, y)) +
  geom_point() + 
  geom_smooth(data = subset(df, z =="a")) + 
  facet_wrap(~ z)

Of course, I later answered my own question. 当然,我后来回答了我自己的问题。 Although, is there a less hack-y way to do this? 虽然,有没有更少的黑客方式来做到这一点? I wonder if one could even fit different functions to different panels. 我想知道是否有人可以适应不同的面板。

One technique is to use + scale_fill_manual and scale_colour_manual. 一种技术是使用+ scale_fill_manual和scale_colour_manual。 They allow one to specify what colors will be used. 它们允许指定将使用的颜色。 So, in this case, let's say you have 所以,在这种情况下,让我们说你有

a<-qplot(x, y, facets=~z)+stat_smooth(method="lm", aes(colour=z, fill=z))

You can specify colors for the fill and colour using the following. 您可以使用以下内容指定填充和颜色的颜色。 Note, the second color is clear, as it is using a hex value with the final two numbers representing transparency. 注意,第二种颜色是清晰的,因为它使用十六进制值,最后两个数字代表透明度。 So, 00=clear. 所以,00 =清楚。

a+stat_fill_manual(values=c("grey", "#11111100"))+scale_colour_manual(values=c("blue", "#11111100"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM