R语言绘制过程中需注意目标估计函数的内置函数是否有分位数计算函数,否则需使用quantile()函数计算相关分位数值。下面以R语言中GAMLSS内置Gamma分布分位数计算函数qGA()为例介绍绘图方法。
Quantile_GA function (obj, data,y){
Q_list = NULL
Q_list 0.05,mu = obj$mu.fv, sigma = obj$sigma.fv),
qGA(0.25,mu = obj$mu.fv, sigma = obj$sigma.fv),
qGA(0.5,mu = obj$mu.fv, sigma = obj$sigma.fv),
qGA(0.75,mu = obj$mu.fv, sigma = obj$sigma.fv),
qGA(0.95,mu = obj$mu.fv, sigma = obj$sigma.fv))
colnames(Q_list) "5%","25%","50%","75%","95")
Q_list
根据需求(设定分位点、均值、方差等)依次计算所需分位点函数值,构建数据向量空间。pp<-ggplot()+
geom_ribbon(aes(ymin = Q_list[,1],ymax = Q_list[,2], x = index),fill = 'lightskyblue', alpha =0.3)+
geom_ribbon(aes(ymin = Q_list[,2],ymax = Q_list[,4], x = index),fill = 'lightskyblue1', alpha =0.8)+
geom_ribbon(aes(ymin = Q_list[,4],ymax = Q_list[,5], x = index),fill = 'lightskyblue1', alpha =0.3)+
geom_line(aes(x = index, y = Q_list[,3]), col = "gray40", alpha = 0.6)+
geom_point(aes(x = index, y = data[,y]), col = "gray26",alpha=0.5)+
labs(x = " ", y = " ") +
theme(plot.title = element_text(hjust = 0.5,size = 20,family = "RMN"),
axis.text = element_text(size = 20,family = "RMN"),
text = element_text(size = 20,family = "RMN"))
return(pp)
}
geom_ribbon()可以绘制带状曲线,需要分别设定上下限。此处将计算的向量按需依次代入计算即可。根据图像所需,使用theme()图像主题背景设置函数调整字号、颜色、坐标轴等要素。
在水文图绘HF.054中我们曾介绍过python绘制置信带的关键代码,使用fill_between函数,提供上下估计误差,即可实现置信带绘制。
ax3 = plt.gca()
rate = np.array([0, 0.25, 0.5, 0.75, 1, 1.5, 2])
plotK(16.094376 * rate, invert=True)
plt.fill_between(Resp_dict['yrs'], Resp_dict['p17'], Resp_dict['p83'],
alpha=alpha, facecolor='royalblue', label='likely range')
plt.plot(Resp_dict['yrs'], Resp_dict['p50'], color='k', linewidth=LW, linestyle=':',
label='Central estimate (ECS)')
plt.plot(Resp_dict['yrs'], Resp_dict['p50_0.5_pattern'], color='grey', linewidth=LW, linestyle=':',
label='Pattern effect -0.5 W m$^{-2}$ C$^{-1}$')
plt.xlim(xmin, xmax)
plt.ylim(-ymax, -ymin)
ax3.yaxis.set_ticks_position('right')
ax3.tick_params(width=0.5)
plt.title('c) Integrated Radiative Response')
plt.legend(loc='lower left', frameon=False, fontsize=fontsize)
在Matlab中绘制置信带有两种方式,一种是根据目标拟合函数计算置信区间后进行图像带填充;另一种是采用拟合函数内置分位数值计算函数进行绘图。下面转自好玩的Matlab推文中的相关示例。需要注意的是,示例二中采用最小二乘法拟合函数polyfit()进行线性拟合,读者在应用时需根据目标拟合函数(不一定是最小二乘法)进行调整。
示例一:
clc;clear;close all;
%--------------------------------------------------------------------------
% @Author: 好玩的Matlab
% @公众号:好玩的Matlab
% @Created: 09,06,2023
% @Email:2377389590@qq.com
%--------------------------------------------------------------------------
% 使用提供的数据
data = repmat([2 2], 200, 1) + randn(200, 2) * [1 .5; 0 1.32];
xData = data(:, 1);
yData = data(:, 2);
% 绘制散点图
figure('Position',[383 355 886 507]);
scatter(xData, yData, 'r', 'filled', 'MarkerEdgeColor', 'none', 'MarkerFaceAlpha', 0.4);
hold on;
% 进行线性回归
linearModel = fitlm(xData, yData);
% 计算回归曲线和置信区间
xFit = linspace(min(xData), max(xData), 200)';
[yPred, yConfInterval] = predict(linearModel, xFit, 'Alpha', 0.05); % 95%置信区间
% 绘制回归曲线
plot(xFit, yPred, 'r', 'LineWidth', 2);
% 使用fill函数绘制置信区间
fill([xFit; flipud(xFit)], [yConfInterval(:, 1); flipud(yConfInterval(:, 2))], 'g', 'FaceAlpha', 0.3, 'EdgeColor', 'none');
defaultAxes
legend(gca, 'Data', 'Regression Line','95%','Confidence Interval','box','on','Location','best','EdgeColor', [1 1 1]*0.8);
clc;clear;close all;
%--------------------------------------------------------------------------
% @Author: 好玩的Matlab
% @公众号:好玩的Matlab
% @Created: 09
,06,2023
% @Email:2377389590@qq.com
%--------------------------------------------------------------------------
% 使用提供的数据
data = repmat([2 2], 200, 1) + randn(200, 2) * [1 .5; 0 1.32];
xData = data(:, 1);
yData = data(:, 2);
% 创建图形窗口并持有当前图
figure('Position',[383 355 886 507]);
hold on;
% 使用polyfit进行线性拟合
[polyCoeff, polyStruct] = polyfit(xData, yData, 1);
xFit = linspace(min(xData), max(xData), 200);
[yFit, fitDelta] = polyval(polyCoeff, xFit, polyStruct);
alpha = 0.05; % 对于95%置信区间
zValue = norminv(1 - alpha/2, 0, 1); % 这将返回1.960
% 计算置信区间
upperConfInt = yFit + zValue .* fitDelta;
lowerConfInt = yFit - zValue .* fitDelta;
% 绘制原始数据
scatter(xData, yData, 'r', 'filled', 'MarkerEdgeColor', 'none', 'MarkerFaceAlpha', 0.4);
% 绘制拟合曲线
plot(xFit, yFit, 'r', 'LineWidth', 2.5);
% 使用fill函数绘制置信区间
fill([xFit, fliplr(xFit)], [upperConfInt, fliplr(lowerConfInt)], 'g', 'EdgeColor', 'none', 'FaceAlpha', 0.15);
defaultAxes
legend(gca, 'Data', 'Regression Line','95%','Confidence Interval','box','on','Location','best','EdgeColor', [1 1 1]*0.8);