有没有办法根据随数据框提供的变量填充使用facet_wrap创建的facet条?
示例数据:
MYdata <- data.frame(fruit = rep(c("apple", "orange", "plum", "banana", "pear", "grape")), farm = rep(c(0,1,3,6,9,12), each=6), weight = rnorm(36, 10000, 2500), size=rep(c("small", "large")))
示例图:
p1 = ggplot(data = MYdata, aes(x = farm, y = weight)) + geom_jitter(position = position_jitter(width = 0.3), aes(color = factor(farm)), size = 2.5, alpha = 1) + facet_wrap(~fruit)
我知道如何更改条带的背景颜色(例如橙色):
p1 + theme(strip.background = element_rect(fill="orange"))

有没有办法转嫁值的变量size中MYdata的参数fill的element_rect?
基本上,对于所有条带而不是1种颜色,我希望小水果(苹果,李子,梨)的条带背景颜色为绿色,大果实(橙色,香蕉,葡萄)的背景颜色为红色.
我正在使用ggplot2中的"提示"数据集.如果我做
sp = ggplot(tips,aes(x=total_bill, y = tip/total_bill)) +
geom_point(shape=1) +
facet_grid(sex ~ day)
Run Code Online (Sandbox Code Playgroud)
情节很好.但我现在想要改变"Fri"下的情节的面板背景.有没有办法做到这一点?
更好的是,我可以通过传递参数来有条件地改变颜色吗?例如,如果超过3个点低于0.1,那么将面板背景(仅适用于该面板)更改为某种颜色,而其他所有颜色保持默认浅灰色?
是否可以有条件地填充这些[ggtext][1]文本框?如果“拾取”,我们说颜色“红色”。
library(cowplot)
library(tidyverse)
library(ggtext)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_wrap(~class) +
theme_half_open(12) +
background_grid() +
theme(
strip.background = element_blank(),
strip.text = element_textbox(
size = 12,
color = "white", fill = "#5D729D", box.color = "#4A618C",
halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"),
padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3)
)
)
Run Code Online (Sandbox Code Playgroud) 我用以下命令创建了下面的图:
ggplot(long.data, aes(owner,value)) + stat_summary(fun.y=mean,geom="bar",
fill=c("deepskyblue","deepskyblue4")) +
stat_summary(fun.data=mean_cl_normal,geom="errorbar",position=
position_dodge(width=.90),width=.1) +
labs(x="",y="") + facet_grid(IV~experiment+type,scales="free_y") +
theme(strip.text.y = element_text(colour = 'red4'))
Run Code Online (Sandbox Code Playgroud)
如果我想仅为上面的x面(在这种情况下为"隐式"和"显式"级别)更改文本颜色(可能还有背景颜色),我该怎么做?可能吗?我在ggplot2文档中没有读到任何相关内容.
安德里亚

编辑:我很抱歉这个混乱.我的目标是改变其中一个上部条带的文本和背景颜色,而不是刻面的颜色.
有没有办法对ggplot2 的facet_grid 中的facet 的名称或条形背景进行着色(不是实际网格的背景,正如这篇文章中所回答的: Conditionally Change panel background with facet_grid? )?
在以下示例中:
#create simple dataframe for plotting
xy <- data.frame(x = rep(1:10, times=3), y = rep(10:1, times=3), type = rep(LETTERS[1:2], each=5), type2 = rep(LETTERS[3:5], each=10), category = rep(c("control", "exp", "control"), each=10))
xy
#create base plot
plot <- ggplot(data = xy)+
geom_point(aes(x = x, y = y))+
facet_grid(type ~ type2)
#plot base plot
plot
Run Code Online (Sandbox Code Playgroud)
有没有办法根据分类变量(例如,我在上面指定的数据框中的“类别”,因此所有控件都有一个红色条,所有的 exp - 一个绿色条)?
提前致谢!
PS我实际上想做的是展示生物实验的结果:
每个基因的两个箱线图以显示变异性;基因名称在条带中
网格的背景是白色或灰色,灰色显示作为对照的基因
我想使用标题中文本的颜色来表示该基因在我的实验中是否重要(我需要在同一图中显示经过验证的基因和未经验证的基因)