Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot-条件背景_R_Ggplot2 - Fatal编程技术网

R ggplot-条件背景

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),

当被试之间的差异小于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), 
         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)