使用 "empty" 插槽格式化行中的 ggplot2 图例文本
Formatting ggplot2 legend text in rows with "empty" slots
我正在尝试在下面的图中格式化图例。这是一些虚拟数据和我的代码:
df <- data.frame(supplement = c("Control",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI"),
axis1 = c(1, 2, 3, 4, 5, 6, 7),
axis2 = c(7, 6, 5, 4, 3, 2, 1))
df %>%
mutate(supplement = factor(supplement,
levels = c("Control",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI"))) %>%
ggplot(aes(x = axis1, y = axis2)) +
geom_point(aes(color = supplement), size = 4) +
labs(y = "Axis 2",
x = "Axis 1") +
scale_colour_manual(name = "",
breaks = c("Control",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI"),
labels = c("No Treatment",
"0.5 mM Nitrate", "5 mM Nitrate", "10 mM Nitrate",
"0.5 mM Nitrite", "5 mM Nitrite", "10 mM Nitrite"),
values = c("#003F5C",
"#824ED4", "#31B3F7", "#FFA600",
"#ABF13D", "#DC267F", "#16F5E3"))+
guides(color = guide_legend(order = 1, nrow = 2, byrow = TRUE)) +
theme(legend.title = element_blank(),
legend.text = element_text(size = unit(14, "pt")),
legend.position = "bottom",
legend.box = "vertical",
axis.title = element_text(size = unit(16, "pt")),
axis.text = element_text(size = unit(14, "pt")),
legend.key.size = unit(25, "pt"),
legend.key.width = unit(0, 'mm'))
我得到的是这个
但我真的很希望他们是这样的。但是我不知道这在 ggplot 中是否可行,因为我还没有找到任何可以做到这一点的东西。
我不知道有任何 (non-complicated) 方法可以将一个特定的图例键与另一行进行比较,但在这种情况下,由于图例中有 7 个项目,您可以通过将指南限制为 3 列并在 scale_color_manual()
中重新排序以将控件放在最后来分离出“No Treatment”键。
在这种情况下,您的情节代码部分将更改为:
# ... original plot code...
scale_colour_manual(name = "",
breaks = c(
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI", "Control"),
labels = c(
"0.5 mM Nitrate", "5 mM Nitrate", "10 mM Nitrate",
"0.5 mM Nitrite", "5 mM Nitrite", "10 mM Nitrite", "No Treatment"),
values = c(
"#824ED4", "#31B3F7", "#FFA600",
"#ABF13D", "#DC267F", "#16F5E3", "#003F5C"))+
guides(color = guide_legend(order = 1, ncol = 3, byrow = TRUE)) +
# ... remaining plot code
另一种方法是使用您的原始代码,只将图例放在绘图的右侧或左侧。为此,删除 guides(...)
行和 legend.position=
主题元素,因为默认设置是放在右边。
如果您可以摆脱图例键背景,那么一种选择是
- 在
guide_legend
中设置ncols=3
- 向您的因素添加两个额外的类别以向图例添加两个额外的槽或键
- 将这些类别的标签设置为空字符串,颜色等于绘图背景颜色
- 此外,我们必须将
drop=FALSE
设置为 scale_color_manual
,这样这些未使用的类别就不会被丢弃
library(ggplot2)
library(dplyr)
df <- df %>%
mutate(supplement = factor(supplement,
levels = c("Control", "foo", "bar",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI")))
ggplot(df, aes(x = axis1, y = axis2)) +
geom_point(aes(color = supplement), size = 4) +
labs(y = "Axis 2", x = "Axis 1") +
scale_colour_manual(name = "",
breaks = c("Control", "foo", "bar",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI"),
labels = c("No Treatment", "", "",
"0.5 mM Nitrate", "5 mM Nitrate", "10 mM Nitrate",
"0.5 mM Nitrite", "5 mM Nitrite", "10 mM Nitrite"),
values = c("#003F5C", "white", "white",
"#824ED4", "#31B3F7", "#FFA600",
"#ABF13D", "#DC267F", "#16F5E3"),
drop = FALSE) +
guides(color = guide_legend(order = 1, ncol = 3, byrow = TRUE)) +
theme(legend.title = element_blank(),
legend.text = element_text(size = unit(14, "pt")),
legend.position = "bottom",
legend.box = "vertical",
legend.key = element_rect(fill = NA),
axis.title = element_text(size = unit(16, "pt")),
axis.text = element_text(size = unit(14, "pt")),
legend.key.size = unit(25, "pt"),
legend.key.width = unit(0, 'mm'))
我正在尝试在下面的图中格式化图例。这是一些虚拟数据和我的代码:
df <- data.frame(supplement = c("Control",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI"),
axis1 = c(1, 2, 3, 4, 5, 6, 7),
axis2 = c(7, 6, 5, 4, 3, 2, 1))
df %>%
mutate(supplement = factor(supplement,
levels = c("Control",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI"))) %>%
ggplot(aes(x = axis1, y = axis2)) +
geom_point(aes(color = supplement), size = 4) +
labs(y = "Axis 2",
x = "Axis 1") +
scale_colour_manual(name = "",
breaks = c("Control",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI"),
labels = c("No Treatment",
"0.5 mM Nitrate", "5 mM Nitrate", "10 mM Nitrate",
"0.5 mM Nitrite", "5 mM Nitrite", "10 mM Nitrite"),
values = c("#003F5C",
"#824ED4", "#31B3F7", "#FFA600",
"#ABF13D", "#DC267F", "#16F5E3"))+
guides(color = guide_legend(order = 1, nrow = 2, byrow = TRUE)) +
theme(legend.title = element_blank(),
legend.text = element_text(size = unit(14, "pt")),
legend.position = "bottom",
legend.box = "vertical",
axis.title = element_text(size = unit(16, "pt")),
axis.text = element_text(size = unit(14, "pt")),
legend.key.size = unit(25, "pt"),
legend.key.width = unit(0, 'mm'))
我得到的是这个
但我真的很希望他们是这样的。但是我不知道这在 ggplot 中是否可行,因为我还没有找到任何可以做到这一点的东西。
我不知道有任何 (non-complicated) 方法可以将一个特定的图例键与另一行进行比较,但在这种情况下,由于图例中有 7 个项目,您可以通过将指南限制为 3 列并在 scale_color_manual()
中重新排序以将控件放在最后来分离出“No Treatment”键。
在这种情况下,您的情节代码部分将更改为:
# ... original plot code...
scale_colour_manual(name = "",
breaks = c(
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI", "Control"),
labels = c(
"0.5 mM Nitrate", "5 mM Nitrate", "10 mM Nitrate",
"0.5 mM Nitrite", "5 mM Nitrite", "10 mM Nitrite", "No Treatment"),
values = c(
"#824ED4", "#31B3F7", "#FFA600",
"#ABF13D", "#DC267F", "#16F5E3", "#003F5C"))+
guides(color = guide_legend(order = 1, ncol = 3, byrow = TRUE)) +
# ... remaining plot code
另一种方法是使用您的原始代码,只将图例放在绘图的右侧或左侧。为此,删除 guides(...)
行和 legend.position=
主题元素,因为默认设置是放在右边。
如果您可以摆脱图例键背景,那么一种选择是
- 在
guide_legend
中设置 - 向您的因素添加两个额外的类别以向图例添加两个额外的槽或键
- 将这些类别的标签设置为空字符串,颜色等于绘图背景颜色
- 此外,我们必须将
drop=FALSE
设置为scale_color_manual
,这样这些未使用的类别就不会被丢弃
ncols=3
library(ggplot2)
library(dplyr)
df <- df %>%
mutate(supplement = factor(supplement,
levels = c("Control", "foo", "bar",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI")))
ggplot(df, aes(x = axis1, y = axis2)) +
geom_point(aes(color = supplement), size = 4) +
labs(y = "Axis 2", x = "Axis 1") +
scale_colour_manual(name = "",
breaks = c("Control", "foo", "bar",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI"),
labels = c("No Treatment", "", "",
"0.5 mM Nitrate", "5 mM Nitrate", "10 mM Nitrate",
"0.5 mM Nitrite", "5 mM Nitrite", "10 mM Nitrite"),
values = c("#003F5C", "white", "white",
"#824ED4", "#31B3F7", "#FFA600",
"#ABF13D", "#DC267F", "#16F5E3"),
drop = FALSE) +
guides(color = guide_legend(order = 1, ncol = 3, byrow = TRUE)) +
theme(legend.title = element_blank(),
legend.text = element_text(size = unit(14, "pt")),
legend.position = "bottom",
legend.box = "vertical",
legend.key = element_rect(fill = NA),
axis.title = element_text(size = unit(16, "pt")),
axis.text = element_text(size = unit(14, "pt")),
legend.key.size = unit(25, "pt"),
legend.key.width = unit(0, 'mm'))