如何将灰色小平面标签(A和B)更改为带有白色文本的红色背景?
A = data.table(x = 1:4, y = 1:4, z = c('A','A','B','B'))
ggplot(A) + geom_point(aes(x = x, y = y)) + facet_wrap(~z) + theme_bw()
Run Code Online (Sandbox Code Playgroud)
我想根据组修改构面背景的颜色.我不确定这是否可行.具体来说,我使用facet_grid(不facet_wrap)多层.
## Sample data
dat <- mtcars
## Add in some colors based on the data
dat$facet_fill_color <- c("red", "green", "blue", "yellow", "orange")[dat$gear]
## Create main plot
library(ggplot2)
P <- ggplot(dat, aes(x=cyl, y=wt)) + geom_point(aes(fill=hp)) + facet_grid(gear+carb ~ .)
## I can easily cahnge the background using:
P + theme(strip.background = element_rect(fill="red"))
Run Code Online (Sandbox Code Playgroud)
但是,我想为不同的组改变颜色.理想情况下,如下所示(当然不起作用)
P + theme(strip.background = element_rect(fill=dat$facet_fill_color))
P + theme(strip.background = element_rect(aes(fill=facet_fill_color)))
Run Code Online (Sandbox Code Playgroud)
小平面背景可以有多种颜色吗?
(相关,但不是上面的实际答案: ggplot2:基于数据集中变量的facet_wrap条带颜色)
将根据这些数据绘制一个图:
library(ggplot2)
mtcars %>%
gather(-mpg, key = "var", value = "value") %>%
ggplot(aes(x = value, y = mpg)) +
geom_point() +
facet_wrap(~ var, scales = "free") +
theme_bw()
Run Code Online (Sandbox Code Playgroud)
例如,如何更改面板标题的灰色
添加图例
有没有办法对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我实际上想做的是展示生物实验的结果:
每个基因的两个箱线图以显示变异性;基因名称在条带中
网格的背景是白色或灰色,灰色显示作为对照的基因
我想使用标题中文本的颜色来表示该基因在我的实验中是否重要(我需要在同一图中显示经过验证的基因和未经验证的基因)