您如何在以下代码中控制geom_line的配色方案?

问题描述

我有一个图表,该图表将条形图和连接的散点图按时间范围分组。我已经使用了scale_fill_manual函数来控制条形图的颜色。 R对条形图上方的温度线使用认颜色。我再次尝试使用scale_fill_manual,但是它不起作用,而且我似乎无法在case_when函数中分配颜色。有什么办法可以更改它们(我希望它们是黄色,橙色和红色以显示温度随时间升高)。谢谢!

am_graph <- ggplot((am),aes(x=factor(Month,levels=month.abb))) +
  geom_col(aes(y = Precipitation,group = Timeframe,fill = Timeframe),width = .7,position = "dodge") +
  geom_line(aes(y = Mean_Temperature*10,col = case_when(
    Timeframe == "1970-2000" ~ "1970-2000",Timeframe== 2018 ~ "2018",TRUE ~ "2041-2060"))) +
  geom_point(size = 0.25,aes(y = Mean_Temperature*10,group = Timeframe)) +
  scale_color_discrete(name = "Timeframe") +
  ggtitle("Alto Mayo Monthly climate Conditions (1970-2060)") +
  xlab("Month") +
  ylab("Precipitation (mm)") +
  scale_y_continuous(breaks = seq(0,220,by = 20),limit = c(0,220),expand = c(0,0),sec.axis = sec_axis(~./10,name = "Temperature (°C)")) +
  scale_fill_manual(values = c("lightblue2","steelblue2","royalblue1")) +
  theme_classic()

解决方法

让它适合任何想问的人!

am_graph <- ggplot((am),aes(x=factor(Month,levels=month.abb))) +
  geom_col(aes(y = Precipitation,group = Timeframe,fill = Timeframe),width = .7,position = "dodge") +
  geom_line(size = 0.5,aes(y = Mean_Temperature*10,col = case_when(
    Timeframe == "1970-2000" ~ "1970-2000",Timeframe== 2018 ~ "2018",TRUE ~ "2041-2060"))) +
  geom_point(size = 0.5,group = Timeframe)) +
  ggtitle("Alto Mayo Monthly Climate Conditions (1970-2060)") +
  xlab("Month") +
  ylab("Precipitation (mm)") +
  scale_y_continuous(breaks = seq(0,220,by = 20),limit = c(0,220),expand = c(0,0),sec.axis = sec_axis(~./10,name = "Temperature (°C)")) +
  scale_fill_manual(name = "Precipitation",values = c("lightblue2","steelblue2","royalblue1")) +
  scale_color_manual(values = c("lightgoldenrod2","darkorange2","tomato3")) +
  guides(col = guide_legend("Temperature")) +
  theme_classic()