【问题标题】:Why are the colors in ggplot (geom_line) different from explicitly assigned colours为什么 ggplot (geom_line) 中的颜色与显式分配的颜色不同
【发布时间】:2019-04-22 16:29:59
【问题描述】:

颜色没有如图所示绘制,我认为这是一个错误

library(ggplot2)

x <- seq(1,20,1)
market_yield1 <- c(1.00,1.20,1.35,1.50,1.60,1.70,1.75,1.85,1.90,1.95,
                   2.00,2.05,2.08,2.11,2.14,2.17,2.19,2.21,2.23,2.25)
market_yield2 <- c(1.00,1.20,1.35,1.50,1.60,1.70,1.75,1.85,1.85,1.80,
                   1.75,1.70,1.67,1.64,1.60,1.58,1.55,1.52,1.50,1.48)
market_yield3 <- c(1.00,0.98,0.95,0.93,0.91,0.89,0.87,0.84,0.81,0.78,
                   0.76,0.73,0.70,0.68,0.66,0.64,0.63,0.61,0.60,0.58)
x <- seq(1,20,1)
total <- data.frame(x, market_yield1, market_yield2, market_yield3)
names(total)[2:4] <- c("Szenario_1","Szenario_2","Szenario_3")


f <- ggplot(data = total) +
  geom_line(aes(x, Szenario_1, color = "red"), size = 1)+
  geom_line(aes(x, Szenario_2, color = "green"), size = 1)+
  geom_line(aes(x, Szenario_3, color = "blue"), size = 1)+
  labs(x = "Anzahl Jahre n",  y = "Zinsen in %", 
       title ="Die erwarteten Zinskurven",subtitle = "für die 3 Szenarien")+
  scale_color_discrete(name = "Verschiede Szenarien",
                       breaks = c("red","green","blue"),
                       labels = c("Szenario 1","Szenario 2","Szenario 3"))
plot(f)

我希望 Szenario_1 应该是红色的,Szenario_3 应该是蓝色的

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这不是错误。您的数据是宽格式的,您正在分配颜色值在 aes()。如果您想保持这种方法,请尝试以下方法:

    ggplot(data = total) +
      geom_line(aes(x, Szenario_1, color = "red"), size = 1)+
      geom_line(aes(x, Szenario_2, color = "green"), size = 1)+
      geom_line(aes(x, Szenario_3, color = "blue"), size = 1)+
      labs(x = "Anzahl Jahre n",  y = "Zinsen in %", 
           title ="Die erwarteten Zinskurven",subtitle = "für die 3 Szenarien")+
    
      # use scale_color_manual to specify the actual colours mapping to the values specified
      # within each aes() mapping
      scale_color_manual(name = "Verschiede Szenarien",
                         limits = c("red", "green", "blue"),
                         values = c("red", "green", "blue"),
                         labels = c("Szenario 1", "Szenario 2", "Szenario 3"))
    

    但更简洁的方法是将数据转换为长格式,并将每个场景都放在同一个geom_line 层中:

    library(dplyr)
    library(tidyr)
    
    total %>%
      gather(scenario, value, -x) %>%
      ggplot(aes(x, value, color = scenario)) +
      geom_line(size = 1) +
      labs(x = "Anzahl Jahre n",  y = "Zinsen in %", 
           title ="Die erwarteten Zinskurven",subtitle = "für die 3 Szenarien") +
      scale_color_manual(name = "Verschiede Szenarien",
                         values = c("red", "green", "blue"),
                         labels = c("Szenario 1", "Szenario 2", "Szenario 3"))
    

    (两段代码返回相同的结果。)

    【讨论】:

    • 非常感谢...我只是不明白它背后的逻辑。我对其进行了实验并删除了limits = c(...并更改了aes()中的颜色顺序并且会遇到同样的问题...无论如何非常感谢您:D tidyr的解决方案是有意义的
    【解决方案2】:

    快速而肮脏的解决方案是从 aes 中删除颜色分配

    geom_line(aes(x, Szenario_1), color = "red", size = 1)+
    geom_line(aes(x, Szenario_2), color = "green", size = 1)+
    geom_line(aes(x, Szenario_3), color = "blue", size = 1)+
    

    【讨论】:

    • 这也是我的方法,但遗憾的是图例不可见。
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多