簡體   English   中英

在多面圖中更改geom_line的顏色

[英]Change color of geom_line in faceted plot

我希望每個國家/地區的線條與我手動設置的顏色相同。 即。 瑞典=紅色,美國=藍色,加拿大=綠色。

如何做到最好?

library(tidyverse)

df <- tibble(
  date = rep(c(as.Date("1995-01-01"), as.Date("1995-01-01"), as.Date("1996-01-01"), as.Date("1996-01-01")), 3),
  decile = rep(c("d1", "d2"), 6),
  income = c(10, 30, 15, 35, 50, 60, 70, 80, 90, 100, 110, 120),
  country = c(rep("Sweden", 4), rep("Canada", 4), rep("USA", 4))
)

g <- ggplot(df, aes(x = date, y = income)) +
  geom_line(aes(colour = decile), size = 2) +
  scale_color_manual(values = c("red", "green", "blue", "black")) 

g + facet_grid(~ country)

編輯:刪除了映射區分十分之分。

g <- ggplot(df, aes(x = date, y = income, group = decile, color = country)) +
  geom_line(size = 2) +
  scale_color_manual(values = c("Sweden" = "red", "Canada" = "green", 
                                "USA" = "blue")) + 
  facet_grid(~ country)
g

在此處輸入圖片說明

暫無
暫無

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

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