【问题标题】:Change color of geom_line in faceted plot在多面图中更改 geom_line 的颜色
【发布时间】:2019-10-30 02:46:30
【问题描述】:

我希望每个国家/地区的线条与我手动设置的颜色相同。 IE。瑞典 = 红色,美国 = 蓝色,加拿大 = 绿色。

如何最好地做到这一点?

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)

【问题讨论】:

    标签: r ggplot2 facet-grid


    【解决方案1】:

    编辑:删除了区分十分位数的映射。

    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
    

    【讨论】:

    • 但我根本不想改变线型。
    • 如果您希望十分位数以相同的外观单独绘制,请使用 group = decile 而不是 lty = decile。我使用线型来区分它们,但您可以交替使用 alpha、点形状、大小等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 2015-11-04
    相关资源
    最近更新 更多