admin管理员组文章数量:1344238
I have a chart showing the observed values, a prediction for each value, and a ribbon behind it indicating the error region of the prediction. Something along these lines:
library(dplyr)
library(lubridate)
library(tidyr)
library(ggplot2)
set.seed(1234)
df <- economics %>%
mutate(year = year(date)) %>%
group_by(year) %>%
summarise(pop = mean(pop, na.rm = TRUE)) %>%
ungroup() %>%
select(year, pop) %>%
rowwise() %>%
mutate(pred_factor = runif(1, 0, 2)) %>%
mutate(limit_factor = runif(1, 0.5, 0.8)) %>%
ungroup() %>%
mutate(pred = pop * pred_factor) %>%
mutate(limit_distance = pred * limit_factor) %>%
mutate(lower_limit = pred - limit_distance) %>%
mutate(upper_limit = pred + limit_distance) %>%
select(year, pop, pred, lower_limit, upper_limit)
df %>%
pivot_longer(cols = c(pop, pred), names_to = 'metric', values_to = 'value') %>%
ggplot(aes(x = year, y = value,
ymin = lower_limit, ymax = upper_limit,
linetype = metric)) +
geom_ribbon(fill = 'lightblue') +
geom_line() +
theme_bw()
However, I would like to separate out the ribbon colour into its own legend item, so I can label it separately. The best I have come up with so far is this:
df %>%
pivot_longer(cols = c(pop, pred), names_to = 'metric', values_to = 'value') %>%
mutate(shading = 'predicted range') %>%
ggplot(aes(x = year, y = value,
ymin = lower_limit, ymax = upper_limit,
linetype = metric, fill = shading)) +
geom_ribbon() +
geom_line() +
scale_fill_manual(values = c('predicted range' = 'lightblue')) +
theme_bw()
But for some reason this approach creates a dark grey fill behind the two 'metric' items in this one, rendering them essentially illegible. Is there a different way I can arrange this data and chart to have this functional?
I have a chart showing the observed values, a prediction for each value, and a ribbon behind it indicating the error region of the prediction. Something along these lines:
library(dplyr)
library(lubridate)
library(tidyr)
library(ggplot2)
set.seed(1234)
df <- economics %>%
mutate(year = year(date)) %>%
group_by(year) %>%
summarise(pop = mean(pop, na.rm = TRUE)) %>%
ungroup() %>%
select(year, pop) %>%
rowwise() %>%
mutate(pred_factor = runif(1, 0, 2)) %>%
mutate(limit_factor = runif(1, 0.5, 0.8)) %>%
ungroup() %>%
mutate(pred = pop * pred_factor) %>%
mutate(limit_distance = pred * limit_factor) %>%
mutate(lower_limit = pred - limit_distance) %>%
mutate(upper_limit = pred + limit_distance) %>%
select(year, pop, pred, lower_limit, upper_limit)
df %>%
pivot_longer(cols = c(pop, pred), names_to = 'metric', values_to = 'value') %>%
ggplot(aes(x = year, y = value,
ymin = lower_limit, ymax = upper_limit,
linetype = metric)) +
geom_ribbon(fill = 'lightblue') +
geom_line() +
theme_bw()
However, I would like to separate out the ribbon colour into its own legend item, so I can label it separately. The best I have come up with so far is this:
df %>%
pivot_longer(cols = c(pop, pred), names_to = 'metric', values_to = 'value') %>%
mutate(shading = 'predicted range') %>%
ggplot(aes(x = year, y = value,
ymin = lower_limit, ymax = upper_limit,
linetype = metric, fill = shading)) +
geom_ribbon() +
geom_line() +
scale_fill_manual(values = c('predicted range' = 'lightblue')) +
theme_bw()
But for some reason this approach creates a dark grey fill behind the two 'metric' items in this one, rendering them essentially illegible. Is there a different way I can arrange this data and chart to have this functional?
Share Improve this question asked 3 hours ago MargaretMargaret 5,93922 gold badges58 silver badges76 bronze badges1 Answer
Reset to default 1Just move the ribbon and line-specific aesthetics out of ggplot()
and into their respective geoms:
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
pivot_longer(cols = c(pop, pred), names_to = 'metric', values_to = 'value') %>%
mutate(shading = 'predicted range') %>%
ggplot(aes(x = year, y = value)) +
geom_ribbon(aes(ymin = lower_limit, ymax = upper_limit, fill = shading)) +
geom_line(aes(linetype = metric)) +
scale_fill_manual(values = c('predicted range' = 'lightblue')) +
theme_bw()
(You’ll actually get the result you want just by moving aes(linetype = metric)
into geom_line()
, but I moved the geom_ribbon()
aesthetics down as well for consistency.)
本文标签: rCreate a separate ggplot legend for a ribbon behind the main dataStack Overflow
版权声明:本文标题:r - Create a separate ggplot legend for a ribbon behind the main data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743739052a2530553.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论