R 如何基于两个数据帧为特定点着色

R 如何基于两个数据帧为特定点着色,r,ggplot2,R,Ggplot2,我有两个数据集。我想策划 df <-structure(list(test = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "D", class = "factor"), N = 1:15, value = c(0.193333333333333, 0.166666666666667, 0.226666666666667, 0.23333333333

我有两个数据集。我想策划

df <-structure(list(test = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "D", class = "factor"), 
    N = 1:15, value = c(0.193333333333333, 0.166666666666667, 
    0.226666666666667, 0.233333333333333, 0.246666666666667, 
    0.113333333333333, 0.14, 0.126666666666667, 0.233333333333333, 
    0.206666666666667, 0.14, 0.26, 0.213333333333333, 0.206666666666667, 
    0.22)), row.names = c(NA, 15L), class = "data.frame")
我想画出它们在df中的值小于0.13,并且它们在df2中的对应值小于0.5的红色

例如,在本例中,查看两个数据(数字6)中的N,df值为0.11,df2中的值为2.903913e-01,小于0.5,因此,我希望它用红色表示


我不介意您使用的不是qplot

这里有一个选项。基本上,您必须创建一个分类变量,稍后可以在ggplot中使用该变量来指定点的颜色

# join data frames to facilitate comparison of values:
dfComb <- full_join(df, df2, by = c("N"))

dfComb %>% 
  mutate(
    colorVar = case_when( # categorize data points based on your logic (if_else would also be nice)
      value.x < .13 & value.y < .5 ~ "red",
      TRUE ~ "not red"
    )
  ) %>% # pipe dataset into ggplot...
  ggplot() +
  geom_point( # the aesthetics here match your qplot
    aes(value.x, N, color = colorVar)
    ) +
  scale_color_manual( # set colors manually to get red
    values = c("blue", "red")
  )
#连接数据帧以便于比较值:
dfComb%
变异(
colorVar=case_when(#根据您的逻辑对数据点进行分类(如果有其他方法也可以)
value.x<.13和value.y<.5~“红色”,
对~“不是红色”
)
)%%>%#将数据集导入ggplot。。。
ggplot()+
geom#u point(#这里的美学与你的作品相匹配
aes(值.x,N,颜色=colorVar)
) +
缩放颜色手动(#手动设置颜色以获得红色
值=c(“蓝色”、“红色”)
)
产生:


这里有一个选项。基本上,您必须创建一个分类变量,稍后可以在ggplot中使用该变量来指定点的颜色

# join data frames to facilitate comparison of values:
dfComb <- full_join(df, df2, by = c("N"))

dfComb %>% 
  mutate(
    colorVar = case_when( # categorize data points based on your logic (if_else would also be nice)
      value.x < .13 & value.y < .5 ~ "red",
      TRUE ~ "not red"
    )
  ) %>% # pipe dataset into ggplot...
  ggplot() +
  geom_point( # the aesthetics here match your qplot
    aes(value.x, N, color = colorVar)
    ) +
  scale_color_manual( # set colors manually to get red
    values = c("blue", "red")
  )
#连接数据帧以便于比较值:
dfComb%
变异(
colorVar=case_when(#根据您的逻辑对数据点进行分类(如果有其他方法也可以)
value.x<.13和value.y<.5~“红色”,
对~“不是红色”
)
)%%>%#将数据集导入ggplot。。。
ggplot()+
geom#u point(#这里的美学与你的作品相匹配
aes(值.x,N,颜色=colorVar)
) +
缩放颜色手动(#手动设置颜色以获得红色
值=c(“蓝色”、“红色”)
)
产生:


如果
dplyr
不是所需的包,则base-R(在本例中)是直接的:
merge(df,df2,by=“N”,all=TRUE)
。如果
dplyr
不是所需的包,则base-R(在本例中)是直接的:
merge(df,df2,by=“N”,all=TRUE)
# join data frames to facilitate comparison of values:
dfComb <- full_join(df, df2, by = c("N"))

dfComb %>% 
  mutate(
    colorVar = case_when( # categorize data points based on your logic (if_else would also be nice)
      value.x < .13 & value.y < .5 ~ "red",
      TRUE ~ "not red"
    )
  ) %>% # pipe dataset into ggplot...
  ggplot() +
  geom_point( # the aesthetics here match your qplot
    aes(value.x, N, color = colorVar)
    ) +
  scale_color_manual( # set colors manually to get red
    values = c("blue", "red")
  )