How to mix a pie chart and a graphic with geobubble
4 次查看(过去 30 天)
显示 更早的评论
My first problem is that I tried to make a pie chart with my data from a table but matlab send a error because it wasn´t a vector of scalar numbers. Which is wrong because when I made my geobubble with the same information it works great and it read the numbers perfectly. The other problem is how to make pie charts for a big amount of cases like a pie chart with information for each country in the world and finally mix the pie chart with the geobubble.
error: Error using pie>parseArgs (line 103) Subscripting into a table using one subscript (as in t(i)) or three or more subscripts (as in t(i,j,k)) is not supported. Always specify a row subscript and a variable subscript, as in t(rows,vars). Error in pie (line 72) [sliceCounts, explode, labels, displayNames] = parseArgs(args, nargs);
code and data:
clc;
clear;
url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/8a22c55b5ae70966b26a4f83eecdc31e0142ce71/csse_covid_19_data/csse_covid_19_daily_reports/04-28-2020.csv';
data = webread (url);
M = websave ('covid.csv', url);
T = readtable ('covid.xlsx');
T.Properties.VariableNames{4} = 'Pais';
T.Properties.VariableNames{6} = 'Latitud';
T.Properties.VariableNames{7} = 'Longitud';
T.Properties.VariableNames{8} = 'Contagiados';
T.Properties.VariableNames{9} = 'Muertos';
T.Properties.VariableNames{10} = 'Recuperados';
Pais = categorical (T.Pais);
%GRÁFICOS
figure(1);
geobubble (T.Latitud, T.Longitud, T.Contagiados, Pais)
2 个评论
Tommy
2020-5-9
You can create a pie chart where each slice contains the total number of cases in a given country:
[G, labels] = findgroups(T.Pais);
cases = splitapply(@sum, T.Contagiados, G);
pie(cases, labels);
It's not very readable because your data contains 185 countries. You could only display the country name if that country has at least 5% of the total cases:
[G, labels] = findgroups(T.Pais);
cases = splitapply(@sum, T.Contagiados, G);
labels(cases < 0.05*max(cases)) = {''};
pie(cases, labels);
How are you imagining the combined pie chart and geobubble would look? If you want the colors in the geobubble to correspond to colors in the pie chart, the pie chart might become hard to read, as a geobubble won't plot more than 7 different colors. Many different countries in the geobubble share the same color.
回答(1 个)
Ankriti Sachan
2020-5-11
There could be two workarounds:
- Workaround 1
I am attaching a script ‘plotMap.m’ to help you understand with the workflow
1. Construct the map axes, and plot any relevant base map data.
Then, looping over the pie charts:
2. a) Using MFWDTRAN (on the map axes) to get the (x,y) coordinates of the desired chart location given its latitude and longitude.
b) Construct a pie chart in a separate axes and figure. While creating the pie chart, capture the handles array that it returns. This array will contain handles to patches and text objects.
>>fTemp= figure()
>>PieHandles= pie( data );
c) Loop over each element of the “PieHandles” vector and rescale the pie chart by multiplying XData and YData by a constant , for each patch. Add the offset derived in step (2.a) to change the position. You can rescale the labeling by changing the font size.
GET function can be used to find the ‘type’ of the handle object and determine if it is a patch or text. It can also be used to get the values of ’Xdata’ and ‘Ydata’ for the patches or ‘Position’ and ‘FontSize’ for text.SET function can then be used to set the properties as desired.
d) Re-parent each patch and text in the scaled and shifted pie, thus moving it into the map axes. You can use COPYOBJ function to do so.
e) Close the temporary figure “fTemp”
The above mentioned workflow will help attain a scalable workaround to plot pie charts with maps.
- Workaround 2
If you wish the pie charts to be linked to a fixed Latitude/Longitude and scale as you scroll around or zoom in / zoom out the map, you can consider using PATCHM and LINEM functions. These functions allow you to plot patches and lines on a map. Based on the values for the pie chart, you can calculate the angles and thus the patches that can be used to plot the pie chart. Please refer to the documentation about these functions to learn more about the use.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Geographic Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!