R 从ggplot2打印生成光栅
我正在处理一个绘图,我需要以某种方式应用一个独特的回归。绘图是放置在原木轴上的一系列点。对于任何熟悉水文学的人来说,我试图以图形的方式产生一个瞬态的泰斯解,所以我的回归需要以指数积分的形式出现(见下图) 这里需要注意的是,指数积分有自己的一组轴值,这些轴值独立于初始图。这就是从数据中提取解决方案的方式,但在R中尝试重现它时会带来许多问题 我想出了一些解决这个问题的方法(除了使用铅笔和纸),但每种方法都遇到了一些小问题。如果您能深入了解这些解决方案的解决方案,我将不胜感激:R 从ggplot2打印生成光栅,r,ggplot2,R,Ggplot2,我正在处理一个绘图,我需要以某种方式应用一个独特的回归。绘图是放置在原木轴上的一系列点。对于任何熟悉水文学的人来说,我试图以图形的方式产生一个瞬态的泰斯解,所以我的回归需要以指数积分的形式出现(见下图) 这里需要注意的是,指数积分有自己的一组轴值,这些轴值独立于初始图。这就是从数据中提取解决方案的方式,但在R中尝试重现它时会带来许多问题 我想出了一些解决这个问题的方法(除了使用铅笔和纸),但每种方法都遇到了一些小问题。如果您能深入了解这些解决方案的解决方案,我将不胜感激: 使用stat\u sm
stat\u smooth
将回归添加到绘图中,根据其斜率在回归上任意选择一个点,然后将该点与具有适当指数积分轴的新绘图的相同斜率相关联。这里的问题是,我不知道如何将stat\u 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 - ...
注释\u光栅的内容,认为将回归视为叠加图像可能是一种合适的方式。不过,我的问题是先将ggplot
plot转换为光栅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')"
如果我尝试使用光栅
软件包并使用光栅()
函数转换绘图,则会出现类似错误。有没有一个简单的方法可以做到这一点
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()
我会上传图片,但我目前缺乏起码的声誉。第一个图显示了指数积分,而第二个图显示了我试图拟合积分的数据。这个问题发表已经五年了,但我想我会分享我的想法。您可以将
ggplot
转换为graster
对象,如下所示:
# 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
ggsave
光栅
为保存的图像创建光栅
对象(或为彩色图像创建堆栈
)# 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
#您可能需要从绘图中删除边距(即,仅保留地球光栅和路线)
您可以发布一个可复制的示例(即一些虚拟数据和可复制代码)。您不能强制从ggplot
对象到光栅
-无疑会有一个简单的解决方案,但如果没有可复制的示例,则很难推荐一个。@mnel可复制的代码已添加。我已经大大减少了数据集,所以请告诉我添加其他点是否有帮助。