简体   繁体   English

R:ggplot2:使用facet_wrap创建边距

[英]R: ggplot2: creating a margin with facet_wrap

Say I am partitioning a scatter plot p by a single variable eg 假设我正在通过单个变量p散点图p进行分区,例如

p = p + facet_wrap(~ DayOfWeek, ncol = 3)

Is there a way to include a margin at the end of the graph ie a scatter plot which doesn't partition the data by DayOfWeek ? 有没有办法在图表的末尾包含一个边距,即一个不按DayOfWeek划分数据的散点图?

Cheers fro any help 欢呼任何帮助

The easiest way I can think of is to append a copy of the data set to itself, with DayofWeek set to "all", as follows: 我能想到的最简单的方法是将数据集的副本附加到自身,将DayofWeek设置为“all”,如下所示:

Make up data and base plot: 弥补数据和基础情节:

w <- c("Sun","Mon","Tues","Weds","Thurs","Fri","Sat")
f <- data.frame(x=runif(200),y=runif(200),
                DayofWeek=factor(sample(w,size=200,replace=TRUE),levels=w))
library(ggplot2)
p  <- ggplot(f,aes(x,y))+geom_point()

Append marginalized data set: 附加边缘化数据集:

fx <- rbind(f,transform(f,DayofWeek="all"))

Substitute combined data and facet: 替换组合数据和方面:

p %+% fx + facet_wrap(~DayofWeek,ncol=3)

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

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