Change the symbol in a legend key in ggplot2

Julian Stander

This R code produces a ggplot2 graph in which the legend key contains the letter "a" repeated in red, blue and green.

x <- rnorm(9); y <- rnorm(9); s <- rep(c("F","G","K"), each = 3)
df <- data.frame(x, y, s)

require(ggplot2)
ggplot(df, aes(x = x, y = y, col = s, label = s)) + 
geom_text() +
scale_colour_discrete(name = "My name", breaks = c("F","K","G"), labels = c("Fbig","Kbig","Gbig")) 

I would like to replace the repeated "a" in the legend key with "F", "K" and "G".

Is this possible please? Thank you.

Sandy Muspratt

Adapting code for this answer: The idea is to inhibit the geom_text legend, but to allow a legend for geom_point, but make the point size zero so the points are not visible in the plot, then set size and shape of the points in the legend in the guides statement

x <- rnorm(9); y <- rnorm(9); s <- rep(c("F","G","K"), each = 3)
df <- data.frame(x, y, s)
#
require(ggplot2)
#
ggplot(df, aes(x = x, y = y, colour = s, label = s)) +
   geom_point(size = 0, stroke = 0) +  # OR  geom_point(shape = "") +
   geom_text(show.legend = FALSE) +
   guides(colour = guide_legend(override.aes = list(size = 5, shape = c(utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"))))) +
   scale_colour_discrete(name = "My name", breaks = c("F","K","G"), labels = c("Fbig","Kbig","Gbig")) 

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

From Dev

ggplot2: Change legend symbol

From Dev

ggplot2: Change legend symbol

From Dev

ggplot2: Change factor order in legend

From Dev

Cannot change legend in ggplot2

From Dev

change labels in legend for ggplot2

From Dev

change labels in legend for ggplot2

From Dev

Remove default legend symbol "a" from ggplot2

From Dev

Customising legend size-symbol items in ggplot2

From Dev

Change Symbol Borders in R Legend

From Dev

Highcharts: change legend symbol runtime

From Dev

How to change legend symbol in dxChart

From Dev

ggplot2: applying width of line to the legend key

From Dev

ggplot2 make legend key fill transparent

From Dev

How can I replace legend key in ggplot2?

From Dev

Change order of items in ggplot2 legend with combined items

From Dev

How can I change the legend geometry in ggplot2

From Dev

How to change the color legend in fine granularity using ggplot2

From Dev

How to change angle of line in customized legend in ggplot2

From Dev

How to change legend label in "theme" argument in ggplot2?

From Dev

How to change background colour of legend in ggplot2?

From Dev

How to change the size of legend text in ggplot2?

From Dev

Is it possible to change the legend size in base R (no ggplot2)

From Dev

ggplot2: Change color of background for geom_vline legend

From Dev

How to change legend label in "theme" argument in ggplot2?

From Dev

Legend in ggplot2

From Dev

How to superscript a - (minus symbol) in a legend title with qpolot in ggplot2 in R

From Dev

Highcharts - Error bar change legend symbol

From Dev

Change ggplot legend title

From Dev

Extracting and plotting facet_wrap elements along with same legend key with ggplot2

Related Related

HotTag

Archive