R语言 为什么我的ggplot显示为黑色和白色,即使我已经指定了线的颜色?

v09wglhw  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(297)

我试图绘制我的ggplot,但颜色不是按照它的编码。
这是我的数据框:
所有航班组合时间
| 年份|时间|延迟_计数|总计_计数|
| - ------|- ------|- ------|- ------|
| 二○ ○三年|午夜|小行星9934|小行星53321|
| 二○ ○三年|上午|小行星544278|小行星25|
| 二○ ○三年|下午|小行星819926|小行星2555560|
| 二○ ○三年|夜晚|小行星485066|小行星1359486|
| 二○ ○四年|午夜|小行星13458|小行星71|
| 二○ ○四年|上午|小行星687543|小行星278|
| 二○ ○四年|下午|小行星1056|小行星2758242|
| 二○ ○四年|夜晚|小行星655|小行星1516|
| 二○ ○五年|午夜|小行星14129|小行星76889|
| 二○ ○五年|上午|小行星717753|小行星2801079|
| 二○ ○五年|下午|小行星1108381|小行星273|
| 二○ ○五年|夜晚|小行星697598|小行星1523989|
这是我的代码:

# To plot the line graph
timeofday_plot <- ggplot() +
  geom_line(data= All_Flights_Combined_TimeOfDay, aes(x =TimeOfDay, y=Delay_Count, group=Year, color=Year)) + 
  geom_line(data=All_Flights_Combined_TimeOfDay, aes(x =TimeOfDay, y=Total_Count , group=Year, color=Year))+ 
  scale_x_discrete(limits = c("Midnight","Morning","Afternoon","Night"))+
  xlab("TimeOfDay")+
  ylab("Number of Flights")+
  ggtitle("Total number of Flights VS Flight Delays") +
  theme_classic() +
  scale_color_manual(values = c("2003 Delay_Count" = "red", 
                                "2004 Delay_Count" = "green", 
                                "2005 Delay_Count" = "blue", 
                                "2003 Total_Count" = "orange", 
                                "2004 Total_Count" = "yellow", 
                                "2005 Total_Count" = "purple"),
                     breaks = c("2003 Delay_Count",
                                "2004 Delay_Count",
                                "2005 Delay_Count",
                                "2003 Total_Count", 
                                "2004 Total_Count", 
                                "2005 Total_Count"))

timeofday_plot

This is the plot it is showing
我试过添加主题,但没有效果。

nfg76nw0

nfg76nw01#

要使您的代码和scale_color_manual工作,请将数据重新整形为long,然后使用unite(例如)将计数类型与年份组合,以便应用自定义调色板:

library(ggplot2)
library(tidyr)

All_Flights_Combined_TimeOfDay <- All_Flights_Combined_TimeOfDay %>% 
  pivot_longer(ends_with("Count"), names_to = "type", values_to = "Count") %>% 
  unite("type", Year, type, sep = " ", remove = FALSE)

ggplot() +
  geom_line(data = All_Flights_Combined_TimeOfDay, aes(x = TimeOfDay, y = Count, 
                                                       group = type, color = type)) +
  scale_x_discrete(limits = c("Midnight", "Morning", "Afternoon", "Night")) +
  xlab("TimeOfDay") +
  ylab("Number of Flights") +
  ggtitle("Total number of Flights VS Flight Delays") +
  theme_classic() +
  scale_color_manual(
    values = c(
      "2003 Delay_Count" = "red",
      "2004 Delay_Count" = "green",
      "2005 Delay_Count" = "blue",
      "2003 Total_Count" = "orange",
      "2004 Total_Count" = "yellow",
      "2005 Total_Count" = "purple"
    ),
    breaks = c(
      "2003 Delay_Count",
      "2004 Delay_Count",
      "2005 Delay_Count",
      "2003 Total_Count",
      "2004 Total_Count",
      "2005 Total_Count"
    )
  )

数据

All_Flights_Combined_TimeOfDay <- data.frame(
  Year = c(
    2003L, 2003L, 2003L, 2003L,
    2004L, 2004L, 2004L, 2004L, 2005L, 2005L, 2005L, 2005L
  ),
  TimeOfDay = c(
    "Midnight", "Morning",
    "Afternoon", "Night", "Midnight", "Morning", "Afternoon", "Night",
    "Midnight", "Morning", "Afternoon", "Night"
  ),
  Delay_Count = c(
    9934L, 544278L, 819926L,
    485066L, 13458L, 687543L, 1056605L, 655077L, 14129L, 717753L,
    1108381L, 697598L
  ),
  Total_Count = c(
    53321L, 2520172L, 2555560L,
    1359486L, 71595L, 2782548L, 2758242L, 1516885L, 76889L,
    2801079L, 2738639L, 1523989L
  )
)

相关问题