I have a line chart with an ribbon between first and third quartil of the data. When I plot the data with ggplot2 I get boxes around the labels which I don't get rid of whatever I try (i. e. theme(legend.background=element_blank()) or guides(guides_legend) with override). dataframe Here are the first rows of my dataset and the code:
mutate(code_range = fct_reorder2(code_range, year, order)) %>%
ggplot(aes(x=year, y=value, group=code_range, color=code_range))
geom_ribbon(aes(ymin = min_range,
ymax = max_range), fill = "grey70", alpha=0.1)
geom_line(size=1)
labs(title="Title",
subtitle=paste0("subtitle"),
x="",y="",
caption="Source")
scale_colour_manual(values = c("min" = "#878787", "q1" = "#B5B5B5", "median" = "#27408B",
"q3" = "#B5B5B5", "max" = "#878787"))
scale_fill_manual(values = c("min" = "#FFFFFF", "q1" = "#B5B5B5", "median" = "#27408B",
"q3" = "#B5B5B5", "max" = "#878787"))
theme_opts
theme(legend.title = element_blank(),
legend.position = "bottom")
theme(legend.background=element_blank())
plot
Does anybody know a solution how to remove those boxes?
CodePudding user response:
The boxes around the legend keys reflect the geom_ribbon
. To remove them you could add show.legend=FALSE
to geom_ribbon
.
Using some fake example data:
library(ggplot2)
df <- data.frame(
year = 2005:2020,
value = 1:16,
min_range = 1:16 - 1,
max_range = 1:16 1
)
base <- ggplot(df, aes(year, value, color = "median"))
geom_line()
scale_colour_manual(values = c(
"min" = "#878787", "q1" = "#B5B5B5", "median" = "#27408B",
"q3" = "#B5B5B5", "max" = "#878787"
))
First replicating your issue:
base
geom_ribbon(aes(ymin = min_range, ymax = max_range), fill = "grey70", alpha = 0.1)
And second using show.legend = FALSE
:
base
geom_ribbon(aes(ymin = min_range, ymax = max_range), fill = "grey70", alpha = 0.1, show.legend = FALSE)
CodePudding user response:
Another option is to move the color aesthetic only to geom_line
library(ggplot2)
p <- ggplot(iris, aes(Sepal.Length, Petal.Width, color = Species))
geom_smooth()
df_p <- layer_data(p)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## something similar to your plot
ggplot(df_p, aes(color = as.character(group)))
geom_ribbon(aes(x = x, ymin = ymin, ymax = ymax))
geom_line(aes(x, y))
## change the location of your color aesthetic to geom_line only
## you need to add a grouping aesthetic into the ribbon call
ggplot(df_p)
geom_ribbon(aes(x = x, ymin = ymin, ymax = ymax, group = as.character(group)))
geom_line(aes(x, y, color = as.character(group)))
Created on 2023-01-02 with reprex v2.0.2
CodePudding user response:
Would you have a solution to this code as well? Here I have the same issue with the boxes around the legend symbols:
plot.scatter <- 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)) ```
[![Plot][1]][1]
Thank you in advance.
[1]: https://i.stack.imgur.com/Mlqn5.png