Ggplot Legend Showing Transparency and Fill Color

ggplot2 make legend key fill transparent

You could trick it if you want. Add a second geom_smooth(). The first with a confidence band and you don't show the legend. With the second one you remove the band but show the legend.

df$Color <- "Red"
df1 <- df
(plot = ggplot() +
geom_smooth(data=df, aes(data1, data2,colour=Color), se = TRUE, show.legend = FALSE) +
geom_smooth(data=df1, aes(data1, data2,colour=Color), se=FALSE) +
geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
scale_x_continuous(expand=c(0,0), limits=c(0,100)) +
scale_y_continuous(expand=c(0,0), limits=c(0,100))+
theme_classic()+
labs(y="data2", x="data1",
title="sample 1 data1 vs data2") +
theme(plot.title = element_text(size=18, face="bold"),
legend.key = element_rect(colour = "transparent", fill = "white"),
legend.justification = c(1,0), legend.position = c(1,0))+
scale_color_discrete(name="Sample"))

Sample Image

ggplot legend showing transparency and fill color

You can override the aesthetics in the legend by adding:

+ guides(fill = guide_legend(override.aes= list(alpha = 0.4)))

to your ggplot call.

But as with most things in ggplot, it is probably simpler to arrange your data in a way that makes the multiple geom_ribbon calls unnecessary:

dt1 <- data.frame(x = c(x,x),
ymin = c(y1,y3),
ymax = c(y2,y4),
grp = rep(c('red','blue'),each = 10))
ggplot(data = dt1,aes(x = x,ymin = ymin, ymax = ymax,fill = grp)) +
geom_ribbon(alpha = 0.4) +
scale_fill_manual(name = "legendname",
values = c('red','blue'),
labels = c('one','two'))

Sample Image

ggplot legend key color and transparency

Your approach is perfectly fine except for one small issue: you need

guides(color = guide_legend(override.aes = list(alpha = 1)))

instead as geom_line doesn't have the fill aesthetic.

Controlling the 'alpha' level in a ggplot2 legend

You can control semitransparency with function alpha() from package scales by providing color and alpha value. This function can be used inside element_rect() when you provide color for fill=.

library(scales)    
p<-ggplot(iris,aes(Petal.Length,Petal.Width,color=Species))+geom_point()
p+theme(legend.position=c(1,1),legend.justification=c(1,1),
legend.direction="vertical",
legend.box="horizontal",
legend.box.just = c("top"),
legend.background = element_rect(fill=alpha('blue', 0.4)))

Sample Image

Remove point transparency in ggplot2 legend

You can use function guides() and override.aes= to set alpha value just for legend entries.

ggplot(df) + geom_point(aes(x, y, color=z), alpha=0.1)+
guides(colour = guide_legend(override.aes = list(alpha=1)))

How to apply legend.key backround transparent in ggplot?

You need to either add se = FALSE or remove stat_smooth(method = "loess") to remove the grey filled background in vehicle model legend keys.

library(ggplot2)

p <- ggplot(data = mpg, mapping = aes(x = cty, y = hwy, colour = class)) +
geom_point(aes(size = displ), alpha = 0.5) +
stat_smooth(method = "loess", se = FALSE) + # Add se = FALSE
scale_size_continuous(range = c(4, 10)) +
facet_wrap(~year, ncol = 2) +
labs(
x = "distance per gallon in city", y = "distance per gallon in highway",
title = "fuel consumption and model", size = "displacement", colour = "vehicle model"
)

p +
theme(
plot.background = element_blank(),
panel.background = element_rect(fill = "transparent", colour = "gray"),
panel.border = element_rect(fill = "transparent", colour = "black"),
axis.text = element_text(color = "black"),
panel.grid.major = element_line(colour = "grey", linetype = "dashed"),
strip.background = element_blank(),
panel.spacing.x = unit(4, "mm"),
legend.box.background = element_rect(fill = "blue", colour = "transparent"),
legend.key = element_rect(fill = "transparent", colour = "transparent")
)

Sample Image

p +
theme(
plot.background = element_rect(fill = "transparent", colour = "transparent"),
panel.background = element_rect(fill = "transparent", colour = "gray"),
panel.border = element_rect(fill = "transparent", colour = "black"),
axis.text = element_text(color = "black"),
panel.grid.major = element_line(colour = "grey", linetype = "dashed"),
strip.background = element_rect(fill = "transparent", colour = "transparent"),
panel.spacing.x = unit(4, "mm"),
legend.background = element_blank(),
legend.box.background = element_blank(),
legend.key = element_blank()
)

Sample Image

Created on 2018-11-03 by the reprex package (v0.2.1.9000)

Adding background transparency and custom ordering in legend in ggplot

Legend backgrounds are always a bit tricky. As we have to deal with multiple elements. Also I'm not 100% sure what you mean by add background transparency. But the first thing I did was to remove the color and the fill for the legend.background, i.e. the box drawn around each legend (whereas legend.box.background is the background of the box drawn around all legends (; BTW: There is also the background of the single legend keys which is set via legend.key. :D ).

The easy part is the ordering of the legends which could be set via the order argument of guide_legend.

library(ggplot2)
library(ggnewscale)

data <-
structure(list(grp1 = c("X", "X", "X", "X", "Y", "Y", "Y", "Y"), grp2 = c("A", "B", "C", "D", "A", "B", "C", "D"), val = c(
1,
2, 3, 4, 3, 4, 5, 6
)), row.names = c(NA, -8L), class = "data.frame")

col_define <- c("red", "orange", "blue", "lightblue")
names(col_define) <- c("A", "B", "C", "D")

ggplot(data, aes(x = grp1, group = grp2, y = val)) +
geom_col(aes(fill = grp2)) +
scale_fill_manual(values = col_define, breaks = c("A", "B"), name = "1", guide = guide_legend(order = 2)) +
new_scale_fill() +
geom_col(aes(fill = grp2)) +
scale_fill_manual(values = col_define, breaks = c("C", "D"), name = "2", guide = guide_legend(order = 1)) +
theme(
legend.position = "top",
legend.direction = "vertical",
legend.box.margin = margin(),
legend.background = element_rect(color = NA, fill = NA),
legend.box.background = element_rect(
fill = alpha('#e5e5e5', .6), size = 0.1, linetype = "solid", color = "#333333"
)
)

Sample Image



Related Topics



Leave a reply



Submit