相对于具有标准偏差的平均值的“点图”

时间:2019-02-03 10:44:08

标签: r ggplot2 tidyverse standard-deviation

在所附图形中,圆点表示每种物种的选择性的平均值。在此情节中,我想

1)具有较大的字体大小,而较小的点表示较小的体重(bodymass <9Kg)。我想通过将较小的大一点,但保持其他最大(>9公斤)在相同的大小,以减少点之间差异的规模。

2)我想得加点周围的黑色边框(这只是突出点)

3)我想添加每个平均点的标准偏差(sd)。平均值和标准偏差值分别显示在“介质”和“ sp”列中。

有人可以帮我吗?谢谢

library(tidyverse)
Dataset %>%
ggplot(aes(x = media, y = specie, 
             colour = energetic_level, size = log(bodymass))) +
  geom_point(alpha = .9) +
  scale_colour_continuous(low = 'green', high = 'red') +
  labs(x = 'Response rate', y = 'Species') +
  ggthemes::theme_few() +
  theme(axis.text.x = element_text(angle = 90, vjust = .5)) 
media   dp  specie  bodymass    energetic_level
4.063478961 3.627269468 AAChlor_cyn 5000.01 3.2
4.05537378  3.585436083 ABOtol_cras 1206.61 2.4
3.999309751 3.818689333 ACMiop_tal  1248.86 3
3.945049659 3.855743536 BACerc_mit  5041.29 2.5
3.862515658 3.687924328 BCThry_swin 4000    2.8
3.655056928 3.732785731 DAHys_afri  14936.02    2.8
3.56041853  3.478167947 DBLep_cap   1500    3
3.402431689 3.446995588 DCCivet_civ 12075.58    4.6
3.401743858 3.569716116 FGenet_gen  1756.17 6.1
3.39029097  3.414370313 GALept_serv 11999.96    7
3.39009097  1.552336764 GBPhil_mont 4896.05 2.6
3.32029097  1.920646552 HOryct_afer 56175.2 5
3.239734182 3.540636613 IHipp_amph  1536310.4   3
3.154474564 3.526089786 JBSylv_grim 15639.15    3.2
2.883544415 3.007873613 MAPota_larv 69063.79    3.3
2.719993477 1.308813082 MBTrag_scri 43250.39    3
2.718552867 3.080761281 MCPant_pa   52399.99    7
1.982822501 2.085016316 MDRed_aru   58059.24    3
1.529854402 1.814623348 MFSync_caf  592665.98   3
1.443776834 1.254052861 NLox_afric  3824539.93  3
1.402107786 1.637998721 OCan_mes    22000   5.2
1.164299734 1.397597868 PPant_le    158623.93   6.8
0.887732043 1.318886523 QLyc_pict   21999.99    7
0.82952687  0.789227213 UCroc_croc  63369.98    7
0.782973623 0.570878282 VTrag_oryx  562592.69   2.7
0.477482615 0.624782141 YHipp_eq    264173.96   3

enter image description here

1 个答案:

答案 0 :(得分:1)

请注意,您的所有问题均已在SO上以一种或另一种形式得到了解决。因此,这似乎是一个扩展的多重复帖子,并且经过全面的SO搜索将为您提供所需的所有答案/细节。

特别是:

  1. 使用scale_sizescale_size_areascale_radius之一可以更改点的大小。对于您在第1点中提出的要求,我还不太清楚,但是看看这些功能并尝试使用某些功能参数应该可以为您提供所需的信息。

    相关帖子为:defining minimum point size in ggplot2 - geom_point ggplot2: how to manually adjust scale_area

  2. 要用黑色轮廓填充点,请使用pch = 21,然后使用fill美学和colour = "black"

    这是Place a border around points

  3. 的副本
  4. 水平误差线可以使用geom_errorbarh来实现;垂直的(如果需要的话),如果是geom_errorbar

    相关帖子为:ggplot2 : Adding two errorbars to each point in scatterplotRemove endpoints from error bars in ggplot2

全部,您最终会得到这样的东西:

library(ggthemes)
library(ggplot2)
ggplot(df, aes(media, specie, fill = energetic_level)) +
    geom_errorbarh(aes(xmax = media + dp, xmin = media - dp)) +
    geom_point(aes(size = log(bodymass)), pch = 21, colour = "black", alpha = .9) +
    scale_fill_continuous(low = 'green', high = 'red') +
    scale_size_area() + 
    labs(x = 'Response rate', y = 'Species') +
    ggthemes::theme_few() +
    theme(axis.text.x = element_text(angle = 90, vjust = .5))

enter image description here


样本数据

df <- read.table(text =
    "media   dp  specie  bodymass    energetic_level
4.063478961 3.627269468 AAChlor_cyn 5000.01 3.2
4.05537378  3.585436083 ABOtol_cras 1206.61 2.4
3.999309751 3.818689333 ACMiop_tal  1248.86 3
3.945049659 3.855743536 BACerc_mit  5041.29 2.5
3.862515658 3.687924328 BCThry_swin 4000    2.8
3.655056928 3.732785731 DAHys_afri  14936.02    2.8
3.56041853  3.478167947 DBLep_cap   1500    3
3.402431689 3.446995588 DCCivet_civ 12075.58    4.6
3.401743858 3.569716116 FGenet_gen  1756.17 6.1
3.39029097  3.414370313 GALept_serv 11999.96    7
3.39009097  1.552336764 GBPhil_mont 4896.05 2.6
3.32029097  1.920646552 HOryct_afer 56175.2 5
3.239734182 3.540636613 IHipp_amph  1536310.4   3
3.154474564 3.526089786 JBSylv_grim 15639.15    3.2
2.883544415 3.007873613 MAPota_larv 69063.79    3.3
2.719993477 1.308813082 MBTrag_scri 43250.39    3
2.718552867 3.080761281 MCPant_pa   52399.99    7
1.982822501 2.085016316 MDRed_aru   58059.24    3
1.529854402 1.814623348 MFSync_caf  592665.98   3
1.443776834 1.254052861 NLox_afric  3824539.93  3
1.402107786 1.637998721 OCan_mes    22000   5.2
1.164299734 1.397597868 PPant_le    158623.93   6.8
0.887732043 1.318886523 QLyc_pict   21999.99    7
0.82952687  0.789227213 UCroc_croc  63369.98    7
0.782973623 0.570878282 VTrag_oryx  562592.69   2.7
0.477482615 0.624782141 YHipp_eq    264173.96   3", header = T)