将因子级别设置为在 ggplot2 中不可见
Setting factor level to be invisible in ggplot2
我认为我错过了对 ggplot2 的更新,并且某些内容显然已经更改。
对于要省略或“不可见”的变量的因子级别,您过去可以在 scale_color_manual 中设置一个命名向量,并将级别设置为 NA,如下例所示:
df_test <- tibble(y = c(100, -50, 20),
group = c("A", "B", "C"),
Marker = c("above average", " ", "below average"))
ggplot(data = df_test, mapping = aes(x = group, y = y, color = Marker)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9), size = 1.5, fill = "yellow") +
scale_color_manual(values = c("above average" = "green3",
"below average" = "red",
" " = NA))
不幸的是,这不再有效,而是将因子级别设置为灰色。
我想要的输出是“B”组都没有轮廓,并且图例中没有显示因子水平。
使用“alpha”美学对我不起作用,因为在我的实际代码中,我已经准备好将 alpha 用于其他用途。
提前致谢!
一个选项是将值设置为 transparent
而不是 NA
并设置中断以忽略不需要的因子水平,如下所示:
library(ggplot2)
library(tibble)
df_test <- tibble(y = c(100, -50, 20),
group = c("A", "B", "C"),
Marker = c("above average", " ", "below average"))
ggplot(data = df_test, mapping = aes(x = group, y = y, color = Marker)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9), size = 1.5, fill = "yellow") +
scale_color_manual(breaks = c("above average", "below average"),
values = c("above average" = "green3",
"below average" = "red",
" " = "transparent"))
另一种选择是对一个数据集进行子集化并将其置于现有图之上,也许吧?
这允许您仅将美学分配给组的一个子集,因为条形图实际上是两个不同的层。
library(ggplot)
df_test <- tibble(y = c(100, -50, 20),
group = c("A", "B", "C"),
Marker = c("above average", " ", "below average"))
ggplot() +
geom_bar(data = df_test, aes(x = group, y = y),
stat = "identity",
position = position_dodge(width = 0.9),
size = 1.5,
fill = "yellow") +
geom_bar(data = (df_test %>% filter(group != "B")),
aes(x = group, y = y,color = Marker),
stat = "identity",
position = position_dodge(width = 0.9), size = 1.5, fill = "yellow") +
scale_color_manual(values = c("above average" = "green3",
"below average" = "red"))
您注意到最近在手动秤如何处理其值以及缺失值或剩余值会发生什么变化方面发生了一些变化,这是对的。现在似乎更容易使用命名向量来设置比例限制。 (见问题4511, 4534, 4569)
还有两个选项:
使用这些比例变化,将比例值作为命名向量提供,缺少您不想包含的级别。这会给它一个 NA
值(默认为灰色),然后你可以用 na.value = NA
.
删除它
ggplot(df_test, aes(x = group, y = y, color = Marker)) +
geom_col(fill = "yellow", size = 1.5) +
scale_color_manual(values = c("above average" = "green3", "below average" = "red"),
na.value = NA)
第二个选项有点傻,但有时很有用(我用它来制作地图,其中不同的地理级别需要根据它们在层次结构中的位置绘制边界)。不是所有具有相同大小轮廓的条,而是手动设置大小比例,空白级别的大小为 0。同样,由于颜色向量中缺少 " "
值,因此它不会包含在该值中传奇;然后放下尺寸图例。如果您需要图例具有粗边框,请使用其比例的 override.aes
参数。
library(ggplot2)
df_test <- tibble::tibble(y = c(100, -50, 20),
group = c("A", "B", "C"),
Marker = c("above average", " ", "below average"))
ggplot(df_test, aes(x = group, y = y, color = Marker)) +
geom_col(aes(size = Marker), fill = "yellow") +
scale_color_manual(values = c("above average" = "green3", "below average" = "red")) +
scale_size_manual(values = c("above average" = 1.5, "below average" = 1.5, " " = 0),
guide = guide_none())
我认为我错过了对 ggplot2 的更新,并且某些内容显然已经更改。 对于要省略或“不可见”的变量的因子级别,您过去可以在 scale_color_manual 中设置一个命名向量,并将级别设置为 NA,如下例所示:
df_test <- tibble(y = c(100, -50, 20),
group = c("A", "B", "C"),
Marker = c("above average", " ", "below average"))
ggplot(data = df_test, mapping = aes(x = group, y = y, color = Marker)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9), size = 1.5, fill = "yellow") +
scale_color_manual(values = c("above average" = "green3",
"below average" = "red",
" " = NA))
不幸的是,这不再有效,而是将因子级别设置为灰色。 我想要的输出是“B”组都没有轮廓,并且图例中没有显示因子水平。 使用“alpha”美学对我不起作用,因为在我的实际代码中,我已经准备好将 alpha 用于其他用途。
提前致谢!
一个选项是将值设置为 transparent
而不是 NA
并设置中断以忽略不需要的因子水平,如下所示:
library(ggplot2)
library(tibble)
df_test <- tibble(y = c(100, -50, 20),
group = c("A", "B", "C"),
Marker = c("above average", " ", "below average"))
ggplot(data = df_test, mapping = aes(x = group, y = y, color = Marker)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9), size = 1.5, fill = "yellow") +
scale_color_manual(breaks = c("above average", "below average"),
values = c("above average" = "green3",
"below average" = "red",
" " = "transparent"))
另一种选择是对一个数据集进行子集化并将其置于现有图之上,也许吧?
这允许您仅将美学分配给组的一个子集,因为条形图实际上是两个不同的层。
library(ggplot)
df_test <- tibble(y = c(100, -50, 20),
group = c("A", "B", "C"),
Marker = c("above average", " ", "below average"))
ggplot() +
geom_bar(data = df_test, aes(x = group, y = y),
stat = "identity",
position = position_dodge(width = 0.9),
size = 1.5,
fill = "yellow") +
geom_bar(data = (df_test %>% filter(group != "B")),
aes(x = group, y = y,color = Marker),
stat = "identity",
position = position_dodge(width = 0.9), size = 1.5, fill = "yellow") +
scale_color_manual(values = c("above average" = "green3",
"below average" = "red"))
您注意到最近在手动秤如何处理其值以及缺失值或剩余值会发生什么变化方面发生了一些变化,这是对的。现在似乎更容易使用命名向量来设置比例限制。 (见问题4511, 4534, 4569)
还有两个选项:
使用这些比例变化,将比例值作为命名向量提供,缺少您不想包含的级别。这会给它一个 NA
值(默认为灰色),然后你可以用 na.value = NA
.
ggplot(df_test, aes(x = group, y = y, color = Marker)) +
geom_col(fill = "yellow", size = 1.5) +
scale_color_manual(values = c("above average" = "green3", "below average" = "red"),
na.value = NA)
第二个选项有点傻,但有时很有用(我用它来制作地图,其中不同的地理级别需要根据它们在层次结构中的位置绘制边界)。不是所有具有相同大小轮廓的条,而是手动设置大小比例,空白级别的大小为 0。同样,由于颜色向量中缺少 " "
值,因此它不会包含在该值中传奇;然后放下尺寸图例。如果您需要图例具有粗边框,请使用其比例的 override.aes
参数。
library(ggplot2)
df_test <- tibble::tibble(y = c(100, -50, 20),
group = c("A", "B", "C"),
Marker = c("above average", " ", "below average"))
ggplot(df_test, aes(x = group, y = y, color = Marker)) +
geom_col(aes(size = Marker), fill = "yellow") +
scale_color_manual(values = c("above average" = "green3", "below average" = "red")) +
scale_size_manual(values = c("above average" = 1.5, "below average" = 1.5, " " = 0),
guide = guide_none())