绘制(ggplot?)平滑+ 2条曲线之间的颜色区域 [英] plot (ggplot ?) smooth + color area between 2 curves
问题描述
请问我一个问题:
我的数据:
Nb_obs <- as.vector(c( 2, 0, 6, 2, 7, 1, 8, 0, 2, 1, 1, 3, 11, 5, 9, 6, 4, 0, 7, 9))
Nb_obst <- as.vector(c(31, 35, 35, 35, 39, 39, 39, 39, 39, 41, 41, 42, 43, 43, 45, 45, 47, 48, 51, 51))
inf20 <- as.vector(c(2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 4, 3, 5, 4))
sup20 <- as.vector(c(3, 4, 4, 4, 5, 4, 4, 5, 4, 4, 5, 5, 5, 6, 5, 6, 6, 5, 7, 6))
inf40 <- as.vector(c(1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 3, 3, 4, 3))
sup40 <- as.vector(c(4, 5, 5, 5, 6, 5, 5, 6, 5, 5, 6, 6, 6, 7, 6, 7, 7, 7, 9, 7))
inf60 <- as.vector(c(1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 2))
sup60 <- as.vector(c(5, 6, 6, 6, 8, 7, 7, 7, 7, 7, 7, 7, 8, 9, 8, 9, 9, 9, 11, 9))
inf90 <- as.vector(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1))
sup90 <- as.vector(c(10, 11, 11, 11, 15, 13, 13, 14, 12, 13, 13, 13, 14, 17, 15, 17, 17, 16, 21, 18))
data <- cbind.data.frame(Nb_obs, Nb_obst, inf20, sup20, inf40, sup40, inf60 , sup60, inf90 , sup90)
我的情节:
plot(data$Nb_obst, data$Nb_obs, type = "n", xlab = "Number obst", ylab = "number obs", ylim = c(0, 25))
lines(data$Nb_obst, data$inf20, col = "dark red")
lines(data$Nb_obst, data$sup20, col = "dark red")
lines(data$Nb_obst, data$inf40, col = "red")
lines(data$Nb_obst, data$sup40, col = "red")
lines(data$Nb_obst, data$inf60, col = "dark orange")
lines(data$Nb_obst, data$sup60, col = "dark orange")
lines(data$Nb_obst, data$inf90, col = "yellow")
lines(data$Nb_obst, data$sup90, col = "yellow")
我的问题:
我想做两件事(所以我认为可以通过ggplot完成):
There are two things I'd like to do (and so I think it could be done by ggplot):
在顶部的图表中,"inf"和"sup"是我模型中IC的极限,分别为20%,40%,60%和90%.我首先要平滑每条曲线,然后再为同一IC的两条曲线之间的表面着色,例如,"data $ inf90"和"data $ sup90"之间的表面为黄色,"data $ inf60"和"data $ 60"是橙色,等等.我想将这些彩色表面中的每一个都叠加起来,并加上好图例.
In the idea of the graph at the top, the "inf" and "sup" are limits of my model in the IC 20%, then 40%, then 60%, and finally 90%. I would first like to smooth each curve, and then I would like to color the surface between two curves of the same IC, for example that the surface between "data$inf90" and "data$sup90" is yellow, the area between "data$inf60" and "data$60" is orange, etc. And I would like to superimpose each of these colored surfaces + put the good legend please.
感谢您的帮助!
推荐答案
很酷的问题,因为我不得不给自己一个使用LOESS织带的速成班!
Cool question since I had to give myself a crash course in using LOESS for ribbons!
我要做的第一件事是使数据变长,因为这正是 ggplot
所期望的,并且因为您的数据具有某些隐藏在值内的特征.例如,如果您将 gather
做成一个长形,并说出一列 key
,其值为"inf20",另一个为"sup20",则这些将保存更多信息.比您当前拥有的权限大,即度量类型为"inf"或"sup",级别为20.您可以从该列中提取该信息以获取度量类型列("inf"或"sup")和级别(20、40、60或90),然后将美观程度映射到这些变量上.
First thing I'm doing is getting the data into a long shape, since that's what ggplot
will expect, and since your data has some characteristics that are kind of hidden within values. For example, if you gather
into a long shape and have, say a column key
, with a value of "inf20" and another of "sup20", those hold more information than you currently have access to, i.e. the measure type is either "inf" or "sup", and the level is 20. You can extract that information out of that column to get columns of measure types ("inf" or "sup") and levels (20, 40, 60, or 90), then map aesthetics onto those variables.
所以在这里,我将数据整理成长形,然后使用 spread
来创建 inf
和 sup
的列,因为将分别变为功能区的 ymin
和 ymax
.我将 level
设置为一个因子并反转其水平,因为我想更改绘制色带的顺序,以使较窄的色带最后出现并绘制在顶部.
So here I'm getting the data into a long shape, then using spread
to make columns of inf
and sup
, because those will become ymin
and ymax
for the ribbons. I made level
a factor and reversed its levels, because I wanted to change the order of the ribbons being drawn such that the narrow one would come up last and be drawn on top.
library(tidyverse)
data_long <- data %>%
as_tibble() %>%
gather(key = key, value = value, -Nb_obs, -Nb_obst) %>%
mutate(measure = str_extract(key, "\\D+")) %>%
mutate(level = str_extract(key, "\\d+")) %>%
select(-key) %>%
group_by(level, measure) %>%
mutate(row = row_number()) %>%
spread(key = measure, value = value) %>%
ungroup() %>%
mutate(level = as.factor(level) %>% fct_rev())
head(data_long)
#> # A tibble: 6 x 6
#> Nb_obs Nb_obst level row inf sup
#> <dbl> <dbl> <fct> <int> <dbl> <dbl>
#> 1 0 35 20 2 2 4
#> 2 0 35 40 2 2 5
#> 3 0 35 60 2 1 6
#> 4 0 35 90 2 0 11
#> 5 0 39 20 8 3 5
#> 6 0 39 40 8 2 6
ggplot(data_long, aes(x = Nb_obst, ymin = inf, ymax = sup, fill = level)) +
geom_ribbon(alpha = 0.6) +
scale_fill_manual(values = c("20" = "darkred", "40" = "red",
"60" = "darkorange", "90" = "yellow")) +
theme_light()
但是它仍然存在锯齿状的问题,因此对于每个级别,我都预测了 inf
和 sup
与 Nb_obst
的平滑值黄土
. group_by
和 do
产生一个嵌套的数据框,而 unnest
将其拉回到可行的形式.可以随意调整 span
参数以及我不太了解的其他 loess.control
参数.
But it still has the issue of being jagged, so for each level I predicted smoothed values of both inf
and sup
versus Nb_obst
using loess
. group_by
and do
yield a nested data frame, and unnest
pulls it back out into a workable form. Feel free to adjust the span
parameter, as well as other loess.control
parameters that I know very little about.
data_smooth <- data_long %>%
group_by(level) %>%
do(Nb_obst = .$Nb_obst,
inf_smooth = predict(loess(.$inf ~ .$Nb_obst, span = 0.35), .$Nb_obst),
sup_smooth = predict(loess(.$sup ~ .$Nb_obst, span = 0.35), .$Nb_obst)) %>%
unnest()
head(data_smooth)
#> # A tibble: 6 x 4
#> level Nb_obst inf_smooth sup_smooth
#> <fct> <dbl> <dbl> <dbl>
#> 1 90 35 0 11.
#> 2 90 39 0 13.4
#> 3 90 48 0.526 16.7
#> 4 90 39 0 13.4
#> 5 90 41 0 13
#> 6 90 41 0 13
ggplot(data_smooth, aes(x = Nb_obst, ymin = inf_smooth, ymax = sup_smooth, fill = level)) +
geom_ribbon(alpha = 0.6) +
scale_fill_manual(values = c("20" = "darkred", "40" = "red",
"60" = "darkorange", "90" = "yellow")) +
theme_light()
由 reprex软件包(v0.2.0)创建于2018-05-26.
Created on 2018-05-26 by the reprex package (v0.2.0).
这篇关于绘制(ggplot?)平滑+ 2条曲线之间的颜色区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!