我正在探索特定索引如何变化。该指数是对数转换量表上第10个百分点与第90个百分点之间的比率的度量。为了探索索引,我创建了这个数据集:
dt =
data_frame(p10 = sample(seq(0.1,1.25,0.05), replace = TRUE, 10000),
p90 = sample(seq(0.25,1.75,0.05), replace = TRUE, 10000)) %>%
filter(p90 > p10) %>%
mutate(p10 = log(p10), p90 = log(p90)) %>%
mutate(index = p10/p90) %>%
filter(abs(index) < 10) %>%
select(index, p10, p90)
dt
我绘制了数据集,以查看随着对数转换的百分位数变化,索引如何变化。
dt %>%
ggplot(aes(x = p10, y = p90)) +
geom_point(aes(colour = index)) +
geom_abline(colour = "black", size = 0.75) +
geom_hline(colour = "black", yintercept = 0, size = 0.75) +
geom_vline(colour = "black", xintercept = 0, size = 0.75) +
scale_colour_distiller(type = "div", palette = 1) +
coord_equal() +
xlim(-2.5,0.75) +
ylim(-2.5,0.75) +
theme_bw()
此图很好地说明了该点,适合我个人使用,但是对于演示而言并不理想。我希望可以将点替换为带有渐变填充的多边形。但是,弄清楚如何做到这一点超出了我的能力范围。此外,我不确定是否确实可行。有人会为此介意将我指向正确的方向吗?非常感谢!
答案 0 :(得分:1)
您可以签出interp
函数来查看它是否满足您的需求:
library(akima)
# interpolate data
dt.interp <- interp(x = dt$p10, y = dt$p90, z = dt$index,
duplicate = "mean",
nx = 100, ny = 100) # set nx / ny based on how fine your want the polygons to be
# convert results back to a data frame
dt.interp <- data.frame(
p10 = rep(dt.interp$x, times = length(dt.interp$y)),
p90 = rep(dt.interp$y, each = length(dt.interp$x)),
index = as.vector(dt.interp$z)
)
# plot results, replacing geom_point with geom_tile & color scale with fill scale
dt.interp %>%
ggplot(aes(x = p10, y = p90)) +
geom_tile(aes(fill = index)) +
geom_abline(colour = "black", size = 0.75) +
geom_hline(colour = "black", yintercept = 0, size = 0.75) +
geom_vline(colour = "black", xintercept = 0, size = 0.75) +
scale_fill_distiller(type = "div", palette = 1) +
coord_equal() +
xlim(-2.5,0.75) +
ylim(-2.5,0.75) +
theme_bw()
(灰色区域是NA值,因为那里没有可插值的点。如果要隐藏它们,可以在na.omit()
上对df.interp
运行+------------+----------+------------+--------+------------+--------+
| Closed | ClosedNo | Open | OpenNo | Plan | PlanNo |
+------------+----------+------------+--------+------------+--------+
| NULL | 0 | NULL | 0 | 2018-10-22 | 3 |
| NULL | 0 | NULL | 0 | 2018-10-23 | 1 |
| NULL | 0 | NULL | 0 | 2018-10-24 | 1 |
| NULL | 0 | NULL | 0 | 2018-10-25 | 1 |
| NULL | 0 | 2018-10-25 | 1 | NULL | 0 |
| NULL | 0 | 2018-10-26 | 1 | NULL | 0 |
| NULL | 0 | 2018-10-27 | 2 | NULL | 0 |
| 2018-10-22 | 3 | NULL | 0 | NULL | 0 |
| 2018-10-23 | 1 | NULL | 0 | NULL | 0 |
| 2018-10-25 | 1 | NULL | 0 | NULL | 0 |
+------------+----------+------------+--------+------------+--------+
或将它们分配给其他对象颜色。
答案 1 :(得分:0)
这是一个将点转换为多边形并设置每个颜色的示例。我怀疑这正是您想要的,但这也许只是一个开始。
library(dplyr)
library(ggplot2)
library(sp)
library(rgeos)
dt =
data_frame(p10 = sample(seq(0.1,1.25,0.05), replace = TRUE, 10000),
p90 = sample(seq(0.25,1.75,0.05), replace = TRUE, 10000)) %>%
filter(p90 > p10) %>%
mutate(p10 = log(p10), p90 = log(p90)) %>%
mutate(index = p10/p90) %>%
filter(abs(index) < 10) %>%
select(index, p10, p90)
dt.points = dt
coordinates(dt.points) = ~p10 + p90
dt.polygons = gBuffer(dt.points, width = 0.01, byid = T)
dt.polygons$colour = cut(dt$index, breaks = 10, labels = F)
plot(dt.polygons, col=dt.polygons$colour)