How to Make Maps Using Mapping Software (QGIS) (user search)
       |           

Welcome, Guest. Please login or register.
Did you miss your activation email?
April 24, 2024, 09:12:04 AM
News: Election Simulator 2.0 Released. Senate/Gubernatorial maps, proportional electoral votes, and more - Read more

  Talk Elections
  About this Site
  Talk Elections Q & A
  How To (Moderator: Virginiá)
  How to Make Maps Using Mapping Software (QGIS) (search mode)
Pages: [1] 2
Author Topic: How to Make Maps Using Mapping Software (QGIS)  (Read 22514 times)
cinyc
Atlas Icon
*****
Posts: 12,721


« on: October 30, 2017, 10:46:23 PM »
« edited: November 04, 2017, 05:51:07 PM by cinyc »

Some posters have asked me for a tutorial on how to make election maps without tediously using paint. I've taught a few others before, so I thought I'd put up a post here.

In general, you need a few things to make maps:

1) A GIS mapping program - either free (QGIS, MapWindow GIS, Saga GIS) or paid (ArcGIS). In these tutorials, we will be using QGIS.
2) Shapefiles of the map you're looking for. The Census Bureau has shapefiles for the most common map types - States, Counties, County Subdivisions and Congressional/Legislative Districts. Some states/municipalities provide shapefiles for precincts.
3) Color styles for your mapping program. You can generate these on your own - or, eventually, I'll provide a link to a zip of mine for QGIS.
4) Excel and/or Open Office Calc (free).  Unlike Excel, Open Office Calc allows you to save .dbf files. Excel doesn't. If you decide to only use Open Office Calc, commands are slightly different than those used in this tutorial (for example, semicolons replace commas in formulas).

Where to find these items:

1) I'm currently using QGIS 2.18 for my maps, which is available for download here.
2) There are many different sources for shapefiles. Census' Tigerline Shapefiles are available here. As jimrtex states below, the Tigerline files are not simplified, which leads to wider lines.
3) [To Come]
4) Open Office is avalable for download here. You have to buy Microsoft Excel.

-------------------------------------------------------------------------------------------------------------

There are a number of ways to make maps. The easiest, most professional method is probably jimrtex's method, detailed in Reply #7 and replicated with pictures below, with modifications suggested by @PrdNewEnglander on Twitter. That does not involve manipulating the .dbf files. My method, also detailed below, requires Open Office Calc and manipulating .dbf files.

For both methods in the first half of this tutorial, we'll use the following:
1) Census' 2016 1:5,000,000 County Shapefile (.zip) You'll need to unzip this file before using it. Please put it somewhere where you can find it!
2) countychange.xlsx, County Estimate data downloaded from the Census Bureau's American Factfinder website as a .csv file and converted to an Excel file for your convenience.
3) QGIS
4) Open Office (cinyc Method 2 only)
5) The Atlas 5 Percent Heatmap Color Scale, 5 PCT Gradation Heat with Zero.qml
------------------------------------------------------------------
jimrtex Method 1:

This is what jimrtex did to set up this excerise: He got a (.csv) file from American Fact Finder with the 2016 county estimates. He converted (saved) this as an .xlsx file (countychange.xlsx), which we will use as the working file.

Step (1a): Add four columns:

M2 = TEXT(B2,"00000")
N2 = E2             ! Population April 2010 Base
O2 = L2             ! Population July 2016 Estimate
P2 = O2/N2 - 1   ! Relative change 2010 to 2016

Copy the formula down the columns.

Row 1 of Columns M-P is blank. In Row 1 of those columns, add column titles:

ID, POP10, POP16, INCREASE

When this step is complete, your spreadsheet should look something like this:



(1b) Save your Excel Spreadsheet by hitting CTRL-S or clicking on File-->Save.

(1c) Converting the new data to a separate CSV file is one method of importing the data into QGIS. It allows you to define the type of data in each column with specificity. This is described in Step 2 below. If you don't want to do this, skip to Step 3.

(2a) Change the format of column T values to % with two decimal places. (If you're new to Excel, you can do this a number of ways. One way is to left click to select column T, right click, select "Format Cells", select "Percentage" from the dialog, then hit "OK" on the bottom of the dialog.)

(2b) Create a blank spread sheet with four columns. (If you're new to Excel, you can do this by either typing CTRL-N or hitting Select File-->New-->Blank Worksheet.)

Copy columns M,N,O,P from countychange.xlsx (You can Left Click and hold to select columns M-P and hit CTRL-C or Right Click and select Copy from the drop down menu).  Paste VALUES to columns A, B, C, D of new spreadsheet. This loses the formatting of the increase column, but that is OK. (As you can see below, you can paste values by right clicking and hitting the highlighted clipboard with 123 or clicking on Paste Special from the drop-down menu, then selecting "Values" and hitting OK on the next prompt):



Row 1 is blank. In Row 1, add column titles:

ID, POP10, POP16, INCREASE

(2c) Save new spreadsheet as .csv file countychange.csv. (Type CTRL-S or select File-->Save As, then type "countychange" and select CSV from the drop-down menu).

(2d) Create a file with the type of .csvt countychange.csvt.

This is a text file that can be created in Notepad, that provides the format of each field of the .csv file. The contents of the .csvt file should be:

"String","Integer","Integer","Real"

Next, select File-->Save As. As you can see in the image below, you first need to change the "Save As Type" from "Text Documents" to "All Files" by clicking on the down arrow near red circle 1. Then type "countychange.csvt" as the filename, and click on "Save":



If you have difficulty creating the .csvt file, you can always create it as a .txt file and rename it in File Explorer. The path and name of the .csv and .csvt file must match, so make sure to save it in the same folder as countychange.csv.

We've now prepared the data to be merged with Census' County Shapefile.  Next, open QGIS:

(3) Create a new project using the blank page icon in the upper left.
(4) Drag the cb_2016_us_county_5m.shp file into the map area or click on the dotted V icon on the left of the screen or hit CTRL-Shift-V and browse for that file.
(5) Save the project as, using the diskette icon in the upper left. Make sure you select a directory you can find. This will create a .qgs file which is your project context. Be sure to save this from time to time. If you exit QGIS (or worse it crashes), you can get back to where you were at.

The screenshot shows where the icons described in Steps 3-5 are and what your map should look like, in case you're lost. If you don't see these icons, you need to make sure that you're not hiding the correct toolbars:



Since you now have a map, we should probably show how to move around, select, and set the projection. But for now I will skip to the join.

If you didn't create a .csv file:
(6a) Open countychange.xlsx as a vecrtor layer You can click on Layer --> Add Layer --> Add Vector Layer; but it is far easier to click on the V icon in the far left of the screen.
(6b) Browse for the .xlsx file. Select "All Files" or "MSL Office Open XML Spreadsheet" from the drop down menu on the Browse dialog. Otherwise, you might not be able to find your spreadsheet.
(6c) Hit Open.

If you're lost, these steps are annotated in the picture below:


If you created a .csv file:
(7a) Open the .csv file as a layer. You can click on Layers --> Add Layer --> Add Delimited Text Layer; but it is far easier to click on the Comma icon in the far left of the screen.
(7b) Browse to the .csv file.
(7c) Click on the No Geometry radio button (near the mid-bottom right)
(7d) Click on the OK button.

See the annotated image below if you're lost:


(8) Join the data layer to the county layer:

(8a) Right Click on cb_2016_us_county layer, and select Properties
(8b) Select Joins
(8c) Select Green Plus Sign to add Join
(8d) Select layer to join (the csv or xlsx layer)
(8e) Specify join field (ID is the name we gave to the first field in the csv file and a later field in the xlsx file)
(8f) Specify the target field in the county layer (GEOID)
(8g) Click on OK

This completes the join. Again, see the image below, if you're lost:



(9) Verify Join.
(9a) Make sure county layer is current layer.
(9b) Click on light blue icon that looks like Vermont or Connecticut mortgage card in Monopoly.
(9c) The new fields should be shown.

If you're not familiar with Monopoly, the mortgage card is the icon below the B in the picture below:



Note that it is quite easy to update/make corrections/add fields.

(10) Select csv layer, right click and Remove, and confirm removal. This will eliminate joined fields.
(11) In xlsx file add new column U2 = S2-R2   ! Numeric change 2016-2010
(12) Copy and paste values from xlsx column U to csv column E. Add name of column in E1 (CHANGE)
(13) Save CSV file.
(14) Add field type to csvt file ,"Integer"

(15) Redo Join.

If you've used this method, skip to Map Coloring Instructions in Reply #15 below for info on how to color the counties.
------------------------------------------------------------------
I'm running out of characters in this post. For the cinyc Method 2, click here for Reply #14.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #1 on: October 31, 2017, 07:52:03 AM »

Some posters have asked me for a tutorial on how I make my election maps without tediously using paint. I've taught a few others before, so I thought I'd put up a post here.

You need a few things to make maps:

Quote
You must be logged in to read this quote.

Excel can open .dbf files, it has disabled writing/updating .dbf files. Microsoft claims that there is something fundamentally flawed with the format, such that they can't reliably produce .dbf files. dBase might have at one time been considered a competitor with Office. dbf files exist because that was what ESRI had available when they invented shapefiles. You really don't need to access the .dbf files directly.

Yes, there is a way merge the data using QGIS, but I always run into the leading zero issue. For some reason, QGIS tends to read the states with a GEOID of 01-09 as a number, mismatching those states (in other words, 01001 becomes 1001, and the data doesn’t merge). Do you have an easy fix for that?

Quote
You must be logged in to read this quote.
For this application I would use the cartographic boundary files. These have been simplified. The basic files have too much detail, and crooked boundaries such as rivers may be rendered as thicker lines.

2016 1:5,000,000 County Shapefile (.zip)[/quote]

Fair point.

Quote
You must be logged in to read this quote.
How was the second column generated?


[/quote]

The second column is random.  I assigned a number from 1 to 3233 to each county, in the order in which Census’ shapefile laid them out, then divided by 3233. That’s why if the =sumif function is done correctly, the second column continuously increases from 0 to 1, but the county colors vary.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #2 on: October 31, 2017, 11:26:15 PM »
« Edited: October 31, 2017, 11:42:35 PM by cinyc »

Census ID's are both strings and numbers, but can be manipulated as numbers. The county ID is composed of a State ID and a County ID within a state. Harris County, Texas is "48201", where "48" is the state code for Texas, and "201" is the code for

The DBF file keeps the field type as string. The csv files produced by the Census Bureau for American Fact File does not have data types. You can think of a csv file as a crude form of data base that does not include metadata such as data types. When you open a csv file into Excel it determines data type by looking at the data values. 48201 and 01001 look like numbers, and stored as 48201 and 1001, and displayed as numbers (right justified by default).

There is another problem with leading zeros in GQIS, because of its unixy/c background. A number with a leading 0 denotes an octal number:  030 = 24 is TRUE.

Anyhow what we need to do is store the ID as a string, which is easy enough by using the expression
=TEXT(A1,"00000") which converts a number to a string with at least 5 digits including leading zeros if necessary.

I just tried that and still end up with NULLs in the 01-09 states' counties when trying to join the =Text column with the GEOIDs from the original .dbf in QGIS.  Is this a limitation in QGIS, or am I doing something wrong?

Edit: I think I figured out a fix by virtually creating another GEOID column, GEOIDFIXED, defined as a text column, using the following formula, then joining GEOIDFIXED and the GEOID column from the .dbf file:

if(length("GEOID,C,5")=4,concat('0',"GEOID,C,5"),"GEOID,C,5")

This seems almost as complicated to explain as just manually manipulating the .dbf file, though - especially when we get to GEOIDs that are longer than 5 characters.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #3 on: November 01, 2017, 12:24:06 PM »

Did you create a layer for the .csv file?

Yes, I created a layer for the .csv file - but I didn’t know to create a .csvt file. I’ll try that when I get to my computer tonight to confirm it works. I’m sure it does.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #4 on: November 01, 2017, 07:05:15 PM »
« Edited: November 01, 2017, 07:07:09 PM by cinyc »

As expected, I got the join to work with a .csvt file.

Do you mind if I put the relevant parts of Reply #7 in the first thread as the first of methods to make maps (the other being mine)? I'll add screenshots to illustrate.

Once you're done with your basic overview of QGIS, I'd like to add responses on how to calculate the WINCODE, how to use the Print Composer to make professional-looking maps, and (perhaps) how to make map gifs. (It doesn't seem like we have much of an audience for this How To, though).
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #5 on: November 01, 2017, 10:11:14 PM »
« Edited: November 02, 2017, 07:50:18 AM by cinyc »

The cinyc Method 2:

1) Open up the unzipped cb_2016_us_county_5m.dbf file in Open Office Calc.

2) Copy the GEOID column E from the opened .dbf file. To do this, click on E and either hit CTRL-C or Right Click and select "Copy", as you can see in the image below:


3)  Open countychange.xlsx in Excel. Create a new sheet by clicking on the plus sign at the bottom of the page. Paste the column from Open Office to Column A of that new sheet. To do this, go to Cell A1 on Sheet1 and hit CTRL-V.

4) Click on the Data sheet. Add four columns to it:

Q2 = TEXT(B2,"00000")
R2 = E2             ! Population April 2010 Base
S2 = L2            ! Population July 2016 Estimate
T2 = S2/R2 - 1   ! Relative change 2010 to 2016

5) Switch back to Sheet1. Note that the GEOIDs from the test data and GeoIDs from Census' Shapefile are in a different order. We have to rectify this. I use a =sumif formula to get the color codes in Census' order.

-Type the following formula into Cell B2 on Sheet2: =sumif(Data!$Q$2:$Q$3221,$A2,Data!R$2:R$3221)
-Copy that formula to Cells C2 and D2.
-Copy that formula into Cells B3-B3234 on Sheet2. Hit CTRL-C after selecting cells B2-D2, then LEFT CLICK and scroll down from Cells B3 to B3234 (there's a shortcut to this - let me know if you want to know it). Note: The dollar signs in the formula are crucial to making sure the same formula gets cut and pasted in each row.
-Type "POP10,N,19,0","POP16,N,19,0","INCREASE,N,19,15" into Cells B1,C1 and D1 to give the columns a name.
 
6) Now it's time to cut and paste the new Color Code Column from Sheet1 into a new row in the .dbf file of Open Office Calc.
-Left Click and hold on the "B","C" and "D" at the top of Columns B-D to select Columns B-D in Excel and then press CTRL-C (or Right Click and Select Copy).
-Switch to Open Office Calc.  Select cell J1. RIGHT CLICK, then select "Paste Special-->Unformatted Text-->OK"

7) Save the .dbf file in Open Office Calc (CTRL-S or hit the diskette on the toolbar) and Excel. Open Office will warn you that some data might not be able to be saved in dbf format, and suggest another format. Select "Keep Current Format". Excel can't save in .dbf format - that's why you need Open Office. Also, save the .xslx file in Excel.

8) Close Open Office Calc.

Now, we're ready to open QGIS:

9) Open cb_2016_us_county_5m.shp in QGIS. You can probably do this by clicking on the file, or dragging the file to the big window on QGIS. If not, open QGIS, and either hit CTRL-Shift-V or select the V symbol on the left side of the screen, or select Layer-->Add Layer-->Add Vector Layer from the drop-down menu.

Hit "Browse", and find cb_2016_us_county_5m.shp on your computer.

Your screen should now look something like this:


10) Save the project as, using the diskette icon in the upper left. Make sure you select a directory you can find. This will create a .qgs file which is your project context. Be sure to save this from time to time. If you exit QGIS (or worse it crashes), you can get back to where you were at.
--------------------------------------------------------------------------
The next reply includes instructions on how to color the counties.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #6 on: November 01, 2017, 11:27:02 PM »

Map Coloring Instructions:

Note: This assumes you have cb_2016_us_county_5m.shp open in QGIS. If not, see the instructions for the method you used to learn how to do that.

1) To color the map:
(1a) RIGHT CLICK on cb_2016_us_county_5m in the Layer Panel and select "Properties" from the drop down map, as you can see in the image below:



(1b) The Layer Properties Dialog will appear. Select "Style" on the Left, then Style-->Load Style on the bottom left, as you can see in the picture below:



(1c) Load 5 PCT Gradation Heat with Zero.qml from where you saved it on your computer. The color scheme should load. The default variable isn't INCREASE. Change this to INCREASE by clicking on the down arrow next to PCT_FI_C60, selecting INCREASE and hitting OK on the bottom of the dialog, as you can see in the picture below:



(2) Congratulations! Your map is now colored in various shades of red and blue! It should look like this:



Unfortunately, your map's not perfect, and you might not know how to use QGIS to zoom in or whatever. jimrtex and I am working on more tutorials to tell you how to do that.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #7 on: November 02, 2017, 08:58:05 AM »

I found out on Twitter that we needn't even do the .csv step. QGIS can export an Excel .xlsx file as a vector file. That seems to import the ID column directly, as a text file.

jimrtex - do you know of any drawbacks to this? If not, I'll further simplify the preferred method instuctions if I have time tonight.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #8 on: November 02, 2017, 04:48:05 PM »
« Edited: November 02, 2017, 08:21:20 PM by cinyc »

QGIS uses a lot of Python plugins, for things like file import/export. I did not know that you could use xlsx files. I did some googling, and there was a tutorial about importing a lay from a csv file, which then mentioned that you could use an xls file, but that it would not import expressions. But I just tried it with an xlsx file and it did import the values of expressions.

Neither did I, until I posted a link to this thread on Twitter and @PrdNewEnglander read this and told me we were making things more complicated than necessary - just import the Excel spreadsheet. I did a very quick test this morning, and seemed to get it to work, properly importing the ID column as a text column. I really should try it with one of my more complicated spreadsheets to make sure, though.

Quote
You must be logged in to read this quote.

The only real reason I developed my method was because I never really knew how to join sheets until your Hudson tutorial, and ran into the leading zero issue when I otherwise tried it. It does have its benefits - I often use more than one map making program/website, and don’t have to do multiple joins across platforms. For example, I can just import the zipped shapefile & .dbf to Carto.com without having to use their join function, which sometimes misreads the column types and imports the GEOIDs as numbers or numbers as text. The data and data type is permanently in the .dbf file.

I’m going to try to update the first post tonight to reflect the possibility of just using the Excel file.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #9 on: November 02, 2017, 10:03:42 PM »
« Edited: November 03, 2017, 02:30:27 PM by cinyc »

Adding Labels to Your Map

Now that you know how to color, pan and zoom your map, you're ready add county labels. Since these would largely be unreadable on a map of the Continental United States, let's use your newly-acquired Projection, Pan and Zoom skills to focus on Alaska.

(1) Zoom to full map extents by hitting the magnifying glass with three blue arrows.

Notice how Alaska is weirdly tilted on this map? Before making a map of Alaska, we need to change the projection to something more Alaska-specific.

(2) Click on the globe icon in the bottom right-hand corner of the screen. That will launch the project properties dialog.
(3) Type "Alaska Albers" in the box.
(4) Select "Alaska_Albers_Equal_Area_Conic" from the list.
(5) Hit okay.

Now, Alaska looks more like most maps of Alaska.

Here's an image detailing these three steps:


(6) Hit the Zoom In magnifying glass, and draw a box around Alaska. Don't worry if you don't get all of the Aleutian Islands in - most are uninhabited anyway. Pan and Zoom in or out of your map until it looks something like this (assuming you've already colored your map):



Now, we're ready to add labels to the map.

(7a) Right click on cb_2016_us_county_5m in the layer panel.
(7b) Select properties from the drop-down menu.
(7c) Select "Labels" on the left side of the dialog.
(7d) Select "Show labels for this layer" from the top menu.

As you can see, the label dialog has 7 categories of options. I will briefly describe the most salient of them below. Here's a diagram, in case you're lost:



Text - This allows you to change the font, font size, color and transparency. I usually change the style to Bold, but YMMV.

(7e) Click on Style and change it from Normal to Bold from the drop-down menu.

Formatting - This allows you to wrap your text on a character, set a left, right or center alignment, and format numbers.  

(7f) Change the Alignment to Center. Click the box next to formatted numbers, and enter 2 in the Decimal Places box.

Buffer - this draws a text buffer around your text. I think this is a good idea to use with the Atlas color scheme because black text is too dark on dark colors and white text is too light to be seen on its light colors.

(7g) Check the Draw text buffer box.

Background - this allows you to draw a background behind the text. This also helps with visibility, but probably isn't necessary here, since we have a buffer. Skip.

Shadow - this allows you to draw a drop shadow behind your text. This isn't necessary here. Skip.

Placement - this allows you to dictate where your labels are placed - offset from centroid, etc. It also allows you to define the priority of this layers' labels compared to other layers.

(7h) Click on Force point inside polygon.

Rendering - this allows you to, among other things, only show labels on certain zoom levels and show all labels, including colliding labels. Skip for now.

What do we want to include in the label? If we were going to be simple about it, just NAME. But there is a way to include multiple things in your label.

(7i) Click on the curved E to the right of the label with dialog.
(7j) Type the following formula:

"NAME" || '\n' || concat(format_number("INCREASE"*100,2),'%')

'\n' places the increase text on the next line, instead of next to name. The concat function allows you to put a percentage mark after the increase. The format_number function allows you to limit the Increase percentage to 2 decimal places.

(7k) Hit OK to save the formula.
(7l) Hit OK to close the Layer Properties Dialog.

Congratulations! You've added labels to your map! Your map should now look something like this:



Again, remember to save your project.

If you want to print a simple image of your map, you can select Project—> Save an Image which creates a png file of the map window. But if you want to add things like a legend and descriptive text or want to make multiple maps with the same map view, it’s probably best to use QGIS’ print composer. How to do that is described in the next post.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #10 on: November 03, 2017, 12:11:35 AM »
« Edited: November 07, 2017, 12:49:25 AM by cinyc »

Using the QGIS Print Composer

We're ready to export an image of this map using the print composer. Before we do that, we should probably add a shapefile of Canada. Otherwise, when we color the background, Alaska will look like an island.

Luckily, Statistics Canada's shapefile website has an available provincial shapefile here. Download this file and unzip it.

(1) Add gpr_000b11a_e.shp to your map by dragging the shapefile to the map screen, clicking on the V icon on the left of the screen, or hitting CTRL-Shift-V. Note: if you last opened the .xmlx file, you might have to change File Type back to ESRI Shapefiles to find it.

The names of these two shapefiles are confusing. You can rename them in the layer panel by right clicking and selecting Rename from the drop-down menu. This will also help later when we add a legend to the map.

(2a) Rename gpr_000b11a_e "Canada".
(2b) Rename cb_2016_us_county_5m "County".
Note: This doesn't change the underlying file name, just the name in the Layer Panel. The layer name is used when the print composer auto-makes a legend - though you could separately rename the shapefile name only in the print composer, if you wish.

What color do we want to make Canada? That's totally up to you - and you can easily change it:
(3a) Right click on Canada in the layer panel, and select "Properties" from the drop-down menu.
(3b) Click Style.
(3c) Click on Simple Fill.

You should see a menu with 7 options:



-Fill allows you to change the background color. Clicking on the arrow brings up a simple dialog with a color wheel and simple and recent colors. Clicking on the bar brings up a more complex dialog that allows you to input an HTML code for your color choice.

(3d) Choose whatever color you want. I usually use a dark grey for foreign countries, HTML notation #616161.  The opacity slider allows you to make the layer more or less transparent. It isn't necessary to adjust it here.

-Outline allows you to choose the outline color - here - provincial and country borders.

(3e) Click on the bar or arrow and choose a color. I usually use red, HTML notation #ca0020. If you don't want to show provincial borders, use the same color you used for fill.

-Fill style allows you to decide how to fill a polygon. Solid is probably fine here.

-Outline style gives you options for the outline - solid line, dash, etc. Solid line is probably fine here.

-I'm not sure exactly what Join Style does. Skip.

-Outline width allows you to set the width of your outline. I tend to make state and country borders wider than county borders.

(3f) To do that, hit the up arrow until Outline Width is 0.66000 millimeters.

-Offset allows you to offset your symbols by a specified length. That's not necessary for a solid fill (but might be for points). Skip.

(3g) Hit Okay.

Canada's colo(u)rs should have changed to your specifications.

Save your project.

We're now finally ready to use the print composer.

(4a) Select Project-->Print Composer or type CTRL-P.

A dialog will pop up asking for a unique name. Name it Alaska. The print composer will pop up. It looks like this:



I'm not going to explain what all of the icons do - just the ones typically used to make a map. They are labeled B-F, in order of what I usually do to make my maps:

(4b-1) Click on the icon that looks like a scrolling piece of paper. This adds a map to the canvas. Draw a rectangle as large as the canvas.

You should now see Map 0 in the Items Panel, and tabs for Composition, Item properties, and Command history below that.

(4b-2) Click on the Item properties tab, if it's not already selected.
(4b-3) Under Extents, click on "Set to map canvas extent"

This will make your print composer map look like what the main QGIS screen looks like. The map is still probably not optimal - too much white space. You can manually fix this by playing around with the X min, Y min, X max and Y max values under Extents. You can play around with them until you like your map.

(4b-4) Enter the following values:
X min: -1500000
Y min: 300000
X max: 1750000
Y max: 2500000

Next, we should set the blank space to the color of water. To do that:
(4b-5) Scroll down and check the background box.
(4b-6) Expand the background section and click on background color.
(4b-7) Select whatever color you want. I use HTML notation #a6cee3 for water
(4b-8) Hit OK.

The water should now be colored blue.

Note that the map is now smaller than the canvas. To rectify this:

(4b-9) Click on the Composition tab.
(4b-10) Under "Resize page to content", hit the Resize page button.

While we're here, we might as well lower the export resolution. 100 dpi is usually sufficient. Anything larger might be too large to be uploaded to the Atlas Gallery.

(4b-11) Type 100 in the Export resolution box.

We're now ready to add a legend to the map:

(4c-1) Click on the icon with Yellow, Red, Green squares on the left side of the screen.
(4c-2) Draw a box on the upper right hand side of the map. A legend should be auto-created.
(4c-3) Select Legend in the Item Pane.
(4c-4) Select Item properties.

Legends can be tailored to only include items on the map screen.

(4c-5) Open the Legend items menu if it isn't already.
(4c-6) Hit the funnel icon underneath the list of shapefiles.
(4c-7) Click on the "Only show items inside current atlas feature" box.

If you've done this correctly, the legend should be truncated to about 7 items, and if you update the map extents in the print composer or change the variable in QGIS, the legend should automatically change to reflect what's shown on the map.

(4c-8) Scroll down to Position and size. Expand the menu, if necessary.
(4c-9) Click on the upper right box next to "Reference point".
(4c-10) Move your cursor back to the map. Click on and drag the Legend to the upper right hand corner of the map. You should see red guidelines on the canvas when you get there.

Next, we can add a scale bar (optional):

(4d-1) Click on the black and white bar icon on the left side of the print composer.
(4d-2) Create a box on the lower right hand corner of the map.
(4d-3) Select <scale bar> in the Item Pane
(4d-4) Select Item properties below (if necessary)
(4d-5) This is America. We don't need no stinking metric system. Under units, change "Meters" to "Feet".
(4d-6) Under Segments, make the bar as long or short as you want. Using the down arrow, change left to 0.
(4d-7) Decide whether you like a short or tall bar. I like short bars, so I lower the height to 1.00 mm.
(4d-8) Under Position and size, check the lower right hand box next to Reference point.
(4d-9) On the map, drag the scale bar to the lower right hand corner. You can manipulate the box by making it larger or smaller to move it closer to the bottom of the map.

Finally, we can add a text description of the map anywhere on the canvas:

(4e-1) Click on the icon of a T in a rectangle on the left hand side of the screen.
(4e-2) Draw a rectangle in the upper left hand corner of the screen.
(4e-3) Highlight QGIS in the Item Pane.
(4e-4) Select Item properties below (if necessary)
(4e-5) Type the title of this map in the box under Main properties: Relative Population Change in Alaska County Equivalents, 2010-16
(4e-6) Click the Font Button
(4e-7) Change the font, type and size however you want. I've used MS Shell Dig, Bold, 24 pt type.
(4e-8) Hit OK.
(4e-9) Move the text box to the upper left hand corner, and drag the edges to fit in all the text, if necessary.

Finally, we can export the map as an image:
(4f-1) Click on the icon next to the printer on the top toolbar (it looks like a piece of paper with a mountain on it).
(4f-2) Type a file name for your picture, and hit Save.
(4f-3) A dialog should pop up. Make sure the Export Resolution is set at 100 dpi and check the box to Crop to content. Hit save.
(4f-4) Open your image. It should look something like this:



Congratulations! You have successfully used the print composer to make a map! Assuming you have enough posts here, you can now upload your map to the Atlas Gallery and share it with us!

As always, remember to save your project. This is especially important now because your print composer will be saved as part of your project. That allows you to easily make multiple maps with the same projection and coordinates simply by changing the variable mapped. It's how I make my gifs.

Note: There are a couple of issues with some labels not showing up on the map. We could force all labels to be printed by checking the appropriate box in the label dialog. I'm still not 100% sure how to manually apply label locations to avoid potential collisions, though. Perhaps jimrtex does.

Next from me: Calculating WINCODEs.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #11 on: November 03, 2017, 10:05:25 AM »
« Edited: November 03, 2017, 02:32:37 PM by cinyc »

After you added labels, I would do the simple way to create a map: Project > Save an Image which creates a png file of the map window. That is what I always use (my use of QGIS is more as an interactive analysis tool).

I tried to use the Print Composer for Hudson, and could never get the Bing Street Maps to line up with the Census Shapefiles. If you use the Census Shapefiles for street features, each street fragment (a block line) is a feature. So you end up with a lot of labeled streets. I merged street features, but it was a tedious process.

I’ll put that in.

My main problem with printing directly from QGIS is that it’s easy to unintentionally move the map in the map window, or change the zoom level, etc. This makes it more difficult to make gifs, where you need the same view of all pictures or the map will jump around. Yes, you can always just write down the extents and zoom level so that you can manually reset, save the project as a different name and reload when you unintentionally move the map or show the use previous view magnifying glass to get back to the old view (if you can remember which it was, anyway), but why bother when the Print Composer doesn’t change extents unless you force it to?

It’s also good that you can easily add a background and legend using the print composer. Otherwise, I’d have to do that manually for every map using Snagit or Paint, anyway, so I actually save time when I make multiple maps by using the print composer. The legend funnel feature is also excellent, as it gets rid of colors in the scale that are not used that portion of the map, making the legend shorter.

But again, YMMV.

Quote
You must be logged in to read this quote.

Yeah, we should probably add that in another tutorial. Lines can get annoying.

Is there a simplified state map somewhere on Census’ website, like there is for counties? I generally only have the tigerline files on my computer.

Quote
You must be logged in to read this quote.

I probably didn’t want to include the percentage change in my map, either - but I wanted to show how to make a multiple-line label (which even I didn’t know how to do until I reread your Hudson GIS tutorial). The multiple-line label is probably what is causing Fairbanks and Ketchikan to disappear when we have the label collision avoidance on. Bristol Bay is going to always be off unless you use a method to assign it or Lake and Peninsula Borough separately. It’s too small and too close to the centroid of Lake & Peninsula Borough.

I’ll add a tutorial on rules-based labeling eventually.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #12 on: November 04, 2017, 12:20:07 AM »
« Edited: November 07, 2017, 12:42:17 AM by cinyc »

Calculating Wincodes

In this tutorial we will show how to create maps of precinct-level election results. Each precinct will be colored to show the winning party, and their voting strength. For example, using the Atlas color scheme, an inner-city precinct might be shown in a deep red (signifying a big Democratic victory), while a suburban precinct won by a plurality in a three-way race might be shown as light pink or light blue.

For this tutorial we need a precinct shapefile and the election data in a tabular form in a spreadsheet. The precinct shapefile for Alaska is here.

In Part 1 of this tutorial we will prepare the election data in a spreadsheet. Since Part 1 is not about QGIS per se, you may skip to Part 2 and begin with the spreadsheet prepared in Part 1.

Calculating Wincodes - Part 1: Creating Tabular Election Data for Use With QGIS.

Our xlsx file will have one row per precinct. Columns will have the precinct name or identification, as well as the votes cast for each candidate in that precinct. We begin with the election results from the Alaska Division of Elections, focusing on the 2014 general election precinct election results. Unfortunately, the Alaska Division of Elections' results aren't in that format and require processing.

(1) Download the 2014 TEXT by Precinct results from the state of Alaska's elections result website by RIGHT clicking on the link to it on Alaska's 2014 results page (i.e. not here) and selecting Save Link As. Otherwise, this will likely open up in your browser. You can save it as a .txt or .csv file. (Yes, Alaska's format is sub-optimal, but at least Alaska offers a TXT file for those years instead of just a PDF that requires even more processing to get into something Excel readable).

(2) Open up the precinct-results.txt. To do this if Excel is not your default application to open .txt files, open up Excel, then click on File-->Open-->Browse. A dialog window should open up. Make sure to change All Excel Files in the box next to Filename to All Files or All Text Files, if that's not selected already. Otherwise, you won't find your file.

(3) The Text Import Wizard should open up.
(3a) Select the radio button (circle) next to Delimited. Hit Next.
(3b) On the next screen, click the box next to Comma. Unclick any other boxes that might be checked. Hit next.
(3c) On the final screen, General format is okay for this exercise. Hit Finish.

Your data should now be imported into Excel.

(4) Rename this sheet Data (Double Click on the tab at the bottom of the screen, delete the text there and type "Data")

Next, we'll copy the precinct numbers to a new sheet and delete duplicates so that we end up with a list of unique precincts.
(5a) Copy all of row A by clicking on the column header and copying the text however you do that (I usually hit CTRL-C).
(5b) Click on the + icon to create a new sheet.
(5c) Paste the contents of Column A into Cell A1 on new Sheet1. This can be done by hitting CTRL-V.
(5d) On the top ribbon, select Data-->Remove Duplicates. Hit OK. Duplicates have been removed. See the image below for the location of "Data" and "Remove Duplicates", next to the red circle 5.
(5e) Insert a new row 1. This can be done by clicking on the row number (1), then RIGHT clicking and selecting Insert.
(5f) Type "Precinct Name" in cell A1.



Note: In Alaska, early, absentee, and questioned votes are not attributed to the election precinct of the voter, but rather to the legislative district. These votes represented about 30% of the total vote in 2014. We will not be able to display these results on our precinct-level maps, but we will retain them for use in future analysis (or tutorials).

(6) We will need to separate the precinct number from precinct name to match the data in the Alaska precinct shapefile. Luckily, all precinct IDs are 6 characters.
(6a) In Cell B2, Type "Precinct ID"
(6b) In cell B2, Type "=LEFT(", scroll over to Cell A2, and then type ",6" - or just type =LEFT(A2,6)
(6c) Copy that formula to the bottom of the data.

There are a number of ways to do this. One way is just to hit CTRL-C, scroll down until you hit the end of the data in Column A, and hit CTRL-V. This might work well when you only have a few hundred cells to paste data into, but not so much when there are thousands or tens of thousands of cells.

One Excel shortcut is to hit CTRL-End to get to the end of your worksheet, click on B562 (i.e. last row) and then SHIFT-CTRL-UP ARROW to select the entire column followed by CTRL-D. If you know the last row number, you can also do a GO TO (type CTRL-G, then B2:B562) and hit CTRL-D. Another way to do this is Hit CTRL-C to copy cell B2. Go to neighboring Column A and Press CTRL-Down Arrow enough times to get to the bottom of the entries in that column with text. Once at the true bottom, use the Right Arrow to scroll back to the cell on that line in blank Column B. The first box on the Function toolbar should now say B562. In that box (below the red circle 6 on the image above), type "B2:" before the B562 and hit enter. That should create a box from B2 to B562. Hit CTRL-V.

Next, we'll calculate the number of votes each candidate in the 2014 Senate, House, and Governor's race received in a format that can be more easily joined with the shapefiles:

(7a) Type the following headers in Cells C1-P1

Begich, Mark    Fish, Mark S.    Gianoutsos, Ted    Sullivan, Dan    Write-in 70    Dunbar, Forrest    McDermott, Jim C.    Young, Don    Write-in 50    Clift/Lee    Myers/Rensel    Parnell/Sullivan    Walker/Mallott    Write-in

I got these by cutting and Pasting Special TRANSPOSE (Right Click T or hit the Clipboard with round arrow) the the names of the Senate, House and Gubernatorial Candidates from the prior spreadsheet. You can, too. Exact names make it easier to do the =sumifs function described below.

(7b) Type the following formula into Cell C2:
=SUMIFS(Data!$F$1:$F$56184,Data!$C$1:$C$56184,C$1,Data!$A$1:$A$56184,$A2,Data!$B$1:$B$56184,Data!$B$3)

Formula explanation:
-Data! Column F is the number of votes that need to be summed. That goes first in a =SUMIFS function, but last in a simple =SUMIF function.
-Data! Column C is the Candidate Name. It gets compared to the candidate name in the header row of this sheet.
-Data! Column A is the Precinct Name. If gets compared to the Precinct Name in Column A of this sheet.
-Data! Column B is the office name. It gets compared to the office name the first time it appears on Data! B for that race. We're going to have to change the last variable when we cut and paste it for the House and Governor's Race. I'd go with two header rows to include this variable, but that might mess up the join in QGIS. And typing the name isn't necessary.

(7c) Copy this formula to Cells D2-H2. (Hit CTRL-R in D2-H2 or CTRL-C in Cell C2, followed by CTRL-V in D2-H2)
(7d) Change "Data!$B$3" to "Data!$B$12" in H2.
(7e) Copy Cell H2 to Cells I2-L2.
(7f) Change "Data!$B$12" to "Data!$B$20" in L2.
(7g) Copy Cell L2 to Cells M2-P2.
(7f) Copy cells B2-O2 to row 562.

After a few seconds, the data should update. At this time, it's probably prudent to make sure the precinct results matched the actual results.

(8a) Type "Total" in Cell A564.
(8b) Type =SUM(B2:B564) in Cell C564.
(8c) Copy the formula in C564 to Cells C564-P564.
(8d) Check the totals against the official total election results on Alaska's website. That's available here.

This step matters. When I attempted this the first time, I noticed the write-in totals were off for some races without adding the race name variable to the =SUMIFS function.

Assuming everything checks out correctly, we're ready to compute Wincodes. Before you do that:

(9a) In Cell A1, Select All (Hit CTRL-A).
(9b) Copy that data.
(9c) Open a new blank spreadsheet.
(9d) Paste VALUES in Cell A1 of the new blank spreadsheet.
(9e) Save this spreadsheet as "AK2014 Tutorial" in Excel format.

Or just click on the link to my AK2014 Tutorial.xlsx in the next post.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #13 on: November 04, 2017, 01:11:29 AM »
« Edited: July 16, 2020, 05:19:41 PM by cinyc »

Calculating Wincodes - Part 2: Calculating Wincodes in Excel

If you completed Part 1 on your own, you can use that file. Otherwise, you'll need to download the following Excel file for this part of the exercise:

AK2014 Tutorial.xlsx

The spreadsheet contains precinct-level results for the Alaska's Senate, House and Gubernatorial Race in the 2014 General Election. We need to manipulate the data to create Wincodes:

(1) Add the following titles in Cells Q1-V1:
Senate Total   House Total   Governor Total   Sen Wincode   House Wincode   Gov Wincode
(2) Type the following =SUM functions to add up the votes in each race:
(2a) Cell Q2:
=SUM(C2:G2) | Sum of Votes in Senate Race
(2b) Cell R2:
=SUM(H2:K2) | Sum of Votes in House Race
(2c) Cell S2:
=SUM(L2:P2) | Sum of Votes in Governor's Race

(3) Next, we'll calculate the Wincode for each race. A Wincode is basically the winner's percentage, increased by the integer necessary to color the precinct the proper shade. For General Elections on Atlas, Republicans are usually blue, Democrats red, Libertarians yellow and Independents or Green party candidates green. In my 7 color style (which is actually 8 colors), for example, blue is in the 0s, green is in the 1s and red is in the 2s. Thus, the general formulas are as follows:

Republican     | PCT
Ind/Green      | PCT +1
Democrat       | PCT +2
Libertarian     | PCT +3
Others           | PCT +4,5,6 or 7.
Tie                | 9
No Data         | 10
Error             | -1 or 11, depending on the color scheme

Ties are generally represented by light gray, no data by dark gray, and errors by white.

Thus, you need to know which candidates are Republicans, which are Democrats, and which are others when creating your Wincode formula. Depending on how the elections officials placed the candidates in the results, they are unique to each race.

I calculate Wincodes using a nested IF function. Here, we haven't precalculated the percentage for each candidate. We can do that - but need not here. It's a simple formula - ={Cell of candidate votes}/{Cell of total votes}.

Here are the formulas for the wincodes. Type (or cut and paste) the following formulas:

(3a) In Cell T2 (Senate Wincode):
=IF(Q2=0,10,if(MAX(C2:G2)=LARGE(C2:G2,2),9,IF(F2=MAX(C2:G2),F2/Q2,IF(C2=MAX(C2:G2),C2/Q2+2,IF(D2=MAX(C2:G2),D2/Q2+3,IF(E2=MAX(C2:G2),E2/Q2+1,11))))))

Explanation of the formula:
- Since we're not worried about precincts not yet reporting in the final results (in which case, the first IF would be "IF(Q2=0,10" where Q2 is the total votes cast, checking for ties comes first. MAX(C2:G2)=LARGE(C2:G2,2),9 compares the largest number of votes received in the Senate race against the second highest number of votes received. If they are equal, it's a tie, and QGIS will color the geometry as light gray.
- Next, we check to see if the Republican candidate, Sullivan, received the most votes. If that's true, the wincode is just the Republican's percent, F2/Q2, which would give us a shade of blue in QGIS.
- Then, we check to see if the Democratic candidate, Begich, received the most votes. If that's true, the wincode is the Democrat's percent plus 2, C2/Q2+2, which would give us a shade of red in QGIS.
- Fourth, we check if the Libertarian candidate received the most votes. If that's true, the wincode is the Libertarian's percent plus 3, D2/Q2+3, which would give us a shade of yellow in QGIS.
- Fifth, we check if the Non-affiliated candidate received the most votes. If that's true, the wincode is that candidate's percent plus 1, E2/Q2+1, which would give us a shade of green in QGIS.
- Last, if none of these are true, the wincode is 11, which would give us a blank white precinct, telling us something's wrong - either Write-ins won or we screwed up.

(3b) In Cell U2 (House Wincode):
=IF(MAX(H2:K2)=LARGE(H2:K2,2),9,IF(J2=MAX(H2:K2),J2/R2,IF(H2=MAX(H2:K2),H2/R2+2,IF(I2=MAX(H2:K2),I2/R2+3,11))))

This formula is shorter because there were fewer candidates in the race.

(3c) So what should be the formula for the Governor's Wincode? Try to calculate this on your own. Here are the partisan affiliations of the candidates:
Clift/Lee           | Libertarian
Myers/Rensel    | Constitution
Parnell/Sullivan | Republican   
Walker/Mallott  | Main Independent

Note: There was no Democrat in the race.

The formula I used is below in white text. Highlight it if you want to know the answer, and cut and paste it to Cell V2 your spreadsheet (you might need to change the font color in Excel for that Cell to black if you cut and paste the formula):

=IF(MAX(L2:P2)=LARGE(L2:P2,2),9,IF(N2=MAX(L2:P2),N2/S2,IF(O2=MAX(L2:P2),O2/S2+1,IF(L2=MAX(L2:P2),L2/S2+3,IF(M2=MAX(L2:P2),M2/S2+4,11)))))

(4) Copy and paste cells P2-U2 to rows P3 to U443.
(5) Save your spreadsheet.

Now that you've calculated Wincodes, we're ready to move on to the next step: Drawing a multiple layer precinct election map in QGIS.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #14 on: November 04, 2017, 11:42:35 AM »
« Edited: November 07, 2017, 12:48:11 AM by cinyc »

Calculating Wincodes Part 3: Drawing a multiple layer precinct election map in QGIS.

Now that we have our 2014 Alaska election data in order, it's time to make maps with it. For this exercise, you'll need the following additional items:

Shapefiles:
(1) Alaska's Precinct Shapefile
(2) My shapefile of Alaska's Coastal Water Features, AlaskaBays.zip

These two items should be unzipped and put somewhere on your computer where you can find them. I have a separate folder for each state's GIS files, but I do a lot of mapping. Putting all your GIS files in one directory should suffice.

QGIS Style Files:
(1) Atlas 7Color Scale
(2) Water
(3) Boundary Yellow

And if you haven't completed steps 1 and 2, you'll need the following Excel File:
AK2014 Tutorial Full

Let's get started.

(1) Open QGIS.

(2) If you've saved your project from the last lesson, open it. Otherwise:
(2a) Open cb_2016_us_county_5m.shp
(2b) Open gpr_000b11a_e.shp
(2c) In the layer panel, rename cb_2016_us_county_5m "County"
Note: This doesn't change the underlying file name, just the name in the Layer Panel. The layer name is used when the print composer auto-makes a legend - though you could separately rename the shapefile name only in the print composer, if you wish.
(2d) Rename gpr_000b11a_e "Canada".
(2e) Change the projection to Alaska_Albers_Equal_Area_Conic
(2f) Select the Zoom tool from the toolbar and draw a box around Alaska. You needn't include all of the Aleutians.
If you don't know how to do any of these steps or need the shapefiles, read the prior lessons.

(3) Open 2013-SW-Precinct-Proc-Plan.shp, the Alaska precinct shapefile. Rename it "Precinct".

(4) Open AlaskaBays.zip. Rename it "Water".

Your screen should look something like this.



Let's start coloring the various layers from bottom up using styles:

(5a) Select County in the Layer Panel.
(5b) Right Click and select Properties.
(5c) Select Style from the menu on the left of the popup dialog if it isn't already selected.
(5d) Click on Style on the bottom of the dialog. Select "Load Style" and Load Boundary Yellow.qml.
(5e) Hit Okay.

Because County is on the bottom in the Layer Panel, the only change you should see is that the county labels are gone. In QGIS, layers at the top of the layer panel are the top layer on the map. In order to see the County lines:

(5f) Select and drag County to the top of the layer panel.

Your map should now look something like this:


We need to join the data to the Precinct shapefile before we can color it, so:

(6a) Add 2014AK Tutorial Full.xlsx (or whatever you called your file from Step 2 of the previous tutorial) as a Vector File. (Hit the V icon on the left of the screen. If your Excel file doesn't show up in the file browser, make sure All Files or MS Office Open XML Spreadsheet is selected in the drop-down menu next to filename.)
(6b) Select Precinct in the Layer Panel.
(6c) Right click on Precinct and select Properties.
(6d) Select Joins from the menu on the left of the popup dialog.
(6e) Hit the plus button on the bottom of the dialog to add a join.
(6f) Make sure the following are selected in the popup's pull-down menus:
Join Layer: AK2014 Tutorial Full Sheet1 None (or whatever the just-added data layer is called on your computer)
Join field: Precinct ID
Target field: District
(6g) Check the box next to Custom field name prefix and delete the text in it.
(6h) Hit OK.
(6i) Check to see that the Join worked by clicking on the Connecticut Avenue Monopoly Card icon and making sure there are numbers in all of the columns. Assuming the join was successful:
(6j) Select Precinct in the Layer Panel.
(6k) Right click on Precinct and select Properties.
(6l) Select Styles from the menu on the left of the popup dialog.
(6m) Click on Style on the bottom of the dialog. Select "Load Style" and Load 7ColorScale.qml.
(6n) Click on the drop-down menu next to column. Change WINCODE to Sen Wincode.
(6o) Hit OK.

Your map should now look something like this:



Finally, we need to change the water color, using the Water style. Try doing this on your own. If you're lost, all you need to do is the following:

(7a) Select Water in the Layer Panel.
(7b) Right click on Precinct and select Properties.
(7c) Select Styles from the menu on the left of the popup dialog.
(7d) Click on Style on the bottom of the dialog. Select "Load Style" and Load Water.qml.

Your map should look something like this:



Congratulations! You've made a precinct map of the 2014 Alaska Senate Election! Save your project (or save as under a different name, if you want to save each lesson separately).

If you want to print a simple image of your map, you can select Project—> Save an Image which creates a png file of the map window. Otherwise, please continue to the next part of this lesson, where I will show how easy and quick it is to make multiple professional-looking maps using QGIS' print composer.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #15 on: November 04, 2017, 12:11:34 PM »

Calculating Wincodes Part 4 - Using the QGIS Print Composer to make multiple maps

(1) Open QGIS and load the project from Step 3.

(2) Open the Print Composer you created in the Using the QGIS Print Composer Lesson. Assuming you named your last print composer Alaska, select Project-->Print Composers-->Alaska.

That will open up your print composer in a new window. The map may or may not have auto updated. To update the map:

(3) Highlight map0 in the Items Pane, select the Item Properties Tab below (if it's not open already) and hit "Update preview".

We need to change the title of the map:

(4a) Select the item that says "Relative Population Chang...." or "QGIS" in the item pane.
(4b) Under Main Properties, delete the text in the box and type "2014 U.S. Senate Results by Precinct"

The legend should have auto-updated. If not:

(5a) Select Legend in the item pane.
(5b) Under Legend Items, toggle the Auto update box on, off and on again.

(6) Print your map by hitting the mountain image icon on the top of the screen.

That's it.

Now, we can easily change the variable mapped.

(7) Open your main QGIS screen.

(8a) Select Precinct in the Layer Panel.
(8b) Right click on Precinct and select Properties.
(8c) Select Styles from the menu on the left of the popup dialog.
(8d) Click on the drop-down menu next to column. Change Senate Wincode to House Wincode.
(8e) Hit okay.

The colors on your map should change. In general, it should look more blue because Republican Don Young won more precincts in the House race than Dan Sullivan in the Senate Race.

(9) Open the Print Manager window.

(10) Highlight map0 in the Items Pane, select the Item Properties Tab below (if it's not open already) and hit "Update preview".

We need to change the title of the map:

(11a) Select the item that says "2014 U.S. Senate Results...." or "QGIS" in the item pane.
(11b) Under Main Properties, delete the text in the box and type "2014 U.S. House Results by Precinct"

The legend should have auto-updated. If not:

(12a) Select Legend in the item pane.
(12b) Under Legend Items, toggle the Auto update box on, off and on again.

(13) Print your map by hitting the mountain image icon on the top of the screen.

Try to do the same for the Governor's race on your own. The relevant variable is Gov Wincode. Your map should be green and blue since there was no Democrat in the race.

That's it! It's pretty simple, right? As always, remember to save your project.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #16 on: November 04, 2017, 03:27:06 PM »

Damn, this is pretty awesome of you to do. I've always wanted to learn how to use QGIS.

Thanks! Also see jimrtex's excellent Hudson QGIS thread from last year to learn some more advanced editing features of QGIS.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #17 on: November 04, 2017, 04:12:47 PM »
« Edited: November 04, 2017, 04:17:30 PM by cinyc »

Calculating Wincodes Part 5 - Making an Inset Map in the Print Composer

The precinct map of Alaska is nice and all, but it really emphasizes rural areas at the expense of populated cities, like Anchorage. We can use the print manager to draw an Anchorage inset map. It's easier than you think.

(1) Open the Alaska print composer (if you're continuing the lesson immediately after completing Part 4, just select the print composer in Windows, otherwise open your project in QGIS and select Project-->Print Composers-->Alaska.

(2) In the print composer, select Composer-->Duplicate Composer from the drop down menu. You can also click on the icon that looks like a white piece of paper on top of a grey one with an orange star. (It's probably the 2nd icon to the right after the save diskette.) See the image below if you can't find it:



(3) Give your new composer a name: Alaska with ANC Inset.

A new print composer should pop up.

(4) Go to your main QGIS screen, select the plus magnifier icon, and draw a box around Anchorage. If you don't know where Anchorage is, it's in South Central Alaska. Your zoom should look something like this:



(5a) Open up the print composer. Create a new map by hitting the new map icon. (If you don't remember which that is, it's marked as 5 in the image under step 2.)
(5b) Draw a rough square somewhere where there's free space on the map. I drew mine in the crook between Kodiak Island and the map scale.

A new map, Map1 should pop up. Map0 and Map1 are confusing names, so:
(6a) Double click on Map0 in the Item pane. Change the name to Alaska.
(6b) Double click on Map1 in the Item pane. Change the name to ANC Inset.

Now, we're ready to make the inset:
(7a) Select the Item properties tab underneath the Item pane for ANC Inset if it isn't already.
(7b) Under extents, hit the Set map to canvas extent button

The inset is probably not as zoomed in on Anchorage as it could be:

(7c) Play around with the coordinates under extents until Anchorage fills the inset. I used the following coordinates:
X Min: 200000
Y Min: 1200000
X Max: 300000
Y Max: 1290000

(7d) Move the inset to make sure it's not obscuring Kodiak Island or other parts of the map, including the scale. Make sure it is aligned with the bottom of the page (a red line should come up when it is).

(7e) If you need to make the inset smaller or larger, click on the upper left corner and move diagonally.

(7f)  Open the Position and size menu on the Items tab. Click the bottom center square next to Reference Point.

Next, we'll add a title to the inset:

(8a) Click on the add text icon. It's the piece of paper with a T.
(8b) Draw a rectangle above your inset.
(8c) Type "Anchorage" or "Anchorage Inset" in the box under Main properties.
(8d) Change the font as you deem fit. I used MS Shell Dig 2, Bold, 12 pt.
(8e) Open the Position and size menu on the Items tab. Click the bottom center square next to Reference Point.


(9) Add a scale bar, if you want, by clicking on the Anchorage Inset map, then the scale icon, drawing a rectangle, and following the instructions in the basic print composer tutorial. It's probably unnecessary.

No new legend is necessary, as Anchorage is included on the main map, and the color scale for precincts in the Anchorage inset are already included.

(10) Export your new map as an image by hitting the Export image icon.

That's it! You've made a map with an inset! It should look something like this:



Like the main map, the inset map can be easily updated when you change variables. Just remember to hit the Update preview button on BOTH maps - but everything should auto-update when you save as a png file, anyway.

Remember to save your project!
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #18 on: November 04, 2017, 10:52:53 PM »

What are WINCODES? It would help me know what you are trying to make your xlsx file look like, and what you are ultimately try to produce. It would help to know what your shapefiles contain since we are trying to match them.

Particularly for exemplary purposes, I would focus on a single race. If you want to compare different results, you can always do multiple joins. I would have kept the Absentee, Questionable, and Early Voting separate by legislative district. Since Alaska does not attribute these votes back to election precinct, and they represent about 1/3 of votes, I would do an analysis of how these results varied by legislative district (e.g. share of in person votes, whether different types of votes were politically different. It might be possible to adjust the precinct in person votes. At minimum, you could aggregate results by legislative district.

I'll add a sentence describing WINCODES. It's already explained more in part two - In order to match the relevant QGIS winner color, we need to add an integer to the percentage of all those who won a precinct except Republicans (since the R color is in the 0-1 range). For example, a D winner has to have 2 added to their percentage if they win a precinct to generate a red color in QGIS using the 7 Color scheme.

I checked the shapefile before determining whether I had to truncate the precinct code. A link to the relevant shapefile is in Part 3 of the discussion. Unfortunately, the full precinct names from the Alaska Precinct shapefile are in terrible shape - some are capitalized, some have extraneous spaces, etc. I couldn't do a join on that column (NAME) without getting NULLs.

I wasn't planning on using the data to sum up the results by House District. You're right - that can be another lesson. I suppose we can do the remove duplicates first and then create a Precinct column in Sheet1 instead of doing it on the Data sheet. I'll rewrite the write-up to do that.

I like to have all of one year's data in the same spreadsheet without doing multiple joins. One of the purposes of the exercise was to show how to change variables and generate a new map in the print composer in Part 4.

Quote
You must be logged in to read this quote.
If you save the precinct results as a .csv file, you can open them directly in Excel. Note that "circle" is called a "radio button"[/quote]

OK
Quote
You must be logged in to read this quote.
Possibly premature to extract ID's, since this is where we merged all of the absentee, questionable, and early votes into a pseudo precinct called "Distri". If we kept them separate, we could create precinct IDs like 40-ABST, 40-QUES, 40-EARL which could then be aggregated into totals for the legislative district. It doesn't matter if not all data can be joined. Note, there are ways to check when there are shapefiles, but with no data to join with them.

If I had done it your way, I would type: "=LEFT(", then click on cell A1, and then typed ",6)" at the point where you select A1, press function key F4 and you can cycle through various absolute and relative expressions. $A1 might be a little bit better if you were going to also fill in columns to the right.

I would then do a GOTO  (G1:G56184) and pressed CTRL-D to fill down. CTRL-R fills right and you can fill up or left from the Fill command.

You could also do a CTRL-End to get to the end of your worksheet, click on G56184 (i.e. last row) and then SHIFT-CTRL-UPARROW to select the entire column followed by CTRL-D.[/quote]
What's a GOTO?

That is how I generated the formula - moused over to A1, then hit 6. I wasn't planning on adding any additional columns, so I didn't see the need to cycle through the relative values (in fact, I didn't even know <F4> did that - I usually just manually type the dollar signs.

CTRL-End, Shift-Ctrl-Up Arrow, CTRL-D seems to work.
Quote
You must be logged in to read this quote.
I couldn't find Select Data-->Remove Duplicates.  Instead I clicked on the light bulb and typed "Remove Duplicates" and a menu popped up.[/quote]

I knew I should have put a few pictures in this discussion. I also miscapitalized "Select". I'm using the newest version of Excel. It has a Data tab on the top ribbon. If you click on that, there's a Remove Duplicates icon.  I'll add a picture to explain.

Quote
You must be logged in to read this quote.
I would have entered in B2

=SUMIFS(Data!$F$1:$F$56184,Data!$C$1:$C$56184,Sheet1!B$1,Data!$G$1:$G$56184,Sheet1!$A2)

And then used CTRL-R and CTRL-D to copy across and down. I had not understood why you also select on the Office because of the weird way they did write-ins. When I went back to correct for the write-ins I added the Office criteria for only the write-ins.[/quote]

You like CTRL-R and CTRL-D. I prefer CRTL-C and CTRL-V. I suppose CTRL-R/CTRL-D saves a keystroke. I'll add alternative keystroke instructions.

Quote
You must be logged in to read this quote.
This is critical. You'll never quite figure out the format that was used (Alaska included results for Senate District 5, rather than Senate District S). Not only did I not have the write-ins, I didn't understand that the "Distri" results were not district totals and repeated.

I've never found a simple way to do partial transposes. You might write a script, but by the time you got it working, you could have used a more brute force method. If every state used the same format, it might work to develop a more automated way.

Amother approach would be sort by Office, Candidate, and Precinct. You could then just copy the column for each candidate. If I did this, I would double check that the precinct order was the same for each column.

Quote
You must be logged in to read this quote.

This is very important step. We want the data in this format, as if the state had provided it this way. You might want to start this tutorial showing the desired end result. The rest is not necessarily showing how to do it, but to suggest a way short of timing in all the results by hand, which would discourage the attempt. Someone reading the tutorial could then skip to the next message.
[/quote]
OK
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #19 on: November 05, 2017, 10:41:35 PM »
« Edited: November 05, 2017, 11:12:55 PM by cinyc »


That’s in the works.

Quote
You must be logged in to read this quote.
I never rename source shapefiles. The census files at least are easy to understand what they are. You haven't actually changed the file by renaming the layer, but it might give the impression that you are.

[/quote]

You have to rename the layers if you use the QGIS print composer. Otherwise, the legend will have the Census Bureau’s name instead of something describing what the color/lines depict. Plus, since Census’ names are very long, not changing them makes the legend a lot wider than it need be.

I’ll mention you haven’t actually changed the filename, just the name in the Legend Panel when I revise things again.  I’ll also take your suggestions for rewriting the first part into account and put in a GOTO description.

I think we’re just going to disagree on whether to include multiple races in the same spreadsheet. My spreadsheets generally include multiple races and even multiple years. It’s much easier to compute things like swings and PVIs that way. Yes, we can do that in QGIS by creating a new column, but I find it easier to do math in Excel instead of QGIS.

One thing I haven’t tried to figure out yet is what happens if you try to import and join an Excel spreadsheet with multiple sheets. Does it import all the sheets or just the first? I’ll have to test that and get back.

Quote
You must be logged in to read this quote.

That's bizarre. I don't think I modified the Excel ribbon at all. The data tab in both my Excel 2007 and Excel 2016 have a Remove Duplicates button. Try right clicking on the Ribbon, selecting Customize the Ribbon and looking for the "Data Tools" group under Commands Not in the Ribbon. Remove Duplicates is in that group.

If its not there, perhaps it's in the Analysis ToolPak add-in, which I have enabled?
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #20 on: November 07, 2017, 01:29:35 AM »

(6) Save the layer to disk. Give it a name like Alaska_Counties. Be sure to browse to the directory where you are going to save the file and be sure the format is ESRI Shapefile

You probably should explain how to do this - Right Click on the layer name and Select "Save As" or Layer-->Save As.

I don't see any button on the toolbars to save a layer, but I tend to keep toolbars off unless I need them. I'd rather have the maximum map on the screen to work with.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #21 on: March 27, 2018, 08:00:05 PM »

That's one way of doing it. You can also create a wincode and use QGIS to create discrete or continuous graduated color ramps. You can do that in 3.0 by selecting Symbology from the properties menu, then selecting Graduated from the drop down menu. Clicking on the down arrow next to the color ramp will allow you to select color ramps.

QGIS has a series of color ramps to choose from (look under "All Color Ramps" for the complete list, or you can make your own).

Making your own color ramp is a bit complicated (QGIS' symbology is probably its weakest point). The easiest way to do it would probably be to load the 8 color ramp and individually change the colors in the tabs. Load the 8 color ramp and click on the colors next to the color ramp. The first color stop is the dimmest color in the 0-1 range; the second is the brightest. The third color tab is the dimmest in the 1-2 range; the fourth is the brightest, etc.  The stops are set at 0%,9.9%;10%,19.9%:20%,29.9%, etc.

Then, just calculate a WINCODE for each plurality racial group.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #22 on: February 06, 2022, 02:42:12 PM »

Are you trying to do a difference or a true split?

The easiest way to split is probably selecting the 2 polygons and using the Intersection tool from the Processing Toolbox to do an intersection. This should split the polygons along the old lines.

Alternatively, you can transform the polygon in Layer B and split by the polygon in Layer A by line using the Split by Line tool making sure the line overlaps the edge of the polygon.

There are also advanced processing tools that might allow you to do it manually, plus a few plugins (like Split Features on Steroids) that might help.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #23 on: February 06, 2022, 03:56:42 PM »

It's also in the Toolbox.

You can save it as a permanent layer by hitting the dots and saving it wherever you want.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


« Reply #24 on: February 06, 2022, 04:51:10 PM »

Run an intersection on the selected polygons. Make sure Layer A is first listed, in case of duplicate column names, so that they don't get a _2 prefix.

Delete the old polygon in Layer A. Copy and paste the new shps from the temporary intersection into Layer A.

Make sure you do this on a seperately-saved version of Layer A if you want to reuse the old shapefile for a different project.
Logged
Pages: [1] 2  
Jump to:  


Login with username, password and session length

Terms of Service - DMCA Agent and Policy - Privacy Policy and Cookies

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Page created in 0.226 seconds with 15 queries.