Merging a bar chart with a map
Seamlessly merge a bar chart with a map to display geographically-coded categorical variables
Let us break down the example code that calls the bar_chart function:
- Data Loading:
- The code begins by loading data from a file named dataexample.
- Data Description:
- The following variables are described:
- Idx: Index of data. This could be countries, provinces, etc.
- BoundingBox: Maximum and minimum latitude and longitude values.
- Lon and Lat: Vertices defining the boundaries of the map object. These can be obtained from loading a .SHP object and extracting the vertices.
- centerLon and centerLat: By default, the center of BoundingBox, but this can be any coordinates that the user requires.
- The column titles VarX_categY are the Yth category of variable X.
- Selecting Data to Plot:
- The code defines sets of indices (idxtoplot{n}) to select the specific columns of the data table for plotting.
- These indices correspond to the columns containing data to be plotted on the bar chart.
- Creating Useful Things:
- The colormap for the bars is set using Colors=colormap('Lines'), but any type of color map can be used.
- Merging Geographical Map and Bar Chart:
- The barchart_map function is called with the loaded data (dataexample), selected indices (idxtoplot), and options (optionsfig).
- The resulting figure handle (h) and other relevant elements (hmap and hdata) are stored.
- Aesthetics and Annotations:
- After the graph is generated, the user can tweak and add various objects by making explicit references to the graph objects.
Now let us dive into the plotting function barchart_map.
The function called barchart_map generates a graph combining geographical limits with a bar chart.
- Inputs:
- data: A table containing the data.
- idxtoplot: A cell array specifying the columns of data to be plotted.
- options: A structure collecting various plot options.
- Options:
- The function checks if options is provided. If not, default values are set:
- options.factorLon: Shrinkage factor for the width of the bars.
- options.factorLat: Shrinkage factor for the height of the bars.
- options.offsetLon: Spacing between bars.
- options.newfigure: Determines whether a new figure needs to be generated.
- options.Colors: Colormap for the bars.
- Data Processing:
- The function processes the data and organizes it into YData for plotting.
- Plotting:
- It plots the geographical map using plot(data.Lon{i,1}, data.Lat{i,1}).
- For each data point, it creates stacked bars based on the specified columns and their values.
- Outputs:
- out: Figure handle.
- outmap: Elements corresponding to the map.
- outdata: Elements corresponding to the data bars.
인용 양식
O.Hubert (2024). Merging a bar chart with a map (https://www.mathworks.com/matlabcentral/fileexchange/166031-merging-a-bar-chart-with-a-map), MATLAB Central File Exchange. 검색 날짜: .
MATLAB 릴리스 호환 정보
개발 환경:
R2021b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux태그
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!버전 | 게시됨 | 릴리스 정보 | |
---|---|---|---|
1.0.0 |