ggplot:geom_vline 和 geom_hline 的单独图例
ggplot: Separate legend for both a geom_vline and geom_hline
我正在尝试在带有 facets 的 ggplot 中为 geom_vline
和 geom_hline
创建一个单独的图例。
这是我的代码:
set.seed(1)
vec1 = rnorm(20)
set.seed(2)
vec2 = rnorm(20)
set.seed(3)
vec3 = rnorm(20)
vec = c(vec1,vec2,vec3)
let = rep(sample(letters[1:3],20,replace=TRUE),3)
num = c(rep(1:20,3))
lab = c(rep("label 1",20),rep("label 2",20),rep("label 3",20))
# Vertical lines for geom_vline
line1 = seq(1,20,5)
df = data.frame(vector = vec, index = num, name = let, label = lab)
# Define facet order
df$label_f = factor(df$label, levels=c("label 1","label 2","label 3"))
# Dataframe for horizontal lines
hl = data.frame(label = c("label 1","label 2","label 3"), Z = c(0.5,1,-0.5))
# Factor to seperate horizontal lines for facets
hl$label_f = factor(hl$label, levels=c("label 1","label 2","label 3"))
ggplot(data=df,aes(index,vector)) +
geom_point(aes(alpha = name, color=name)) +
geom_vline(xintercept=line1, color="blue") +
geom_hline(data = hl, aes(yintercept=Z),color="red") +
scale_alpha_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c(1,0.5,0.5)) +
scale_color_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c("black","orange","darkorange4")) +
xlab("index") + ylab("rnorm") +
facet_wrap(~label_f,ncol=1,scale="free_y") +
theme(legend.position="bottom")
这是 ggplot:
我想要 geom_hline
和 geom_vline
的单独图例(“图例 2”),并带有适当的标签和颜色:
我希望图例中的 geom_hline
是水平的,geom_vline
也是垂直的!
它应该是这样的:
谢谢!
利用 ggnewscale
和自定义 draw_key
函数可以这样实现:
可以通过 ggnewscale::new_scale_color
实现单独的线条图例,这将添加第二个颜色图例。这样做需要为您的 hline
和 vline
添加一个映射。
棘手的部分是,默认情况下,您将获得图例键的垂直线和水平线。为了覆盖它,我添加了一个自定义键字形函数,该函数以行的 color
为条件。这意味着如果您更改颜色,您也必须调整条件。
library(ggplot2)
# Custom Key Glyph. Vertical Line if color = "blue". Horizontal Line otherwise
draw_key_cust <- function(data, params, size) {
if (data$colour == "blue") {
draw_key_vpath(data, params, size)
} else {
draw_key_path(data, params, size)
}
}
ggplot(data=df,aes(index,vector)) +
geom_point(aes(alpha = name, color=name)) +
scale_alpha_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c(1,0.5,0.5), guide = guide_legend(order = 1)) +
scale_color_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c("black","orange","darkorange4"), guide = guide_legend(order = 1)) +
ggnewscale::new_scale_color() +
geom_vline(data = data.frame(xintercept = line1), aes(xintercept = xintercept, color="line1"), key_glyph = "cust") +
geom_hline(data = hl, aes(yintercept = Z, color="line2"), key_glyph = "cust") +
scale_color_manual(name="Legend 2",
labels=c(line1 ="line 1", line2 = "line 2"),
values=c(line1 = "blue", line2 = "red"),
guide = guide_legend(order = 2)) +
xlab("index") + ylab("rnorm") +
facet_wrap(~label_f,ncol=1,scale="free_y") +
theme(legend.position="bottom")
一个选项是使用 fill=name
作为 geom_point() 的“颜色”,而不是 color=name
。
另外:
- 设置
pch=21
- 为 vline_data
创建一个小数据框
- 将颜色(例如,红色和蓝色)移动到 vline 和 hline
aes()
(我另外增加了 geom_points 的大小)
vline_data = data.frame(label_f = c(rep("label 1",length(line1)),rep("label 2",length(line1)),rep("label 3",length(line1))), Z = line1)
ggplot(data=df,aes(index,vector)) +
geom_point(aes(fill=name ), size=4,pch=21) +
geom_vline(data = vline_data, aes(xintercept=Z, color="blue")) +
geom_hline(data = hl, aes(yintercept=Z, color="red")) +
scale_fill_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c("black","orange","darkorange4")
) +
scale_alpha_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c(1,0.5,0.5)) +
scale_color_manual(name="Additional Lines",
labels=c("Vertical", "Horizontal"),
values=c("blue", "red")) +
xlab("index") + ylab("rnorm") +
facet_wrap(~label_f,ncol=1,scale="free_y") +
theme(legend.position="bottom") +
guides(alpha="none")
我正在尝试在带有 facets 的 ggplot 中为 geom_vline
和 geom_hline
创建一个单独的图例。
这是我的代码:
set.seed(1)
vec1 = rnorm(20)
set.seed(2)
vec2 = rnorm(20)
set.seed(3)
vec3 = rnorm(20)
vec = c(vec1,vec2,vec3)
let = rep(sample(letters[1:3],20,replace=TRUE),3)
num = c(rep(1:20,3))
lab = c(rep("label 1",20),rep("label 2",20),rep("label 3",20))
# Vertical lines for geom_vline
line1 = seq(1,20,5)
df = data.frame(vector = vec, index = num, name = let, label = lab)
# Define facet order
df$label_f = factor(df$label, levels=c("label 1","label 2","label 3"))
# Dataframe for horizontal lines
hl = data.frame(label = c("label 1","label 2","label 3"), Z = c(0.5,1,-0.5))
# Factor to seperate horizontal lines for facets
hl$label_f = factor(hl$label, levels=c("label 1","label 2","label 3"))
ggplot(data=df,aes(index,vector)) +
geom_point(aes(alpha = name, color=name)) +
geom_vline(xintercept=line1, color="blue") +
geom_hline(data = hl, aes(yintercept=Z),color="red") +
scale_alpha_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c(1,0.5,0.5)) +
scale_color_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c("black","orange","darkorange4")) +
xlab("index") + ylab("rnorm") +
facet_wrap(~label_f,ncol=1,scale="free_y") +
theme(legend.position="bottom")
这是 ggplot:
我想要 geom_hline
和 geom_vline
的单独图例(“图例 2”),并带有适当的标签和颜色:
我希望图例中的 geom_hline
是水平的,geom_vline
也是垂直的!
它应该是这样的:
谢谢!
利用 ggnewscale
和自定义 draw_key
函数可以这样实现:
可以通过
ggnewscale::new_scale_color
实现单独的线条图例,这将添加第二个颜色图例。这样做需要为您的hline
和vline
添加一个映射。棘手的部分是,默认情况下,您将获得图例键的垂直线和水平线。为了覆盖它,我添加了一个自定义键字形函数,该函数以行的
color
为条件。这意味着如果您更改颜色,您也必须调整条件。
library(ggplot2)
# Custom Key Glyph. Vertical Line if color = "blue". Horizontal Line otherwise
draw_key_cust <- function(data, params, size) {
if (data$colour == "blue") {
draw_key_vpath(data, params, size)
} else {
draw_key_path(data, params, size)
}
}
ggplot(data=df,aes(index,vector)) +
geom_point(aes(alpha = name, color=name)) +
scale_alpha_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c(1,0.5,0.5), guide = guide_legend(order = 1)) +
scale_color_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c("black","orange","darkorange4"), guide = guide_legend(order = 1)) +
ggnewscale::new_scale_color() +
geom_vline(data = data.frame(xintercept = line1), aes(xintercept = xintercept, color="line1"), key_glyph = "cust") +
geom_hline(data = hl, aes(yintercept = Z, color="line2"), key_glyph = "cust") +
scale_color_manual(name="Legend 2",
labels=c(line1 ="line 1", line2 = "line 2"),
values=c(line1 = "blue", line2 = "red"),
guide = guide_legend(order = 2)) +
xlab("index") + ylab("rnorm") +
facet_wrap(~label_f,ncol=1,scale="free_y") +
theme(legend.position="bottom")
一个选项是使用 fill=name
作为 geom_point() 的“颜色”,而不是 color=name
。
另外:
- 设置
pch=21
- 为 vline_data 创建一个小数据框
- 将颜色(例如,红色和蓝色)移动到 vline 和 hline
aes()
(我另外增加了 geom_points 的大小)
vline_data = data.frame(label_f = c(rep("label 1",length(line1)),rep("label 2",length(line1)),rep("label 3",length(line1))), Z = line1)
ggplot(data=df,aes(index,vector)) +
geom_point(aes(fill=name ), size=4,pch=21) +
geom_vline(data = vline_data, aes(xintercept=Z, color="blue")) +
geom_hline(data = hl, aes(yintercept=Z, color="red")) +
scale_fill_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c("black","orange","darkorange4")
) +
scale_alpha_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c(1,0.5,0.5)) +
scale_color_manual(name="Additional Lines",
labels=c("Vertical", "Horizontal"),
values=c("blue", "red")) +
xlab("index") + ylab("rnorm") +
facet_wrap(~label_f,ncol=1,scale="free_y") +
theme(legend.position="bottom") +
guides(alpha="none")