R ggplot-条件背景
当被试之间的差异小于10时,我想有条件地给背景上色 例如: 样本数据:R ggplot-条件背景,r,ggplot2,R,Ggplot2,当被试之间的差异小于10时,我想有条件地给背景上色 例如: 样本数据: library(tidyverse) set.seed(895) df <- data.frame(s1 = rnorm(20, 85, 3), s2 = rnorm(20, 75, 3)) %>% rownames_to_column("Time") %>% mutate(Time = as.numeric(Time),
library(tidyverse)
set.seed(895)
df <- data.frame(s1 = rnorm(20, 85, 3),
s2 = rnorm(20, 75, 3)) %>%
rownames_to_column("Time") %>%
mutate(Time = as.numeric(Time),
dif = abs(s1 - s2) < 10)
将col=“NA”
移动到美学之外
geom_tile(aes(x=Time,y=80, height = 20, fill=dif), col = "NA")
或者更好。。。如果您不希望所有图形组件使用相同的美感(比如颜色),那么不要将它们放在顶级ggplot函数中-将它们放在要遵循的单独geom_uu函数中
library(tidyr)
library(ggplot2)
df %>%
gather(subject, value, -Time, -dif) %>%
ggplot(aes(Time, value)) +
geom_tile(aes(x=Time, y=80, height = 25, fill=dif), alpha=0.5) +
geom_line(aes(col = subject), lwd=1)
您可能还希望更改瓷砖或线条的颜色,以便它们不会相互遮挡。在这里,我使用了透明度(alpha=0.5)来降低瓷砖的透明度,从而使线条可见
library(tidyr)
library(ggplot2)
df %>%
gather(subject, value, -Time, -dif) %>%
ggplot(aes(Time, value)) +
geom_tile(aes(x=Time, y=80, height = 25, fill=dif), alpha=0.5) +
geom_line(aes(col = subject), lwd=1)