使用数据ggplot的子集创建自定义图例

时间:2019-11-06 17:34:43

标签: r ggplot2

我正在创建一些堆积的条形图,其中大约有50种不同的因素。我想要一个传说,它仅绘制8个最丰富因素的颜色。

我首先计算了平均丰度。然后我以此命令订购了该因素。并且还可以获取相应的颜色,但是我不知道是否应该“独立地”创建图例,或者是否缺少ggplot的另一种“简便”方式。 任何帮助表示赞赏。

谢谢

## calculating the OTUs mean abundance
## instead of labelling all 50 in the legend just label top 5 or 10 for label 
  meanAbund<-as.data.frame(colSums(dat.grouped[,grep("Otu",colnames(dat.grouped))]))
  meanAbund$Name<-as.character(tax_tab1[match(rownames(meanAbund),rownames(tax_tab1)),ncol(tax_tab1)])
  colnames(meanAbund)<-c("mean_abund","Name")
  top8_meanAbund<-meanAbund[rev(order(meanAbund$mean_abund)),][1:8]
# use melted dataframe for plotting      
## now ordered by mean abundance
  dat.groupedM$Level<-factor(dat.groupedM$Level,meanAbund[rev(order(meanAbund$mean_abund)),"Name"])
  #top8 (mean is greater than 0.1 %)
  # top 8 colors
  library(RColorBrewer)
  n <- length(unique(dat.groupedM$Level))
 # generate divergent color palete, cause so many colors
  qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
  col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
  set.seed(200)
  mycols<-sample(col_vector, n)
  top8cols<-mycols[1:8]
  #pie(rep(1,n),  mycols)
  ggplot(dat.groupedM, aes(x = Time, y = value, fill = Level))+
    geom_bar(stat = "identity") +
    facet_wrap(~Diet)+
   scale_fill_manual(values = mycols) +
    theme_bw()+
    theme(legend.position = "none")+
    ggtitle(paste(level,"composition - Cohort 1"))

enter image description here

感谢靠近,我现在有这个,见下文。但是它没有使用我之前用scale_fill_manual指定的调色板“ mycols”,现在我根据建议更改为“ mycols”。好的,现在这是正确的!

scale_fill_manual(values = mycols,breaks = top10_meanAbund$Name)+

enter image description here

    > dput(dat.groupedM)
structure(list(Diet = structure(c(2L, 2L, 3L, 3L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23L, 23L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 26L, 
26L, 26L, 26L, 27L, 27L, 27L, 27L, 28L, 28L, 28L, 28L, 29L, 29L, 
29L, 29L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 32L, 32L, 32L, 
32L, 33L, 33L, 33L, 33L, 34L, 34L, 34L, 34L, 35L, 35L, 35L, 35L, 
36L, 36L, 36L, 36L, 37L, 37L, 37L, 37L, 38L, 38L, 38L, 38L, 39L, 
39L, 39L, 39L, 40L, 40L, 40L, 40L, 41L, 41L, 41L, 41L, 42L, 42L, 
42L, 42L, 43L, 43L, 43L, 43L, 44L, 44L, 44L, 44L, 45L, 45L, 45L, 
45L, 46L, 46L, 46L, 46L, 47L, 47L, 47L, 47L, 48L, 48L, 48L, 48L, 
49L, 49L, 49L, 49L, 50L, 50L, 50L, 50L), .Label = c("Otu1", "Otu108", 
"Otu11", "Otu112", "Otu136", "Otu14", "Otu155", "Otu157", "Otu158", 
"Otu161", "Otu182", "Otu186", "Otu197", "Otu21", "Otu23", "Otu242", 
"Otu244", "Otu247", "Otu27", "Otu29", "Otu30", "Otu314", "Otu35", 
"Otu36", "Otu378", "Otu4", "Otu40", "Otu42", "Otu427", "Otu44", 
"Otu464", "Otu49", "Otu53", "Otu535", "Otu56", "Otu585", "Otu607", 
"Otu623", "Otu629", "Otu645", "Otu648", "Otu664", "Otu69", "Otu715", 
"Otu730", "Otu741", "Otu76", "Otu77", "Otu86", "Otu90"), class = "factor"), 
    value = c(0.125797907894198, 0.236708006778536, 0.577822547980787, 
    0.590703405992419, 0.010616635104027, 0.0101758392492056, 
    0.000833768625886666, 0.00075132991625995, 3.6675879491077e-05, 
    4.75000300636826e-05, 1.80354502712558e-05, 7.12272349413578e-06, 
    0.0923297961192989, 0.0516121598003722, 0.00943059557285678, 
    0.0071906839719107, 0.00539229454875785, 0.00506484304043506, 
    0.00338347504700533, 0.0025680210124108, 0.00175591025013762, 
    0.00395104856215105, 0.0561412864297836, 0.0389581641347799, 
    0.000170273382201598, 0.00010844375756416, 2.43702775724318e-05, 
    2.69013349522259e-05, 0.00832912099453767, 0.010385453199374, 
    0.000178806881780292, 0.000192961017676232, 0.000133411286157921, 
    0.000160277350764036, 3.61247330219488e-05, 2.18172717187001e-05, 
    0.0052992653709469, 0.0157284378186556, 1.02095850472487e-05, 
    6.26578426928999e-06, 1.60700750910741e-05, 1.5642472562587e-05, 
    6.62816031320301e-06, 0.00184183762620945, 0.000976314979621793, 
    0.000837378482800852, 0.00119405390180218, 0.000749043101546972, 
    2.53563183604468e-05, 6.40671433657518e-05, 2.15845735655314e-05, 
    2.23193546150843e-05, 0.021152108912735, 0.0154293683430873, 
    0.0179088127298003, 0.0243504748685249, 0.112274814726721, 
    0.117619628857172, 3.02064928526444e-05, 4.05741755377207e-05, 
    0.00181223384695308, 0.00207625195815772, 0.000943789542460111, 
    0.000206467245724716, 0.00154532777776536, 0.00226885210972427, 
    0.000119002475328241, 0.00022113732768583, 0.000235485770787934, 
    0.000458323057092208, 0.000652960109256795, 0.000724862646259261, 
    0.0727879614169974, 0.0343552622468531, 0.00966883214135748, 
    0.0121723948692721, 0.0502079899784375, 0.0642059286182179, 
    0.0744606748095006, 0.0279356500162451, 0.0165712179668264, 
    0.0155282557608783, 0.0161360297824822, 0.0176198016904319, 
    0.0025636247608102, 0.0011990003339988, 8.03074966867612e-05, 
    2.33672477744522e-05, 0.082133947866647, 0.000268818474649765, 
    9.9394282675768e-05, 5.50800067064597e-05, 0.0740561864383357, 
    0.0623723308152442, 0.0122016944534841, 0.00661733731518039, 
    0.000206774473028281, 0.000181997559545235, 0.000504582984323445, 
    0.0003864059666649, 0.260050134018791, 0.262172946886686, 
    0.18452199172589, 0.173786000484964, 0.0269969369566207, 
    0.0314904830827211, 0.00193643447967831, 0.00224489247051206, 
    3.93537896032742e-05, 1.28428312370815e-05, 0.0088546261393493, 
    0.0167354308377304, 1.21169704358824e-05, 2.12185497965253e-05, 
    6.62816031320301e-06, 1.43770979648094e-05, 1.55017664132774e-05, 
    4.91659313365037e-05, 0.000125001966670021, 6.22422609777641e-05, 
    2.67226640566305e-05, 3.7715397236691e-05, 0.000156595002030591, 
    0.000177342276671081, 9.9289511018095e-05, 3.26074531511788e-05, 
    0.0133283089983016, 0.016339336685079, 0.000374046362264717, 
    0.000314832095026693, 0.000101840989635774, 0.0499536313744285, 
    3.07770376000906e-05, 2.61927046816827e-05, 6.62816031320301e-06, 
    6.26578426928999e-06, 0.0190857963424119, 0.033561743200736, 
    0.00670074972877479, 0.00537788085712994, 1.18971385403729e-05, 
    1.11445994651961e-05, 6.62816031320301e-06, 6.26578426928999e-06, 
    1.14712208113288e-05, 2.05354865936513e-05, 7.60426873897666e-06, 
    6.26578426928999e-06, 1.20062319150955e-05, 1.36539983715138e-05, 
    6.62816031320301e-06, 7.2222843532018e-06, 9.9140609239925e-06, 
    9.94926827158573e-06, 7.53483093323745e-06, 8.69321376638133e-06, 
    1.21169704358824e-05, 1.19091870744721e-05, 6.62816031320301e-06, 
    7.23507396088365e-06, 2.46118496456816e-05, 2.17587837491135e-05, 
    6.62816031320301e-06, 6.26578426928999e-06, 1.82559375344064e-05, 
    1.89730194735652e-05, 6.62816031320301e-06, 6.26578426928999e-06, 
    0.00660850850197291, 0.0212222385456563, 0.00211118341459913, 
    0.00141420689575609, 9.26797200368037e-06, 8.90032999685065e-06, 
    7.54855398500617e-06, 6.26578426928999e-06, 2.23089983289096e-05, 
    2.17587837491135e-05, 6.62816031320301e-06, 6.26578426928999e-06, 
    5.45457596965578e-06, 5.45457596965578e-06, 6.62816031320301e-06, 
    5.98284905868666e-06, 3.2907497931271e-05, 2.32160410324433e-05, 
    6.62816031320301e-06, 1.51092819784857e-05, 1.21169704358824e-05, 
    1.19091870744721e-05, 7.91517227728171e-06, 7.23507396088365e-06, 
    4.18664545392701e-05, 4.14341191547126e-05, 0.000151073775168509, 
    0.000399628089283355, 9.9140609239925e-06, 1.4300121286468e-05, 
    7.53483093323745e-06, 6.26578426928999e-06), Level = structure(c(1L, 
    1L, 1L, 1L, 25L, 25L, 25L, 25L, 3L, 3L, 3L, 3L, 7L, 7L, 7L, 
    7L, 28L, 28L, 28L, 28L, 9L, 9L, 9L, 9L, 31L, 31L, 31L, 31L, 
    27L, 27L, 27L, 27L, 30L, 30L, 30L, 30L, 26L, 26L, 26L, 26L, 
    42L, 42L, 42L, 42L, 39L, 39L, 39L, 39L, 11L, 11L, 11L, 11L, 
    13L, 13L, 13L, 13L, 4L, 4L, 4L, 4L, 33L, 33L, 33L, 33L, 36L, 
    36L, 36L, 36L, 43L, 43L, 43L, 43L, 6L, 6L, 6L, 6L, 5L, 5L, 
    5L, 5L, 15L, 15L, 15L, 15L, 38L, 38L, 38L, 38L, 10L, 10L, 
    10L, 10L, 8L, 8L, 8L, 8L, 45L, 45L, 45L, 45L, 2L, 2L, 2L, 
    2L, 16L, 16L, 16L, 16L, 24L, 24L, 24L, 24L, 44L, 44L, 44L, 
    44L, 18L, 18L, 18L, 18L, 46L, 46L, 46L, 46L, 20L, 20L, 20L, 
    20L, 12L, 12L, 12L, 12L, 47L, 47L, 47L, 47L, 17L, 17L, 17L, 
    17L, 32L, 32L, 32L, 32L, 48L, 48L, 48L, 48L, 49L, 49L, 49L, 
    49L, 50L, 50L, 50L, 50L, 34L, 34L, 34L, 34L, 29L, 29L, 29L, 
    29L, 40L, 40L, 40L, 40L, 19L, 19L, 19L, 19L, 35L, 35L, 35L, 
    35L, 37L, 37L, 37L, 37L, 41L, 41L, 41L, 41L, 14L, 14L, 14L, 
    14L, 21L, 21L, 21L, 21L, 23L, 23L, 23L, 23L, 22L, 22L, 22L, 
    22L), .Label = c("Akkermansia", "Bacteroides", "Pseudomonas", 
    "Anaeroplasma", "Acetatifactor", "Lactobacillus", "Clostridium_XlVa", 
    "Oscillibacter", "Eubacterium", "Alistipes", "Acinetobacter", 
    "Parabacteroides", "Parasutterella", "Bacillus", "Clostridium_sensu_stricto", 
    "Turicibacter", "Clostridium_IV", "Staphylococcus", "Bifidobacterium", 
    "Romboutsia", "Escherichia/Shigella", "Listeria", "Enterococcus", 
    "Lactococcus", "Flavonifractor", "Ruminococcus", "Lachnospiracea_incertae_sedis", 
    "Clostridium_XlVb", "Stenotrophomonas", "Eisenbergiella", 
    "Hungatella", "Adhaeribacter", "Butyricicoccus", "Rhizobium", 
    "Prevotella", "Intestinimonas", "Sphingobium", "Dorea", "Enterorhabdus", 
    "Shewanella", "Bradyrhizobium", "Desulfovibrio", "Clostridium_XVIII", 
    "Anaerostipes", "Asaccharobacter", "Anaerotruncus", "Eggerthella", 
    "Butyricimonas", "Bilophila", "Anaerococcus"), class = "factor")), row.names = c(2L, 
3L, 4L, 5L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 15L, 17L, 18L, 19L, 
20L, 22L, 23L, 24L, 25L, 27L, 28L, 29L, 30L, 32L, 33L, 34L, 35L, 
37L, 38L, 39L, 40L, 42L, 43L, 44L, 45L, 47L, 48L, 49L, 50L, 52L, 
53L, 54L, 55L, 57L, 58L, 59L, 60L, 62L, 63L, 64L, 65L, 67L, 68L, 
69L, 70L, 72L, 73L, 74L, 75L, 77L, 78L, 79L, 80L, 82L, 83L, 84L, 
85L, 87L, 88L, 89L, 90L, 92L, 93L, 94L, 95L, 97L, 98L, 99L, 100L, 
102L, 103L, 104L, 105L, 107L, 108L, 109L, 110L, 112L, 113L, 114L, 
115L, 117L, 118L, 119L, 120L, 122L, 123L, 124L, 125L, 127L, 128L, 
129L, 130L, 132L, 133L, 134L, 135L, 137L, 138L, 139L, 140L, 142L, 
143L, 144L, 145L, 147L, 148L, 149L, 150L, 152L, 153L, 154L, 155L, 
157L, 158L, 159L, 160L, 162L, 163L, 164L, 165L, 167L, 168L, 169L, 
170L, 172L, 173L, 174L, 175L, 177L, 178L, 179L, 180L, 182L, 183L, 
184L, 185L, 187L, 188L, 189L, 190L, 192L, 193L, 194L, 195L, 197L, 
198L, 199L, 200L, 202L, 203L, 204L, 205L, 207L, 208L, 209L, 210L, 
212L, 213L, 214L, 215L, 217L, 218L, 219L, 220L, 222L, 223L, 224L, 
225L, 227L, 228L, 229L, 230L, 232L, 233L, 234L, 235L, 237L, 238L, 
239L, 240L, 242L, 243L, 244L, 245L, 247L, 248L, 249L, 250L), class = "data.frame")

enter image description here

1 个答案:

答案 0 :(得分:1)

我无法运行您的代码,因此我将使用虚拟数据回答您的问题。

让我们生成一些不均匀分布的数据,在这种情况下为字母。

set.seed(0)
df <- data.frame(x = LETTERS[c(rpois(100, 1) + 1, rpois(100, 8) + 1)])

我们可以确定8个最频繁出现的字母:

most_freq <- names(tail(sort(table(df$x)), 8))

然后我们可以将其中任何一个离散量表函数(包括手动尺度函数)作为breaks参数:

ggplot(df, aes(x, fill = x)) +
  geom_bar() +
  scale_fill_discrete(breaks = most_freq)

enter image description here

并且您可以看到,不在休息时间中的类别仍然带有颜色,但在图例中没有提及。