[英]Add text to legend symbols in ggplot
Using geom_rect
I am separating my data into classes in a ggplot
.使用
geom_rect
我将我的数据分成ggplot
中的类。 These classes (v high, high, medium, low, v low) are denoted in the legend.这些类别(v 高、高、中、低、v 低)在图例中表示。 Is there a possibility to add the lower and upper limits of the classes from the
data.frame
to the legend symbols?是否有可能将
data.frame
中的类的下限和上限添加到图例符号?
Here is my set up:这是我的设置:
library(ggplot2)
library(viridis)
dat <- data.frame(x = seq(1,100,by=2), y = seq(1,300,by=3))
df <- data.frame(class = c("very high", "high", "medium", "low", "very low"),
lwr = c(90, 75, 35, 15, 0),
upr = c(100, 90, 75, 35, 15))
ggplot(dat) +
geom_rect(data = df, aes(xmin = lwr, xmax = upr, ymin = -Inf, ymax = Inf, fill = class), alpha = 0.4)+
geom_point(aes(x, y)) +
theme_bw()+
scale_fill_viridis("Classes", discrete = TRUE, option = "viridis",
limits = c("very high", "high", "medium", "low", "very low"))
I would like the legend to follow the following principle: Legend depicting the classes as well as their ranges.我希望图例遵循以下原则:描述类及其范围的图例。
So far, I have only found ways to change the label itself, but no indication on how to add information from the data frame to the legend or how to widen the symbols enough for text to fit in. Do you have any suggestions?到目前为止,我只找到了更改 label 本身的方法,但没有说明如何将数据框的信息添加到图例或如何加宽符号以适应文本。你有什么建议吗?
TBMK there is no easy way to achieve your desired result. TBMK 没有简单的方法可以达到您想要的结果。 One option would be to make use of a custom key glyph to add you labels to the legend keys.
一种选择是使用自定义键字形将标签添加到图例键。
To this end为此
Create a color palette as a named vector and a named vector of key labels.创建一个调色板作为命名向量和键标签的命名向量。 These vectors are used inside the custom key glyph to select the key label based on the value of the
fill
color via match
.这些向量在自定义键字形内使用到 select 键 label 基于
match
的fill
颜色值。
For the custom key glyph I adapted the code from ggplot2::draw_key_label
.对于自定义键字形,我改编了
ggplot2::draw_key_label
中的代码。 Basically I added the code to select the key label via match
and do some data manipulation on the data
object passed to draw_key_text
, ie I remove the fill
column, set the font colour to "black"
, set the alpha for the text to 1 and the font size to 11pt.基本上我通过
match
将代码添加到 select 键 label 并对传递给draw_key_text
的data
object 进行了一些数据操作,即我删除了fill
列,将字体颜色设置为"black"
,将文本的 alpha 设置为 1 和字体大小为 11pt。
Finally, I use strwidth
to compute the width of the legend keys which I set via guide_legend
.最后,我使用
strwidth
来计算我通过guide_legend
设置的图例键的宽度。
library(ggplot2)
library(dplyr)
# Color palette
pal <- scales::viridis_pal(option = "viridis")(5)
names(pal) <- c("very high", "high", "medium", "low", "very low")
# Legend key labels
key_label <- df |>
mutate(key_label = case_when(
upr == 100 ~ paste(">", lwr),
lwr == 0 ~ paste("<", upr),
TRUE ~ paste(lwr, upr, sep = "-")
)) |>
select(class, key_label) |>
tibble::deframe()
# Legend key width = maximum label * 1.10 to add some padding
width <- unit(max(sapply(key_label, strwidth, units = "inches")) * 1.10, "in")
# Custom key glyph
draw_key_cust <- function(data, params, size) {
data_text <- data
data_text$label <- key_label[names(pal)[match(data$fill, pal)]]
data_text[c("fill")] <- NULL
data_text$colour <- "black"
data_text$alpha <- 1
data_text$size <- 11 / .pt
grid::grobTree(
draw_key_rect(data, list()),
draw_key_text(data_text, list())
)
}
ggplot(dat) +
geom_rect(data = df, aes(xmin = lwr, xmax = upr, ymin = -Inf, ymax = Inf, fill = class), alpha = 0.4, key_glyph = "cust") +
geom_point(aes(x, y)) +
theme_bw() +
scale_fill_manual("Classes", values = pal, limits = c("very high", "high", "medium", "low", "very low")) +
guides(fill = guide_legend(keywidth = width))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.