簡體   English   中英

ggplot2:如何為添加到散點圖中的線添加圖例?

[英]ggplot2: how to add the legend for a line added to a scatter plot?

我想在ggplot2散點圖上比較一些x和y數據。 我想添加一條統一線(y = x),兩倍(y = 2x),一半(y = x / 2)和更平滑的線以幫助理解數據,但是我找不到如何將這些線添加到情節的傳奇。 任何想法?

set.seed(123)
x <- runif(20, 1, 10)
y <- 0.8 * x + runif(20, -1, 1)
cat <- factor(c(rep("high", 10), rep("low", 10)))
d <- data.frame(x, y, cat)

ggplot(data=d) +
     geom_point(aes(x, y, colour=cat)) +
     geom_abline(aes(intercept=0, slope=1), col = "red") +
     geom_abline(aes(intercept=0, slope=0.5), col="blue", linetype="dotted") +
     geom_abline(aes(intercept=0, slope=2), col="blue", linetype="dashed")+
     geom_smooth(aes(x, y))

ggplot2中的y vs x散點圖

我希望標簽“統一線”,“雙重”,“一半”和“平滑”出現在圖例中的“高”和“低”標簽下方。

遵循User3640617的回答,我嘗試了以下代碼,但結果仍然不令人滿意,因為數據點現在具有圖例中鏈接到它們的線型和平滑線型。

ggplot(data=d) +
     geom_point(aes(x, y, colour=cat)) +
     geom_abline(aes(intercept=0, slope=1, colour="y = x")) +
     geom_abline(aes(intercept=0, slope=.5, colour="y = x/2")) +
     geom_abline(aes(intercept=0, slope=2, colour="y = 2x")) +
     geom_smooth(aes(x,y, colour="smooth")) +
     scale_color_manual(
        values=c("red", "darkgreen", "black", "blue", "red", "blue")) +
     scale_linetype_manual(
        values=c("blank", "blank", "solid", "dashed", "solid", "dotted")) +
     scale_shape_manual(values=c(1, 1, NA, NA, NA, NA))

另外,我似乎無法手動更改tinetype:

帶有散點圖和更平滑的散點圖

我知道,我可以簡單地選擇其他顏色,這樣可以減少混亂,但是應該可以有一個圖例,其中僅點和線表示點,而不是點線都表示點線,不是嗎?

ggplot2似乎被aes之后添加colorlinetype所困擾。 將行添加到圖例時,類別的順序似乎已更改。

您可以將字符值映射到未使用的外觀,以欺騙ggplot為您創建圖例:

ggplot(data=d) +
  geom_point(aes(x, y, colour=cat)) +
  geom_abline(aes(intercept=0, slope=1, lty='unity line'), col = "red")

在此處輸入圖片說明

使用+ scale_linetype(name = NULL)刪除linetype圖例標題。

我想沒有辦法再包含包含行的第二個數據集。

獲得線條的第二個圖例的一種方法是,通過線型映射線並對顏色進行硬編碼(隨后手動調整圖例中的顏色)。

可以這樣完成:

library(ggplot2)

# 1 recreate the data
set.seed(123)
x <- runif(20, 1, 10)
y <- 0.8 * x + runif(20, -1, 1)
cat <- factor(c(rep("high", 10), rep("low", 10)))
d <- data.frame(x, y, cat)

# create the lines-data
d_lines <- data.frame(int = rep(0, 3),
                      sl = c(1, 0.5, 2),
                      col = c("red", "blue", "blue"),
                      lty = c("solid", "dotted", "dashed"))

# extract the linetypes from the data.frame (with names to map the values correclty)
ltys <- as.character(d_lines$lty)
names(ltys) <- as.character(d_lines$lty)
# extract the colors in the order that ggplot will put the names of the lines (dashed, dotted, solid)
cols <- as.character(d_lines$col)
cols <- cols[order(as.character(d_lines$lty))]

# plot the data
ggplot() +
  geom_point(data = d, aes(x, y, colour=cat)) +
  geom_smooth(data = d, aes(x, y)) +
  # add the two different line-colors
  geom_abline(data = d_lines[d_lines$col == "red", ], 
              aes(intercept = int, slope = sl, lty = lty), color = "red") +
  geom_abline(data = d_lines[d_lines$col == "blue", ], 
              aes(intercept = int, slope = sl, lty = lty), color = "blue") +
  # change the color of the legend
  scale_linetype_manual(name = "Linetype", values = ltys,
                        guide = guide_legend(override.aes = list(colour = cols)))
#> `geom_smooth()` using method = 'loess'

也許這不是一個很整潔的解決方案,但是您可以使用統一行的信息創建一個附加數據框,如下所示:

dfabline <- data.frame(intercept=0, slope=1, group="unity line")

ggplot(data=d) +
  geom_point(aes(x, y, colour=cat)) +
  geom_abline(data=dfabline, aes(intercept=intercept, slope=slope,  colour=group))

您可以使用scale_colour_manual指定線條和點的scale_colour_manual

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM