简体   繁体   English

使用ggplot2添加额外的图例信息

[英]Adding extra legend information using ggplot2

I'm trying to add extra information to the legend in this stacked bar plot, but the information comes from a different data frame. 我正在尝试在此堆积的条形图中为图例添加额外的信息,但是该信息来自不同的数据框。 What I want to do is to add a legend line (beneath the others) that has a solid dot and the text is "Recorded Contaminant Values". 我想做的是添加一个带有实心圆点的图例行(在其他行下方),文本为“记录的污染物值”。 I've tried all kinds of stuff, and nothing has worked. 我尝试了各种东西,但没有任何效果。 Here is the code I have so far: 这是我到目前为止的代码:

library(ggplot2)

columns <- unlist(lapply(1:4, function(x) rep(x,32)))
labels <- c("SuperDeduper", "AdapterTrimmer", "QWindowTrim", "Reads Remaining")
diffs <- c(35666631, 36774314, 36193131, 36140701, 41584694, 33888805, 
41314700, 36430192, 36164379, 37684733, 36156080, 33443533, 28441368, 
33025395, 31980771, 29354621, 29042522, 33575127, 34144001, 35359005, 
34037116, 34030228, 30695581, 27310435, 33448959, 39547820, 37183407, 
40013726, 35679976, 33506154, 31546743, 33917856, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49237692, 
50180374, 47276553, 48635427, 54147578, 39709279, 47969795, 44230737, 
52976902, 55651916, 55788100, 54082912, 50965229, 48481376, 54821029, 
55814879, 50763305, 54242897, 57001053, 54111642, 50922042, 55563781, 
48964424, 46377355, 46074343, 57514467, 51998501, 58061503, 55020584, 
54041618, 48210015, 55463206)

samps <- c("CHOR1", "CHOR10", "CHOR2", "CHOR3", "CHOR4", "CHOR5", "CHOR6", 
"CHOR7", "CHOR8", "CHOR9", "CPC1", "CPC10", "CPC11", "CPC2", 
"CPC3", "CPC4", "CPC5", "CPC6", "CPC7", "CPC8", "CPC9", "CPP1", 
"CPP2", "CPP3", "CPP4", "CPP5", "CPP6", "NB1", "NB2", "NB3", 
"NB4", "NB5", "CHOR1", "CHOR10", "CHOR2", "CHOR3", "CHOR4", "CHOR5", 
"CHOR6", "CHOR7", "CHOR8", "CHOR9", "CPC1", "CPC10", "CPC11", 
"CPC2", "CPC3", "CPC4", "CPC5", "CPC6", "CPC7", "CPC8", "CPC9", 
"CPP1", "CPP2", "CPP3", "CPP4", "CPP5", "CPP6", "NB1", "NB2", 
"NB3", "NB4", "NB5", "CHOR1", "CHOR10", "CHOR2", "CHOR3", "CHOR4", 
"CHOR5", "CHOR6", "CHOR7", "CHOR8", "CHOR9", "CPC1", "CPC10", 
"CPC11", "CPC2", "CPC3", "CPC4", "CPC5", "CPC6", "CPC7", "CPC8", 
"CPC9", "CPP1", "CPP2", "CPP3", "CPP4", "CPP5", "CPP6", "NB1", 
"NB2", "NB3", "NB4", "NB5", "CHOR1", "CHOR10", "CHOR2", "CHOR3", 
"CHOR4", "CHOR5", "CHOR6", "CHOR7", "CHOR8", "CHOR9", "CPC1", 
"CPC10", "CPC11", "CPC2", "CPC3", "CPC4", "CPC5", "CPC6", "CPC7", 
"CPC8", "CPC9", "CPP1", "CPP2", "CPP3", "CPP4", "CPP5", "CPP6", 
"NB1", "NB2", "NB3", "NB4", "NB5")

dffs <- structure(list(x = structure(1:32, .Label = c("CHOR1", "CHOR10", 
"CHOR2", "CHOR3", "CHOR4", "CHOR5", "CHOR6", "CHOR7", "CHOR8", 
"CHOR9", "CPC1", "CPC10", "CPC11", "CPC2", "CPC3", "CPC4", "CPC5", 
"CPC6", "CPC7", "CPC8", "CPC9", "CPP1", "CPP2", "CPP3", "CPP4", 
"CPP5", "CPP6", "NB1", "NB2", "NB3", "NB4", "NB5"), class = "factor"), 
    y = c(0.0544709955463634, 0.0596702158255113, 0.0586614896014222, 
    0.0624578654972305, 0.0627572173362813, 0.0652714546210198, 
    0.0720983861755616, 0.0721253780749289, 0.0663543751407387, 
    0.0550210025217426, 0.058055876946208, 0.0600278464411527, 
    0.0614602587742175, 0.0647506573410938, 0.044467464960404, 
    0.0463054497208508, 0.0540317062311753, 0.0623795178994235, 
    0.0609709990407159, 0.0660813707986263, 0.0564277955767876, 
    0.0657846664725093, 0.0702825464296669, 0.056445470816807, 
    0.0676118554533865, 0.06639558163306, 0.0850892537531267, 
    0.0593398767389062, 0.0896267454136998, 0.047540684416275, 
    0.101005522315739, 0.0840679762789124)), .Names = c("x", 
"y"), row.names = c(NA, -32L), class = "data.frame")


ggplot() + 
      geom_bar(data=data, aes(fill=columns, y=diffs, x=samps), stat="identity", position="fill") + 
      scale_fill_discrete(name="Preprocessing Steps\n(Dots are recorded\ncontaminant values)",labels=labels) +
      theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
      xlab("Samples") + ylab("Fractions of all reads") +
      geom_point(data=dffs, aes(x=x,y=y))

I'm not quite sure what you mean with to add a legend line. 我不确定您添加图例行的含义。 Do you maybe mean that you want a key in the legend for the dots that co-exists with the keys of the preprocessing steps? 您是否意味着要在图例中找到与预处理步骤的键共存的点的键?

If that is the case I would recommend mapping the geom_point() colour to a variable, which can exist outside any data.frame: 如果是这种情况,我建议将geom_point()颜色映射到一个变量,该变量可以存在于任何data.frame之外:

ggplot() + 
  geom_bar(aes(fill=columns, y=diffs, x=samps), stat="identity", position="fill") + 
  scale_fill_discrete(name="Preprocessing Steps",labels=labels,
                      # Order set to 1 to appear before dot
                      guide = guide_legend(order = 1)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  xlab("Samples") + ylab("Fractions of all reads") +
  # Add a colour mapping
  geom_point(data=dffs, aes(x=x,y=y, colour = "Recorded\nContaminant\nValues")) +
  # You can colour the dot how you like
  scale_colour_manual(values = "red", name = "")

Which gave me the following plot: 这给了我以下情节:

在此处输入图片说明

Is that close to what you wanted? 那接近您想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM