在ggplot2中,我如何更改选定构面的边界? [英] In ggplot2, how can I change the border of selected facets?

查看:153
本文介绍了在ggplot2中,我如何更改选定构面的边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从ggplot2帮助页面获取图表:

  ggplot(mtcars,aes(factor(cyl)))+ geom_bar( )+ facet_grid(。〜vs)

是否可以更改边框(颜色和/或厚度)只有选定的面板?例如,我想改变faceting变量 vs 的方面的边界。



我尝试添加

 主题(panel.border = element_rect(size = 3,color =red,fill = NA ))

但该解决方案会改变所有边界。



我也在考虑使用 geom_rect geom_polygon ,但我不确定如何将它限制为一个绘图。



我偶然发现了这个 thread on the R help list,but the solutions does not work for me



关于如何向前移动的任何建议都将不胜感激。

这个?
$ b $ $ p $ dd < - data.frame(vs = c(0,1),ff = factor(0:1 ))
ggplot()+ geom_rect(data = dd, aes(fill = ff),
xmin = -Inf,xmax = Inf,ymin = -Inf,ymax = Inf,alpha = 0.15)+
geom_bar(data = mtcars,aes(factor(cyl) ))+ facet_grid(。 〜vs)+
scale_fill_manual(values = c(NA,red),breaks = NULL)


Taking the graph from ggplot2 help pages:

ggplot(mtcars, aes(factor(cyl))) + geom_bar() + facet_grid(. ~ vs)

Is it possible to change the border (colour and/or thickness) of only selected panels? I'd like to, for instance, change the border of the facet of '1' of faceting variable vs.

I tried adding

theme(panel.border = element_rect(size = 3, colour = "red", fill = NA))

but that solution changes all borders.

I was also thinking about using geom_rect or geom_polygon but am not sure how to limit it to one plot either.

I stumbled upon this thread on R help list, but the solutions didn't work for me

Any suggestions on how to move forward will be much appreciated.

解决方案

How about filling it with a colour like this?

dd <- data.frame(vs = c(0,1), ff = factor(0:1))
ggplot() + geom_rect(data=dd, aes(fill=ff), 
    xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf, alpha=0.15) + 
    geom_bar(data = mtcars, aes(factor(cyl))) + facet_grid(. ~ vs) + 
    scale_fill_manual(values=c(NA, "red"), breaks=NULL)

这篇关于在ggplot2中,我如何更改选定构面的边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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