我有以下多层情节:
df <- data.frame(number = 1:10,
values = rnorm(10),
decision = factor(rbinom(10,1,.5), levels = c(0,1),
labels=c("No","Yes")),
expValues = rnorm(10),
bandwidth = runif(10, 0,1))
ggplot(df,aes(x = number, y = values, color=decision)) + aes(group=NA) +
geom_line(size = 1) +
geom_point(size = 3,shape = 16) +
geom_smooth(data = df, aes(ymin = values-bandwidth , ymax = values+bandwidth),
stat = "identity") +
geom_point(data=df,
aes(x=number,y=expValues),shape = "x", size = 5, color = "blue") +
geom_text(data = data.frame(x = Inf, y = max(df$values), label = "Mean = 12"),
aes(label=label, x = x, y = y) ,
hjust = 1, vjust = -0.1, color = "brown", size = 10) +
geom_hline(yintercept=mean(df$values) ,color="blue", linetype = "dashed") +
theme(text=element_text(size=20))
我想为geom_hline
和geom_point
添加形状&#34; x&#34;的图例,表示第一个是&#34; Cut Value&#34;,for the第二个&#34;预期价值&#34;。
我该怎么做?
答案 0 :(得分:5)
要添加图例,首先要做的是将某些内容映射到所需的美学。例如,现在您已将点形状设置为x
,但您没有在aes
内映射它,因此您无法获得图例。您可以映射到常量和变量以强制传奇。
对于geom_point
图层,您可以将shape
移到aes
内,映射到您喜欢的任何常量。使用一些表明它是什么的值将使图例编辑更容易。
geom_point(data=df, aes(x=number, y=expValues, shape = "Expected value"), size = 5, color = "blue")
对于geom_hline
,您需要一个特定于图层的数据集以进行映射。根据您的路线,您可能还需要在此图层中将show_guide
设置为TRUE
。
geom_hline(data = data.frame(yint = mean(df$values)), aes(...), show_guide = TRUE)
你可以制作两个单独的传说。您还可以将线条和形状组合到单个图例中,如此答案here中所示。这两个选项都涉及在适当的scale_xxx_manual
中设置值,并在override.aes
中使用guide_legend
。
以下是制作单个新传奇的方法。请注意,我必须在geom_hline
之前添加geom_line
,以使decision
图例看起来正确。
ggplot(df, aes(x = number, y = values, color=decision, group = NA)) +
geom_hline(data = data.frame(yint = mean(df$values)),
aes(yintercept = yint, shape = "Cut value"),
color="blue", linetype = 2, show_guide = TRUE) +
geom_line(size = 1) +
geom_point(size = 3,shape = 16) +
geom_smooth(data = df, aes(ymin = values-bandwidth , ymax = values+bandwidth),
stat = "identity") +
geom_point(data=df, aes(x=number, y=expValues, shape = "Expected value"),
size = 5, color = "blue") +
geom_text(data = data.frame(x = Inf, y = max(df$values), label = "Mean = 12"),
aes(label=label, x = x, y = y),
hjust = 1, vjust = -0.1, color = "brown", size = 10) +
theme(text=element_text(size=20)) +
scale_shape_manual(name = "", values = c("x", "x")) +
guides(shape = guide_legend(override.aes = list(linetype = c(2, 0),
shape = c(NA, "x"))))
修改添加错误带功能区的图例
我无法使用fill
根据错误带功能添加第三个图例。你可以做三个单独的传说,虽然我认为间距不是很好:
ggplot(df, aes(x = number, y = values, color=decision, group = NA)) +
geom_hline(data = data.frame(yint = mean(df$values)),
aes(yintercept = yint, linetype = "Cut value"),
color="blue", show_guide = TRUE) +
geom_line(size = 1) +
geom_point(size = 3,shape = 16) +
geom_smooth(data = df, aes(ymin = values-bandwidth , ymax = values+bandwidth, fill = "Error band"),
stat = "identity") +
geom_point(data=df, aes(x=number, y=expValues, shape = "Expected value"),
size = 5, color = "blue") +
geom_text(data = data.frame(x = Inf, y = max(df$values), label = "Mean = 12"),
aes(label=label, x = x, y = y),
hjust = 1, vjust = -0.1, color = "brown", size = 10) +
theme(text=element_text(size=20)) +
scale_shape_manual(name = "", values = "x") +
scale_linetype_manual(name = "", values = 2) +
scale_fill_manual(name = "", values = "grey") +
guides(shape = guide_legend(override.aes = list(linetype = 0)),
fill = guide_legend(override.aes = list(linetype = 0)),
color = guide_legend(override.aes = list(fill = NA)))
或者,如果override.aes
内部有一些工作,可以将colour
和size
以及linetype
和shape
结合使用。
ggplot(df, aes(x = number, y = values, color=decision, group = NA)) +
geom_hline(data = data.frame(yint = mean(df$values)),
aes(yintercept = yint, shape = "Cut value"),
color="blue", linetype = 2, show_guide = TRUE) +
geom_line(size = 1) +
geom_point(size = 3,shape = 16) +
geom_smooth(data = df, aes(ymin = values-bandwidth , ymax = values+bandwidth, shape = "Error band"),
stat = "identity", show_guide = FALSE) +
geom_point(data=df, aes(x=number, y=expValues, shape = "Expected value"),
size = 5, color = "blue") +
geom_text(data = data.frame(x = Inf, y = max(df$values), label = "Mean = 12"),
aes(label=label, x = x, y = y),
hjust = 1, vjust = -0.1, color = "brown", size = 10) +
theme(text=element_text(size=20)) +
scale_shape_manual(name = "", values = rep("x", 3)) +
guides(shape = guide_legend(override.aes = list(linetype = c(2, 1, 0),
size = c(.5, 10, 5),
shape = c(NA, NA, "x"),
colour = c("blue", "grey75", "blue"))))