【发布时间】: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"))
生成此图:
【问题讨论】: