Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.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 基于不同层中geom值的geom_点的条件着色_R_Ggplot2 - Fatal编程技术网

R 基于不同层中geom值的geom_点的条件着色

R 基于不同层中geom值的geom_点的条件着色,r,ggplot2,R,Ggplot2,创建一条裸骨骼编号线,其中最小值、最大值和中间值与第二层的平均值一起绘制,作为线上的一个点。我想能够改变点的颜色,这取决于它相对于中间带的相对位置 以下是一些示例数据: library(tidyverse) df <- data.frame( species = rep(c('dog','cat'),5), trait = 'weight', value = sample(5:25, 5)) df %>% ggplot(aes(value,trait)) +

创建一条裸骨骼编号线,其中最小值、最大值和中间值与第二层的平均值一起绘制,作为线上的一个点。我想能够改变点的颜色,这取决于它相对于中间带的相对位置

以下是一些示例数据:

library(tidyverse)
df <- data.frame(
  species = rep(c('dog','cat'),5),
  trait = 'weight', 
  value = sample(5:25, 5))

df %>%
  ggplot(aes(value,trait)) +
  stat_boxplot(geom = "errorbar", width = 0.5) +
  stat_summary(fun=median, geom="segment", aes(xend=..x.., yend=1.25, y = 0.75)) +
  stat_summary(data = (df %>% filter(species == 'dog')), fun=mean,
               geom="point",  size=4,
               shape = 15, 
               color = 'green') + # i'd like to change thhis color
  theme_minimal() +
  theme(line = element_blank(), text = element_blank())
当前代码:

color = ifelse(mean(test %>%
                                      filter(species == 'dog') %>%
                                      pull(value)) >
                                 mean(test %>% pull(value)),
                                    'green', 'red'))

设置种子(1)

df你可以在统计之后使用相对较新的
。也就是说,如果你不太确定在计算你的统计数据时会发生什么,更安全的选择是遵循Jon的想法

我使用了乔恩的数据(谢谢你和+1:)

库(tidyverse)
种子(1)
df%
ggplot(aes(值、特征))+
统计箱线图(geom=“errorbar”,宽度=0.5)+
统计汇总(fun=中位数,geom=“段”,aes(xend=…x..,yend=1.25,y=0.75))+
统计汇总(aes(颜色=统计(x)<中值(x)),乐趣=平均值,
geom=“点”,尺寸=4,
形状=15)+
面_包装(~种)

由(v1.0.0)

于2021-03-23年创建,只需稍加调整即可接受(我还没有编辑权限,因此无法编辑您的答案以反映),但保留了我的片段,因为我希望中间线不在组中,而是在所有动物(猫+狗)上。然后,我将geom_点的数据更改为data=gf_sum%>%过滤器(species==“dog”),这在我的闪亮应用程序中非常有效,我将==更改为用户输入。我还必须在scale\u color\u手动图层中添加值=c(“真”=“绿”、“假”=“红”)。
color = ifelse(mean(test %>%
                                      filter(species == 'dog') %>%
                                      pull(value)) >
                                 mean(test %>% pull(value)),
                                    'green', 'red'))
set.seed(1)
df <- data.frame(
  species = rep(c('dog','cat'),5),
  trait = 'weight', 
  value = sample(5:25, 10)) # changed to make dif cat and dog #s
library(dplyr)
df_sum <- df %>%
  group_by(species) %>%
  summarize(grp_median = median(value),
            grp_mean = mean(value))

df %>%  
  ggplot(aes(value,species)) +
  stat_boxplot(geom = "errorbar", width = 0.5) +
  geom_segment(data = df_sum,
               aes(y = as.numeric(as.factor(species)) - 0.1,
                   yend = as.numeric(as.factor(species)) + 0.1,
                   x = grp_median, xend = grp_median), inherit.aes = F) +
  geom_point(data = df_sum,
             aes(grp_mean, species, color = grp_mean < grp_median)) +
  scale_color_manual(values = c("green", "red"), guide = F) +
  theme_minimal() +
  theme(line = element_blank(), text = element_blank())