0

I have a plot in ggplot and I have added an abline to show where the significance cut off is after multiple correction but the legend for the line is not displaying separate to the first legend displaying the domain of my variables. Instead it just plots a dotted line over the key for each domain. I want a second box with a dashed black line titled labelled "FDR Threshold" and I don't want the first legend box to have its color values filled with dashed black lines.

  geom_abline(aes(slope=0,intercept=-log10(c(var)[astsa::FDR(c(var))]),lty='FDR Correction'), 
              linetype = "dashed", show.legend = TRUE)+

enter image description here

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
Angus Campbell
  • 563
  • 4
  • 19
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 07 '21 at 21:49
  • Possibly related: https://stackoverflow.com/questions/38588925/have-separate-legends-for-a-set-of-point-line-plots-and-a-vertical-line-plot – MrFlick Jun 07 '21 at 21:50
  • That solution is deprecated. ggplot does not use guide any more. – Angus Campbell Jun 07 '21 at 22:02
  • I'm not sure what you mean. `ggplot2` definitely still uses guides. The documentation for them can be found here: https://ggplot2.tidyverse.org/reference/index.html#section-guides-axes-and-legends and `scale_fill_manual` still has a `guide=` parameter: https://ggplot2.tidyverse.org/reference/scale_manual.html – MrFlick Jun 07 '21 at 22:05
  • Oh my mistake, show_guide is deprecated, thank you for correcting me. I will try this solution. – Angus Campbell Jun 07 '21 at 22:29
  • It doesnt quite work its throwing errors, I can't quite get it to gel with my previous code – Angus Campbell Jun 07 '21 at 22:52

1 Answers1

2

I believe this could be accomplished by mapping the abline linetype within "aes", thus creating a legend. I use scale_linetype_manual to assign a dashed line to the category called "legend."

library(ggplot2)
ggplot(mtcars, aes(wt, mpg, color = as.character(gear))) +
  geom_point() +
  geom_abline(aes(intercept = 10, slope = 3, linetype = "threshold")) +
  scale_linetype_manual(values = c("threshold" = "dashed"))

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Thank you, when i try to implement this howevr I get an error: Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet Also is there a way to change the legend title from linetype or remove it? – Angus Campbell Jun 07 '21 at 23:20
  • 1
    Try `scale_linetype_manual(values = c("threshold" = "dashed"), name = NULL)` – Jon Spring Jun 07 '21 at 23:22
  • Your error looks related to a text layer, are you sure it's related? – Jon Spring Jun 07 '21 at 23:23
  • There's a lot of other code I didn't add here because I didn't think it was relevant, its entirely possible it is a text layer problem. The script could be interacting in weird ways. I've noticed depending on where certain lines are in the code it will change the plot or be ignored. – Angus Campbell Jun 08 '21 at 00:38