Reputation: 15458
Following is the sample code for the random chart:
library(ggplot2)
ggplot(mtcars,aes(x=mpg,y=wt,color=factor(vs)))+geom_line()+theme(legend.position="top",legend.direction = "horizontal",legend.title=element_blank(),legend.key = element_blank())
and following is the output from above code:
My requirement: Instead of having two lines denoted by 0 and 1 as legend elements, I only want legend texts "0" and "1" colored as "red" and "skyblue" (same as color of lines) and remove the lines. Is this possible?
Upvotes: 4
Views: 1790
Reputation: 23807
teunbrands custom guide is awesome. Just for completeness sake, here the fake legend option.
Technically, a legend is a very specific type of plot annotation. One of the most frequently asked questions is "how do I annotate outside the plot area" - which you can almost always answer with "create a second plot and stitch it to the main plot". Same here.
It requires a couple of tricks, in particular use of geom_blank
for scaling, and then correct use of theme elements, which you can either pass to each plot or globally (with &
)
The disadvantage of this approach is that you need to fiddle around with the coordinates to get the position right. but this should not be too much of an issue, except if you need to automate this.
P.S. the colors you are showing are neither "steelblue" nor "red" ... ;)
library(ggplot2)
library(patchwork)
p_main <-
ggplot(mtcars, aes(x = mpg, y = wt, color = factor(vs))) +
geom_line()
# semi - manual - chose x and y depending on the position relative to the other plot
# for labels: I used as.character just to be very explicit
df_legend <- data.frame(x = c(22,24), y = 3, labels = as.character(0:1))
p_legend <-
ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_blank() +
geom_text(data = df_legend, aes(x, y, label = labels, color = labels)) +
theme_void()
p_legend / p_main &
plot_layout(heights = c(1, 10)) &
theme(legend.position = "none")
Created on 2021-03-20 by the reprex package (v1.0.0)
Upvotes: 1
Reputation: 38063
I've written a string legend guide in the ggh4x package, that you might find useful.
Example:
library(ggplot2)
library(ggh4x)
ggplot(mtcars,aes(x=mpg,y=wt,color=factor(vs))) +
guides(colour = "stringlegend") +
geom_line() +
theme(legend.position="top",
legend.direction = "horizontal",
legend.title=element_blank(),
legend.key = element_blank())
Created on 2021-03-19 by the reprex package (v0.3.0)
Upvotes: 5
Reputation: 28441
You can use the ggtext
package which has element_markdown()
function that allows text formatting
The ggtext package provides simple Markdown and HTML rendering for ggplot2. Under the hood, the package uses the gridtext package for the actual rendering, and consequently it is limited to the feature set provided by gridtext.
Support is provided for Markdown both in theme elements (plot titles, subtitles, captions, axis labels, legends, etc.) and in geoms (similar to geom_text()). In both cases, there are two alternatives, one for creating simple text labels and one for creating text boxes with word wrapping.
library(RColorBrewer)
library(ggplot2)
library(ggtext)
my_col <- RColorBrewer::brewer.pal(unique(mtcars$vs), 'Dark2')
p <- ggplot(mtcars, aes(x = mpg, y = wt, color = factor(vs))) +
geom_line() +
theme_bw(base_size = 16) +
theme(
legend.position = "top", legend.direction = "horizontal",
legend.title = element_blank(), legend.key = element_blank()
)
p +
scale_color_manual(
labels = paste(
"<span style='color:",
my_col,
"'>",
unique(mtcars$vs),
"</span>"
),
values = my_col
) +
theme(legend.text = element_markdown(size = 16)) +
guides(color = guide_legend(override.aes = list(linetype = c(NA, NA))))
Created on 2021-03-18 by the reprex package (v1.0.0)
Upvotes: 3