我正在处理一个图,我需要以某种方式对其应用独特的回归。该图是放置在双对数轴上的一系列点。对于任何熟悉水文学的人来说,我正在尝试以图形方式生成瞬态 Theis 解,因此我的回归需要采用指数积分的形式(请参阅此维基百科页面)。
这里需要注意的是,指数积分有自己的一组独立于初始图的轴值。这就是从数据中提取解决方案的方式,但在尝试在 R 中重现它时会引入许多问题。
我想出了一些解决这个问题的想法(除了使用铅笔和纸之外),但每种方法都遇到了一些小障碍。如果您能深入了解这些解决方案的解决方法,我将不胜感激:
使用 向图中添加回归stat_smooth,根据其斜率任意选择回归上的一个点,然后将其与具有适当指数积分轴的新图的相同斜率相关联。这里的问题是我不知道如何使用stat_smooth除了y ~ x、y ~ poly(x, 2)和 的变体以外的公式y ~ log(x)。回归需要结合使用 Poly 和 Log 函数,但当我尝试这样做时,R 会出现问题。例如:
stat_smooth(data = example, method = "lm",
formula = y ~ log(x) + x - poly(x, 2)/4 + poly(x, 3)/18 - ...
Run Code Online (Sandbox Code Playgroud)我还尝试将数据和指数积分绘制为两个单独的图,然后根据我武断地认为最适合的位置重叠这两个图(这可能是更简单的方法,并且对于我来说足够准确)目的)。为了便于叠加,我剥离了指数积分图的背景、轴、网格线等,只留下带有标签的水平和垂直线,以指示曲线上具有某些值的点。如果我可以将其放在另一个图的顶部(当然,假设两个图保持相同的大小比例),我想我可以推动回归,直到它排列在我认为应该的位置,然后读出其相应的位置基于水平和垂直标记线的存在的值。
我读过一些关于 的内容annotation_raster,并认为这可能是我将回归视为叠加图像的合适方法。不过,我的问题是ggplot首先将绘图转换为栅格。 as.raster()产生以下错误:
Error in as.raster(raster) :
error in evaluating the argument 'x' in selecting a method for function 'as.raster':
Error in UseMethod("as.raster") :
no applicable method for 'as.raster' applied to an object of class "c('gg', 'ggplot')"
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用该raster包并使用该函数转换绘图,则会出现类似的错误raster()。有没有一种简单的方法可以做到这一点?
这是一些可重现的代码:
library(ggplot2)
Data = data.frame(matrix(
# Elapsed_sec Drawdown_ft
c(20, 0.0038,
40, 0.0094,
60, 0.017,
80, 0.0283,
100, 0.0358,
120, 0.0415,
140, 0.049,
160, 0.0528,
180, 0.0548,
200, 0.0567), nrow = 10, ncol = 2, byrow = TRUE))
colnames(Data) = c("Elapsed_sec", "Drawdown_ft")
Integral = data.frame(matrix(
# u W_u
c(1e-3, 6.33,
5e-3, 4.73,
1e-2, 4.04,
5e-2, 2.47,
1e-1, 1.82,
5e-1, 0.56,
1e0, 0.219,
2e0, 0.049,
3e0, 0.013,
4e0, 0.0038,
5e0, 0.0011,
6e0, 0.00036), nrow = 12, ncol = 2, byrow = TRUE))
colnames(Integral) = c("u", "W_u")
# Plot exponential integral (Theis curve)
Tcurve = ggplot(Integral, aes(1/u, W_u)) + geom_line() +
scale_x_log10(limits = c(10^-1, 10^3), breaks = c(10^-1, 10^0, 10^1, 10^2, 10^3)) +
scale_y_log10(limits = c(10^-3, 10^1), breaks = c(10^-3, 10^-2, 10^-1, 10^0, 10^1)) +
xlab("1/u") + ylab("W(u)") + coord_equal() +
geom_hline(aes(yintercept = 0.219), linetype = 2) +
geom_vline(aes(xintercept = 1), linetype = 2) +
geom_text(color = "black", size = 3, aes(x = 0.3, y = 0.3, label = "W(u) = 0.219")) +
geom_text(color = "black", size = 3, aes(x = 0.8, y = 0.01, label = "u = 1"), angle = 90) +
theme(line = element_blank(), text = element_blank(), panel.background = element_rect(fill = NA))
# Plot drawdown data
plot = ggplot(Data, aes(Elapsed_sec, Drawdown_ft)) + geom_point(alpha = 0.5, size = 1) +
scale_x_log10(limits = c(10^0, 10^4), breaks = c(10^0, 10^1, 10^2, 10^3, 10^4)) +
scale_y_log10(limits = c(10^-3, 10^1), breaks = c(10^-3, 10^-2, 10^-1, 10^0, 10^1)) +
xlab("Elapsed Time (sec)") + ylab("Drawdown (ft)") + coord_equal() + theme_bw()
Run Code Online (Sandbox Code Playgroud)
我会上传图片,但目前我缺乏最低的声誉。第一个图显示了指数积分,第二个图显示了我试图拟合积分的数据。
这个问题发布已经五年了,但我想我应该分享一下我的想法。您可以将 a 转换ggplot为raster对象,如下所示:
ggplot到任何格式的图像文件中(我会使用tiff)ggsaveraster为保存的图像创建对象rasterstack上述的实现过程如下:
# You may need to remove the margins from the plot (i.e., keep the earth raster and the routes only)
plot <- plot+
theme(
axis.ticks=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "null"),
legend.position = 'none'
) +
labs(x=NULL, y=NULL)
# Save the plot
ggsave(plot=plot, "my_ggplot.tiff", device = "tiff")
# Create a StackedRaster object from the saved plot
raster <- raster("my_ggplot.tiff") # OR stack("my_ggplot.tiff") for colored images
# Get the GeoSpatial Components
lat_long <- ggplot_build(plot)$layout$panel_params[[1]][c("x.range","y.range")]
# Supply GeoSpatial data to the StackedRaster
extent(raster) <- c(lat_long$x.range,lat_long$y.range)
projection(raster) <- CRS("+proj=longlat +datum=WGS84")
# Then, do whatever you want with the raster object here
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
3036 次 |
| 最近记录: |