1

Similar to the recent post (see: How to remove boxes around ggplot2 legend labels), I am not able to get rid of the boxes around the symbols of in the legend. Here the code and below some of the data and the plot:

theme_opts_V5 <- list(theme(
  panel.grid.minor.x = element_line(color = "transparent"),
  panel.grid.major.x = element_line(color = "transparent"),
  panel.grid.minor = element_line(color = "grey"),
  panel.grid.major = element_line(color = "grey"),
  panel.background = element_blank(),
  plot.background = element_rect(),
  panel.border = element_blank(),
  axis.line = element_line(colour = "black")
))

ggplot(data, aes(x = value, y = old_value, fill = category, color = category)) +
  geom_hline(
    yintercept = 0, linetype = "solid",
    color = "#898800", size = 1
  ) +
  geom_vline(
    xintercept = 57, linetype = "solid",
    color = "#898800", size = 1
  ) +
  geom_text(aes(x = 61, label = "Mean", y = 15), colour = "#898800", angle = 0, size = 3.4) +
  geom_point(size = 3, shape = 23) +
  labs(
    title = "Title",
    subtitle = "Subtitle",
    caption = "Source"
  ) +
  xlab("new, in %") +
  ylab("change to old") +
  scale_color_manual(values = c("#440154", "#3b528b")) +
  scale_fill_manual(values = c("#440154", "#3b528b")) +
  theme(
    legend.title = element_blank(),
    legend.position = "bottom",
    legend.key = element_rect(fill = NA)
  ) +
  theme(legend.background = element_blank()) +
  theme_opts_V5 +
  scale_x_continuous(limits = c(30, 80), breaks = seq(30, 80, by = 10), expand = c(0, 0)) +
  scale_y_continuous(limits = c(-30, 20), breaks = seq(-30, 20, by = 10), expand = c(0, 0))

Here you find a snippet of my data:

structure(list(city = c("Aadorf", "Affeltrangen", "Altnau", "Amlikon-Bissegg", 
"Amriswil", "Arbon", "Basadingen-Schlattingen", "Berg", "Berlingen", 
"Bettwiesen", "Bichelsee-Balterswil", "Birwinken", "Bischofszell", 
"Bottighofen", "Braunau", "Bürglen", "Bussnang", "Diessenhofen", 
"Dozwil", "Egnach"), value = c(55L, 48L, 60L, 70L, 63L, 76L, 
52L, 42L, 53L, 49L, 49L, 57L, 70L, 36L, 49L, 69L, 47L, 47L, 44L, 
57L), old_value = c(4L, 2L, -1L, 0L, 0L, 0L, 3L, -3L, -2L, 6L, 
5L, -11L, -2L, -3L, -3L, 6L, -3L, 0L, -8L, -3L), category = c("low", 
"low", "low", "low", "high", "high", "low", "low", "low", "low", 
"low", "low", "low", "low", "low", "low", "low", "low", "low", 
"low")), row.names = c(NA, 20L), class = "data.frame")

And the plot:

Plot

Thanks for any help!

Sorry for the incompleteness of the post. Here is the code for theme_opts_V5 that only defines background and grid lines of the plot: `

theme_opts_V5 <- list(theme(panel.grid.minor.x = element_line(color = "transparent"),
                            panel.grid.major.x = element_line(color = "transparent"),
                            panel.grid.minor = element_line(color = "grey"),
                            panel.grid.major = element_line(color = "grey"),
                            panel.background = element_blank(),
                            plot.background = element_rect(),
                            panel.border = element_blank(),
                            axis.line = element_line(colour = "black")))

```

Thank you in advance!
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. Please do not post an image of code/data/errors [for these reasons](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). To post your data type e.g. `dput(head(NAME_OF_DATASET, 20))` into the console and copy the output starting with `structure(....` into your post. – stefan Jan 02 '23 at 14:58
  • Moreover it would be useful to know which theme you apply and which adjustments are included in `theme_opts_V5`. My guess would be that the outline around your legend keys are a result of the theme options set. – stefan Jan 02 '23 at 15:00
  • 1
    Sorry for the incompleteness of the post. Here is the code for theme_opts_V5 that only defines background and grid lines of the plot: – user11841097 Jan 03 '23 at 06:50
  • Thanks for providing the data. Unfortunately I can't repdroduce your issue. When I run you code I don't get any boxes drawn around the the legend keys. First I would do is to run the code in a fresh R session. Also it might be due to a different ggplot2 version, i.e. I use `3.4.0` (you could check the version via `packageVersion("ggplot2")`). Finally, if my guess was right that it is related to the theme, you could try with `legend.key = element_rect(fill = NA, color = NA)`. – stefan Jan 03 '23 at 08:35
  • Thank you for your answer. I use ggplot2 version 3.3.2. I've already tried your hint, was my first guess, but did not work. However refreshing helped. R is sometimes strange :). – user11841097 Jan 03 '23 at 09:05
  • OP, are you using one of the associated theme packages with ggplot (ex. `ggtheme`, `hrbrthemes`, etc)? Your graphic shown is not created by the plot code alone unless you also have set elsewhere some default ggplot theme, and this could be the culprit behind your issue here which is drawing the line around the keys by default. Try `theme_set(theme_bw())` after `scale_fill_manual(...)` and see if you still get box outlines. – chemdork123 Jan 04 '23 at 22:02

0 Answers0