How to use stacked bar charts to draw multiple confidence intervals
3 Commenti
Hi @ Hongyun,
First, I will define the coefficients, lower bounds, and upper bounds as provided in your question.
% Coefficients
coef = [-0.0186; 0.0057; -0.0067; -0.0007; 0; -0.0295; -0.0517; -0.0651;
-0.0689; -0.0862; -0.0866];
% Lower bounds for confidence intervals
Lower_Bound = [
-0.061944, -0.051528, -0.04632, -0.042792, -0.040104; -0.04203, -0.03056, -0.024825, -0.02094, -0.01798; -0.05314, -0.04198, -0.0364, -0.03262, -0.02974; -0.044302, -0.033824, -0.028585, -0.025036, -0.022332; 0, 0, 0, 0, 0; -0.07723, -0.06576, -0.060025, -0.05614, -0.05318; -0.103042, -0.090704, -0.084535, -0.080356, -0.077172; -0.121602, -0.108024, -0.101235, -0.096636, -0.093132; -0.132368, -0.117116, -0.10949, -0.104324, -0.100388; -0.152506, -0.136572, -0.128605, -0.123208, -0.119096; -0.183092, -0.159904, -0.14831, -0.140456, -0.134472 ];
% Upper bounds for confidence intervals
Upper_Bound = [ 0.024744, 0.014328, 0.00912, 0.005592, 0.002904; 0.05343, 0.04196, 0.036225, 0.03234, 0.02938; 0.03974, 0.02858, 0.023, 0.01922, 0.01634; 0.042902, 0.032424, 0.027185, 0.023636, 0.020932; 0, 0, 0, 0, 0; 0.01823, 0.00676, 0.001025, -0.00286, -0.00582; -0.000358, -0.012696, -0.018865, -0.023044, -0.026228; -0.008598, -0.022176, -0.028965, -0.033564, -0.037068; -0.005432, -0.020684, -0.02831, -0.033476, -0.037412; -0.019894, -0.035828, -0.043795, -0.049192, -0.053304; 0.009892, -0.013296, -0.02489, -0.032744, -0.038728 ];
Then calculate the heights of the bars to create the stacked bar chart. The heights for the lower bounds will be the difference between the coefficients and the lower bounds, while the heights for the upper bounds will be the difference between the upper bounds and the coefficients.
% Calculate the heights for the lower and upper bounds
lower_heights = coef - Lower_Bound;
upper_heights = Upper_Bound - coef;
Once you have the heights for the lower and upper bounds, you can create the stacked bar chart using the bar function.
% Create the stacked bar chart
figure;
bar([lower_heights, upper_heights], 'stacked');
% Set the x-axis labels
set(gca, 'XTickLabel', {'1%', '5%', '10%', '15%', '20%'});
xlabel('Confidence Levels');
ylabel('Coefficient Values');
title('Stacked Bar Chart of Coefficients with Confidence Intervals');
legend({'Lower Bound', 'Upper Bound'}, 'Location', 'Best');
Finally, you can customize the chart to enhance its readability and presentation. This includes adding grid lines, adjusting colors, and ensuring that the legend is clear.
% Customize the appearance
grid on;
% Custom colors for lower and upper bounds
colormap([0.8 0.2 0.2; 0.2 0.8 0.2]);
Please see attached.
Feel free to adjust the aesthetics and parameters to better fit your specific needs and preferences. If you have any further questions, please let me know.
Risposta accettata
Hi @ Hongyun,
So,your goal is to create a visually intuitive representation of these coefficients such that they are centered at zero on a stacked bar chart, with lower and upper bounds extending symmetrically around them. To achieve this, I need to adjust how I calculate and plot the heights of the bars in relation to the coefficients. Here’s an updated version of the code,
% Coefficients
coef = [-0.0186; 0.0057; -0.0067; -0.0007; 0; -0.0295; -0.0517; -0.0651; -0.0689; -0.0862; -0.0866];
% Lower bounds for confidence intervals
Lower_Bound = [ -0.061944, -0.051528, -0.04632, -0.042792, -0.040104; -0.04203, -0.03056, -0.024825, -0.02094, -0.01798; -0.05314, -0.04198, -0.0364, -0.03262, -0.02974; -0.044302, -0.033824, -0.028585, -0.025036, -0.022332; 0, 0, 0, 0, 0; -0.07723, -0.06576, -0.060025, -0.05614, -0.05318; -0.103042, -0.090704, -0.084535, -0.080356, -0.077172; -0.121602, -0.108024, -0.101235, -0.096636, -0.093132; -0.132368, -0.117116, -0.10949, -0.104324, -0.100388; -0.152506, -0.136572, -0.128605, -0.123208, -0.119096; -0.183092, -0.159904, -0.14831, -0.140456, -0.134472 ];
% Upper bounds for confidence intervals
Upper_Bound = [ 0.024744, 0.014328, 0.00912, 0.005592, 0.002904; 0.05343, 0.04196, 0.036225, 0.03234, 0.02938; 0.03974, 0.02858, 0.023, 0.01922, 0.01634; 0.042902, 0.032424, 0.027185, 0.023636, 0.020932; 0, 0, 0, 0, 0; 0.01823, 0.00676, 0.001025, -0.00286, -0.00582; -0.000358, -0.012696, -0.018865, -0.023044, -0.026228; -0.008598, -0.022176, -0.028965, -0.033564, -0.037068; -0.005432, -0.020684, -0.02831, -0.033476, -0.037412; -0.019894, -0.035828, -0.043795, -0.049192, -0.053304; 0.009892, -0.013296, -0.02489, -0.032744, -0.038728 ];
% Calculate heights for plotting
lower_heights = coef - Lower_Bound; % Height from coefficient to lower bound
upper_heights = Upper_Bound - coef; % Height from coefficient to upper bound
% Prepare data for stacked bar chart
data_to_plot = [Lower_Bound(:, 1), lower_heights, upper_heights];
% Create stacked bar chart
figure;
hBar = bar(data_to_plot, 'stacked');
% Set x-axis labels and other properties
set(gca, 'XTickLabel', {'1%', '5%', '10%', '15%', '20%'});
xlabel('Confidence Levels');
ylabel('Coefficient Values');
title('Stacked Bar Chart of Coefficients with Confidence Intervals');
legend({'Lower Bound', 'Coefficient', 'Upper Bound'}, 'Location', 'Best');
% Customize appearance
grid on;
colormap([0.8 0.2 0.2; 0 0 1; 0.2 0.8 0.2]); % Custom colors for each part
Please see attached.
So, after going through @dpb comments, I did concur with his comments. It was my fault not paying attention to comments.
% Coefficients
coef = [-0.0186, 0.0057, -0.0067, -0.0007, 0, -0.0295, -0.0517,
-0.0651, ... -0.0689, -0.0862, -0.0866];
% Lower bounds for confidence intervals
Lower_Bound = [ -0.061944, -0.051528, -0.04632, -0.042792,
-0.040104; -0.04203, -0.03056, -0.024825, -0.02094, -0.01798; -0.05314, -0.04198, -0.0364, -0.03262, -0.02974; -0.044302, -0.033824, -0.028585, -0.025036, -0.022332; 0, 0, 0, 0, 0; -0.07723, -0.06576, -0.060025, -0.05614, -0.05318; -0.103042, -0.090704, -0.084535, -0.080356, -0.077172; -0.121602, -0.108024, -0.101235, -0.096636, -0.093132; -0.132368, -0.117116, -0.10949, -0.104324, -0.100388; -0.152506, -0.136572, -0.128605, -0.123208, -0.119096; -0.183092, -0.159904, -0.14831, -0.140456, -0.134472];
% Upper bounds for confidence intervals
Upper_Bound = [ 0.024744, 0.014328, 0.00912, 0.005592, 0.002904; 0.05343, 0.04196, 0.036225, 0.03234, 0.02938; 0.03974, 0.02858, 0.023, 0.01922, 0.01634; 0.042902, 0.032424, 0.027185, 0.023636, 0.020932; 0, 0, 0, 0, 0; 0.01823, 0.00676, 0.001025, -0.00286, -0.00582; -0.000358, -0.012696, -0.018865, -0.023044, -0.026228; -0.008598, -0.022176, -0.028965, -0.033564, -0.037068; -0.005432, -0.020684, -0.02831, -0.033476, -0.037412; -0.019894, -0.035828, -0.043795, -0.049192, -0.053304; 0.009892, -0.013296, -0.02489, -0.032744, -0.038728];
% Reshape coef to match dimensions
coef_matrix = repmat(coef', 1, size(Lower_Bound, 2));
% Calculate differences for plotting
lower_heights = coef_matrix - Lower_Bound; % Height from coefficient to lower bound
upper_heights = Upper_Bound - coef_matrix; % Height from coefficient to upper bound
% Prepare data for stacked bar chart
data_to_plot = [Lower_Bound, lower_heights, upper_heights];
% Create stacked bar chart
figure;
hBar = bar(data_to_plot, 'stacked');
% Set x-axis labels and other properties
set(gca, 'XTickLabel', {'1%', '5%', '10%', '15%', '20%'});
xlabel('Confidence Levels');
ylabel('Coefficient Values');
title('Stacked Bar Chart of Coefficients with Confidence Intervals');
legend({'Lower Bound', 'Coefficient', 'Upper Bound'}, 'Location', 'Best');
% Customize appearance
grid on;
colormap([0.8 0.2 0.2; 0 0 1; 0.2 0.8 0.2]); % Custom colors for each part
Please see updated attached plot.
Hope this helps. Please let me know if you have any further questions.
27 Commenti
Più risposte (1)
3 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!