【问题标题】:Create a legend, change Y-axis for a ggplot2 plot that has a geom_col and 4 geom_line plots创建一个图例,更改具有 geom_col 和 4 个 geom_line 图的 ggplot2 图的 Y 轴
【发布时间】:2021-08-10 00:55:06
【问题描述】:

我看过这篇文章:Legend formatting for ggplot with geom_col and geom_line 和:Controlling legend appearance in ggplot2 with override.aes

我有一个带有 geom_col 层和 4 个 geom_line 层的图。我希望能够使用我在 Plot 1 中生成每个图层时选择的颜色来显示没有标题的图例。或者,如果我可以更改 Plot 2 中可能有效的颜色和图例标题。我还想更改 Y 轴比例,因为生产数据永远不会低于 20 英亩英尺,但是当我使用 scale_y_continuous 和 geom_col 时,条形不会出现。

数据

structure(list(Month_Day = structure(c(18628, 18629, 18630, 18631, 
18632, 18633, 18634, 18635, 18636, 18637), class = "Date"), `2021` = c(37.57464179, 
35.95859072, 39.14746726, 37.9630674, 40.55096688, 42.53487456, 
41.66243889, 40.18150773, 37.90217088, 38.30979013), Mean = c(36.9619627272733, 
36.2260000000002, 36.4146654545455, 36.3741972727362, 36.9979654545361, 
36.5846318181822, 35.7040999999992, 36.3543818181826, 36.3387527272723, 
36.5865854545457), Median = c(36.44704, 35.9190000000022, 35.56288, 
36.19223, 35.82997, 36.21986, 35.69489, 35.07475, 36.29047, 36.18302
), Maximum = c(40.2170000000067, 42.43354, 41.04897, 39.2959999999978, 
42.3659999999978, 40.524, 41.24238, 41.48184, 38.49166, 39.26223
), Minimum = c(34.1998, 31.78064, 33.38932, 34.74012, 33.96955, 
31.54425, 31.48285, 33.49063, 35.02563, 34.7831)), row.names = c(NA, 
10L), class = "data.frame")

情节 1

library(ggplot2)
library(scales)

p_2021 <- ggplot(dfinal21, aes(x = Month_Day)) +
geom_col(aes(y = `2021`), size = 1, color = "darkblue", fill = "white") +
geom_line(aes(y = `Median`), size = 1.0, color="red") +
geom_line(aes(y = `Mean`), size = 1.0, color="yellow") +
geom_line(aes(y = `Maximum`), size = 1.0, color="darkgreen") +
geom_line(aes(y = `Minimum`), size = 1.0, color = "darkviolet") +
scale_x_date(breaks = "1 month", minor_breaks = "1 day", labels=date_format("%b")) +
labs(x = "Month", y = "acre-feet", title = "Treatment Plant Production 2021")

生成此图:

情节 2

library(ggplot2)
library(scales)
p_2021 <- ggplot(dfinal21) +
geom_col(aes(x = Month_Day, y = `2021`, color = "2021")) +
geom_line(aes(x = Month_Day, y = `Median`, color = "Median")) +
geom_line(aes(x = Month_Day, y = `Mean`, color= "Mean")) +
geom_line(aes(x = Month_Day, y = `Maximum`, color= "Maximum")) +
geom_line(aes(x = Month_Day, y = `Minimum`, color= "Minimum")) +
scale_x_date(breaks = "1 month", minor_breaks = "1 day", labels=date_format("%b")) +
labs(x = "Month", y = "acre-feet", title = "Treatment Plant Production 2021") +
theme(legend.background = element_rect(fill = "white"))

生成此图:

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    事实证明,我的数据没有 ggplot2 喜欢的格式(整洁数据)。一个熟人帮了我一把。这是新代码:

    dfinal21a <- dfinal21 %>%
      mutate(y2021 = `2021`) %>% #rename column
      select(-(`2021`)) %>% # remove old col name
      pivot_longer(cols = Mean:y2021, names_to = "measure", values_to = "data") #tidy the data
    
    p_2021 <- ggplot() +
      geom_col(data = filter(dfinal21a, measure == "y2021"),
               aes(y = data, x = Month_Day),
               color = "darkblue", fill = "white") +
      geom_line(data = filter(dfinal21a, measure != "y2021"),
                aes(y = data, x = Month_Day, color = measure)) +
      scale_color_manual(values = c("red", "yellow", "darkgreen", "darkviolet")) +
      coord_cartesian(ylim = c(20,120)) + #changed range to include maximums 
      theme(legend.title = element_blank()) + 
      scale_color_discrete(name = "Year") +
      scale_x_date(breaks = "1 month", minor_breaks = "1 day", labels=date_format("%b")) +
      labs(x = "Month", y = "acre-feet", title = "Treatment Plant Production 2021")
    

    【讨论】:

      【解决方案2】:

      这里有一个解决方案!如果这是您要找的,请告诉我。然后我可以解释代码:

      代码:

      library(tidyverse)
      library(scales)
      
      # data manipulation
      dfinal21_long <- dfinal21 %>% 
        pivot_longer(
          cols = c(`2021`, Mean, Median, Maximum, Minimum),
          names_to = "names", 
          values_to = "values"
        ) %>% 
        mutate(color = case_when(names=="Mean" ~ "red",
                                 names=="Median" ~ "yellow",
                                 names=="Maximum" ~ "darkgreen",
                                 names=="Minimum" ~ "darkviolet")) %>% 
        arrange(names)
      
      # function for custom transformation of y axis
      shift_trans = function(d = 0) {
        scales::trans_new("shift", transform = function(x) x - d, inverse = function(x) x + d)
      }
      
      # plot
      
      ggplot()  + 
        geom_col(data= dfinal21_long[1:10,],mapping = aes(x=Month_Day,y=values), size = 1, color = "darkblue", fill = "white" ) +
        geom_line(data= dfinal21_long[11:50,],mapping=aes(x=Month_Day,y=values, color=color), size = 1.0) +
        scale_x_date(breaks = "1 month", minor_breaks = "1 day", labels=date_format("%b")) +
        labs(x = "Month", y = "acre-feet", title = "Treatment Plant Production 2021") + 
        scale_y_continuous(trans = shift_trans(20))+
        scale_color_manual(labels = c("Maximum","Mean","Median","Minimum"), values = c("red","yellow","darkgreen","darkviolet")) +
        theme_minimal() +
        theme(legend.position="bottom") +
        theme(legend.title=element_blank())
      

      【讨论】:

      • TarJae,您的解决方案对我不起作用。 geom_line 不会生成。这是错误:“错误:无法对不存在的列进行子集化。x 位置 11、12、13、14、15 等不存在。ℹ 只有 4 列。”抱歉,我花了这么长时间才尝试您的代码。我有一个解决方案(见下文),但还没有回到项目中。
      • 这很奇怪。我使用了您发布的数据并再次获得了发布的情节。 dfinal21 &lt;- structure(list(Month_Day = structure(c(18628,.............。并注意[11:50,]中的逗号
      • 我在上面的代码中只看到一个 geom_line 行。
      猜你喜欢
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多