要添加傳說,首先要做的是實際將某物映射到所需的美學。例如,現在您已將點形狀設置爲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
中設置值並在guide_legend
中使用override.aes
。
下面介紹如何創建一個新的圖例。注意我必須在geom_line
之前添加geom_hline
以使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
與linetype
沿組合和size
完成和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"))))
http://docs.ggplot2.org/current/guides.html – N8TRO 2014-10-19 03:37:25
@ N8TRO我從頁面明白,一個可以在主要添加傳說不同尺度'data.frame'在'ggplot使用() '。但是從鏈接的頁面中看不出來,例如'geom_hline()'。你能分享一下你的見解嗎? – HBat 2014-10-19 14:40:22