How can I completely remove legend.key from ggplot2 legend?

Vinícius Neres

I'm plotting a graph and I need to completely remove the legend.key from my ggplot legend. Why I need to do this? The legend starts with a number that reference the X axis breaks, and the label its too large and I don't want to keep it in the X axis. So, in the X axis i put breaks=1:15, and in legend the label starts with this numbers.

GRAPH TAHT I'M WORKING WITH

In resume, I just want to remove the legend.key from my graph. Is it possible? I have tried legend.key=element_blank(), but without sucess.

Obs.: In the code is it possible to see that I don't want the fill=legto change the colors of each bar. Everything is set to be gray and I just want to remove de legend.key.

ggplot(IC_QS, aes(x=ind,y=values))+
  geom_boxplot(aes(fill=leg),color="black", outlier.colour = "red")+
  labs(title = "XXXXXXXXXX", 
       subtitle = "XXXXXXXXXXX",
       caption = "XXXXXXXXXXXXX")+
  scale_x_discrete(name = "", labels=1:15)+
  scale_y_continuous(name = "XXX", breaks = seq(0,10,1), expand = c(0,0.08*max(IC_QS$values)))+
  scale_fill_manual(name="Sectors", values = rep("gray", 15), labels=str_wrap(IC_QS_leg,25))+
  theme(legend.position = "right", legend.background = element_blank(), 
        legend.key = element_blank(),legend.text = element_text(face = "bold", size = 8,),
        panel.background = element_blank(), panel.grid.major = element_line(colour = "gray", linetype = "dashed"),
        axis.title.x = element_text(face = "bold",vjust = -1), axis.title.y = element_text(face="bold", vjust = +1.5),
        axis.text = element_text(colour="black", face = "bold"), title = element_text(face = "bold"))

Allan Cameron

Obviously we don't have your data, but here's an idea using the iris built-in data set

ggplot(iris, aes(Species, Petal.Width, fill = Species)) +
  geom_boxplot() +
  scale_x_discrete(labels = seq(length(levels(iris$Species)))) +
  scale_fill_manual(values = rep("grey", length(levels(iris$Species))),
                    labels = paste(seq(length(levels(iris$Species))),
                                   levels(iris$Species), sep = " - ")) +
  guides(fill = guide_legend(override.aes = list(color = NA, fill = NA))) +
  theme_light(base_size = 16) +
  theme(legend.key.width = unit(0, "mm"))

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I replace legend key in ggplot2?

r How to remove some parameters from the legend in ggplot2

How to remove percentage breakdowns from a legend created by ggplot2

How can I change the legend linetype in ggplot2?

How can I edit legend using ggplot2 in R?

How can I change the legend labels in R (ggplot2)

Remove errorbars from legend (ggplot2)

Remove grey from legend in ggplot2

How can I remove the legend symbol for a SINGLE legend in Highcharts?

How to remove scientific notation from this legend ggplot

How do I change the shape of the legend key glyph to a hexagon in ggplot2?

ggplot2: how to remove slash from geom_density legend

How can I add a legend to this plot (with ggplot())?

how to remove the letters under the dots in the ggplot2 legend?

Ggplot2 - I can't insert the chart legend

Can I group items in a legend in ggplot2?

How can I match the colors of lines and legend entries in ggplot2?

How can I manually place the NA entry in R ggplot2 discrete map legend?

How can I add a different legend for different quantiles in the same graph in ggplot2?

How can I edit the common legend title name using ggplot2 and ggpubr?

How can I add the legend for a line made with stat_summary in ggplot2?

How can I add a legend that counts points above or below a certain value in ggplot2? Volcano Plot

How can I combine two ggplot2 custom functions makig the legend separate

Remove n legend from ggplot

How to remove the legend key border in ggplot's geom_sf?

How to remove the gap in the ggplot legend?

How to extract the legend labels from a ggplot2 object?

ggplot2 remove legend removes annotations

change the key labels in a legend in ggplot2