如何用不同颜色的geom_errorbar绘制ggplot?

阿德里安
library(tidyverse)
mydata <- structure(list(Index = c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 
2L, 3L, 3L), Gender = c("Male", "Female", "Male", "Female", "Male", 
"Female", "Male", "Female", "Male", "Female", "Male", "Female"
), Height = c(NA, NA, 0.740339097715042, 0.758196660530606, 0.218048382364689, 
0.329217343894418, NA, NA, 0.749669793529369, 0.7445564572896, 
0.273171513372133, 0.452720902980308), Lo = c(NA, NA, 0.729682349457229, 
0.739120997850057, 0.20316516805848, 0.311224922583981, NA, NA, 
0.718886931624498, 0.708069014803844, 0.233670719484927, 0.402244911750405
), Up = c(NA, NA, 0.755735601081241, 0.778557403161694, 0.237068004398738, 
0.349859894992744, NA, NA, 0.786845227603324, 0.786837817718618, 
0.314042083915779, 0.499544458312984), Score = c(0.075, 0.075, 
0.075, 0.075, 0.075, 0.075, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3)), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))


> mydata
# A tibble: 12 × 6
   Index Gender Height     Lo     Up Score
   <int> <chr>   <dbl>  <dbl>  <dbl> <dbl>
 1     1 Male   NA     NA     NA     0.075
 2     1 Female NA     NA     NA     0.075
 3     2 Male    0.740  0.730  0.756 0.075
 4     2 Female  0.758  0.739  0.779 0.075
 5     3 Male    0.218  0.203  0.237 0.075
 6     3 Female  0.329  0.311  0.350 0.075
 7     1 Male   NA     NA     NA     0.3  
 8     1 Female NA     NA     NA     0.3  
 9     2 Male    0.750  0.719  0.787 0.3  
10     2 Female  0.745  0.708  0.787 0.3  
11     3 Male    0.273  0.234  0.314 0.3  
12     3 Female  0.453  0.402  0.500 0.3  

我有一个包含男女学生身高数据的数据集。这些学生被分成两组Score在 的每个级别内Score,有 3 个Indexes。

dplyr::rename(mydata, Gender = "Gender") %>%  ggplot(aes(x = Index, y = Height, color = Gender, ymin = Lo, ymax = Up)) +
  geom_errorbar(position = position_dodge(width = 0.2),  width = 0.2) +
  geom_point(position = position_dodge(width = 0.2), size = 0.8) 

在此处输入图片说明

我可以看到雄性是绿色的,雌性是红色的。但是,如何区分两个不同Scores的误差线我怎样才能给它们上不同的颜色?

哈顿cp

您可以通过使用 tidyr::unite() 将性别和分数变量组合到一个列中,然后将新的组合变量分配给颜色美学来实现这一点。

    tibble(Index = c(1L, 1L, 2L, 2L, 3L, 3L, 
                 1L, 1L, 2L, 2L, 3L, 3L), 
       Gender = c("Male", "Female", "Male", "Female", "Male", 
                  "Female", "Male", "Female", "Male", "Female", "Male", "Female"), 
       Height = c(NA, NA, 0.740339097715042, 0.758196660530606, 0.218048382364689, 
                  0.329217343894418, NA, NA, 0.749669793529369, 0.7445564572896, 
                  0.273171513372133, 0.452720902980308), 
       Lo = c(NA, NA, 0.729682349457229, 
              0.739120997850057, 0.20316516805848, 0.311224922583981, NA, NA, 
              0.718886931624498, 0.708069014803844, 0.233670719484927, 0.402244911750405), 
       Up = c(NA, NA, 0.755735601081241, 0.778557403161694, 0.237068004398738, 
              0.349859894992744, NA, NA, 0.786845227603324, 0.786837817718618, 
              0.314042083915779, 0.499544458312984), 
       Score = c(0.075, 0.075,  0.075, 0.075, 0.075, 0.075, 
                 0.3, 0.3, 0.3, 0.3, 0.3, 0.3)) %>% 
  tidyr::unite(Gender, Score, col = "group") %>% 
  ggplot(aes(x = Index, y = Height, color = group, ymin = Lo, ymax = Up)) +
  geom_errorbar(position = position_dodge(width = 0.2),  width = 0.2) +
  geom_point(position = position_dodge(width = 0.2), size = 0.8) 

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何用自定义值绘制geom_errorbar(R或森林中的元分析?)

ggplot抖动geom_errorbar?

使用ggplot2为R中的日期绘制geom_errorbar

如何在ggplot2的条形图上使geom_errorbar正确“躲避”?

使用 ggplot2 中的 geom_errorbar() 重新格式化数据以绘制校准曲线

如何用不同的颜色绘制蓝色贝塞尔曲线的每个部分?

Qt 5.6,如何用不同的填充和轮廓颜色绘制文本

ggplot中带有ecdf的geom_errorbar

ggplot2 和 geom_errorbar() 的问题

ggplot如何用不同的颜色填充一个价格值

如何用不同的颜色绘制同一个Pandas系列色谱柱的不同部分?

如何用不同的颜色绘制相同 Pandas Series 列的不同部分,并具有自定义索引?

如何删除geom_errorbar中的误差线末端

如何用不同颜色的Tkinter Python制作不同的单词?

如何用ggplot指定不同的颜色

用不同颜色的ggplot绘制多个数据框

如何用不同大小的节点绘制图形

如何用不同的颜色设置相同的类名值

如何用不同颜色填充KDE曲线下的区间

如何用不同的颜色左右扩展包裹的元素背景?

如何用不同的图修复颜色条

如何用不同的颜色填充`EditText`项目?

如何用颜色订购geom_segment ggplot

在ggplot2中使用geom_errorbar会导致“错误:geom_errorbar需要以下美感:ymin,ymax”

如何在 JTable 中用不同颜色绘制特定列?

如何用ggplot绘制?

R-ggplot2-如果geom_errorbar超出限制,则添加箭头

使用 ggplot2 的 geom_errorbar 时出现“缺少美学错误”

如何在`survminer`包的`ggcompetingrisks`中用`geom_errorbar`替換默認的`geom_ribbon`?