ggplot2: Change color of background for geom_vline legend

Rebecca Eliscu

I'm trying to follow the code suggested in this post, but to no avail.

Specifically, I would like the legend associated with the red vertical line to have a white background.

Some toy data:

df1 <- data.frame(correlation = c(rnorm(1000, 0, 0.6), rnorm(1000, 0, 0.1)), type = c(rep("Real", 1000), rep("Permuted", 1000)))

corSig <- 0.24542

My code:

ggplot(df1, aes(correlation, fill = type)) +
    geom_density(alpha = .5) +
    geom_vline(aes(xintercept = signif(corSig, 2), linetype = 'FDR = .05'), colour = 'red') +
    theme(plot.title = element_text(hjust = .5),
          plot.subtitle = element_text(hjust = .5),
          legend.title = element_blank(),
          legend.position = c(.8, .8),
          panel.background = element_blank()) +
    guides(linetype = guide_legend(override.aes = list(fill = "#000000"))) +
    ggtitle("Gene Expression Correlation", subtitle = paste(nrow(datExpr), "genes,", ncol(datExpr), "bulk sections")) +
    xlab("Correlation") +
    ylab("Density")

enter image description here

Carson Keeter

There doesn't seem to be a consistent solution for this issue (for me, at least) but I've got a solution here:

library(tidyverse)

df1 <- data.frame(correlation = c(rnorm(1000, 0, 0.6), rnorm(1000, 0, 0.1)), type = c(rep("Real", 1000), rep("Permuted", 1000)))

corSig <- 0.24542

ggplot(df1, aes(correlation, fill = type)) +
    geom_density(alpha = .5) +
    geom_vline(aes(xintercept = signif(corSig, 2), linetype = 'FDR = .05'), colour = 'red') +
    theme(
        plot.title = element_text(hjust = .5),
        plot.subtitle = element_text(hjust = .5),
        legend.title = element_blank(),
        legend.position = c(.8, .8),
        panel.background = element_blank(),
        legend.key = element_rect(colour = "transparent", fill = "transparent")) +
    ggtitle("Gene Expression Correlation", subtitle = paste(nrow(df1), "genes,", ncol(df1), "bulk sections")) +
    xlab("Correlation") +
    ylab("Density")

Fixed Gene Expression Correlation Plot

In short, I removed ...guides(linetype = guide_legend(override.aes = list(fill = "#000000")))... and added ...legend.key = element_rect(colour = "transparent", fill = "transparent"))...

Hopefully that helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

incorrect linetype in geom_vline with legend in r ggplot2

From Dev

Duplicate legend with geom_vline() in ggplot

From Dev

Duplicate legend with geom_vline() in ggplot

From Dev

R - customizing legend in ggplot2 to add geom_vline() component

From Dev

Add legend to geom_vline

From Dev

How to change background colour of legend in ggplot2?

From Dev

How to change the color legend in fine granularity using ggplot2

From Dev

How to background geom_vline and geom_hline in ggplot 2 in a bubble chart

From Dev

R - Manual legend color in geom_line ggplot2

From Dev

ggplot2: how to assign a graduated color legend and add shaded relief background to a map

From Dev

ggplot2: how to assign a graduated color legend and add shaded relief background to a map

From Dev

change color for two geom_point() in ggplot2

From Dev

Why can't I change the background color of a ggplot legend box to white? (Other colors work fine)

From Java

Mixed fill color in ggplot2 legend using geom_smooth() in R

From Dev

Matching geom_text color to elements in plot and removing legend title in ggplot2

From Dev

ggplot2: Change factor order in legend

From Dev

Cannot change legend in ggplot2

From Dev

ggplot2: Change legend symbol

From Dev

Change the symbol in a legend key in ggplot2

From Dev

change labels in legend for ggplot2

From Dev

ggplot2: Change legend symbol

From Dev

change labels in legend for ggplot2

From Dev

Legend showing an unexpected black line with geom_vline

From Dev

Change the color of legend elements in ggplot (R)

From Dev

matplotlib legend background color

From Dev

Legend with ggplot2 and geom_point

From Dev

Legend with ggplot2 and geom_point

From Dev

How to separate geom_vline() and geom_hline() legends from other legends in ggplot2

From Dev

Change color in barchart legend

Related Related

  1. 1

    incorrect linetype in geom_vline with legend in r ggplot2

  2. 2

    Duplicate legend with geom_vline() in ggplot

  3. 3

    Duplicate legend with geom_vline() in ggplot

  4. 4

    R - customizing legend in ggplot2 to add geom_vline() component

  5. 5

    Add legend to geom_vline

  6. 6

    How to change background colour of legend in ggplot2?

  7. 7

    How to change the color legend in fine granularity using ggplot2

  8. 8

    How to background geom_vline and geom_hline in ggplot 2 in a bubble chart

  9. 9

    R - Manual legend color in geom_line ggplot2

  10. 10

    ggplot2: how to assign a graduated color legend and add shaded relief background to a map

  11. 11

    ggplot2: how to assign a graduated color legend and add shaded relief background to a map

  12. 12

    change color for two geom_point() in ggplot2

  13. 13

    Why can't I change the background color of a ggplot legend box to white? (Other colors work fine)

  14. 14

    Mixed fill color in ggplot2 legend using geom_smooth() in R

  15. 15

    Matching geom_text color to elements in plot and removing legend title in ggplot2

  16. 16

    ggplot2: Change factor order in legend

  17. 17

    Cannot change legend in ggplot2

  18. 18

    ggplot2: Change legend symbol

  19. 19

    Change the symbol in a legend key in ggplot2

  20. 20

    change labels in legend for ggplot2

  21. 21

    ggplot2: Change legend symbol

  22. 22

    change labels in legend for ggplot2

  23. 23

    Legend showing an unexpected black line with geom_vline

  24. 24

    Change the color of legend elements in ggplot (R)

  25. 25

    matplotlib legend background color

  26. 26

    Legend with ggplot2 and geom_point

  27. 27

    Legend with ggplot2 and geom_point

  28. 28

    How to separate geom_vline() and geom_hline() legends from other legends in ggplot2

  29. 29

    Change color in barchart legend

HotTag

Archive