Racket Plot: Energy Use and Country Populations

DrRacket is good for visualizations. Emacs helps me do the preparatory thinking and editing, but adjusting plot details is fun in DrRacket. The .csv data used for this post's visualization is below the code.

DrRacket Screenshot: Visualizing Energy Use and State Populations

Using the same wikipedia data as a previous script and post (fn:1), this is a reproduction of another of Hiroaki Koide's小出裕章の graphs. It was good brain training and attention strengthening to figure out how to do this with Racket Plot.

Unjust Energy Use and Country Population Visualization

Reproducible research techniques with DrRacket could contribute to a good, exploratory education. Learners can hack the script, make lists of countries to label, adjust the sizes of the plot and create something that they feel is worth printing out to thinking about...

I love grass-roots activist books like these by Hiroaki Koide., The books remind me a bit of Yu Tanaka's 田中優 books about dams, nukes, and other forms of environmental degradation. But all the facts, figures, and illustrations can be overwhelming. Maybe a Graded Direct Method approach in classrooms could empower more people to think, decide, and act according to individual conscience.

Koide Hiroaki Book Covers

Translating the Koide-san's plots into English might be useful for people. And it's probably important that these sorts of plots are made with free software, and that the scripts are available for people to improve and use. The inspiration for this script is from page 89 of 「小出裕章さんおおはなし」野村保子. A less honed version is in 「核の真実」too...

Hiroaki Koide's original plot

code

#lang racket

(current-directory-for-user "/home/brian/Racket/Earth-Data/")
(define Energy-Data-File "EnergyConsumption2003W.csv")
(define Population-File "Population2019.csv")

(define get-path
  (lambda (file-name)
    (build-path (current-directory-for-user) file-name)))

(define get-data
  (lambda (pth)
    (let* ((inp (open-input-file pth))
	   (lines (port->lines inp)))
      (close-input-port inp)
      (map (lambda (s) (string-split
			(regexp-replace* "\"" s "")
				     ","))
	   lines))))

(define csvf->dict
  (lambda (file-name)
    (get-data (get-path file-name))))

(define EnergyDict (csvf->dict Energy-Data-File))
(define PopulationDict (csvf->dict Population-File))

(require racket/dict)

(define ref-str->num
  (lambda (dct)
    (map (lambda (l) (list (first l)
			   (string->number (second l))))
	 dct)))

(define ref-str->sym
  (lambda (dct key)
    (string->symbol (car (dict-ref dct key)))))

(define PopNums (ref-str->num (cdr PopulationDict)))
(define EnergyNums (ref-str->num (cdr EnergyDict)))

(define ref-sort
  (lambda (dct)
    (sort dct #:key second >)))

(define OrdrdEnergyNumsDict
  (ref-sort EnergyNums))

(define shared-key-combined-refs
  (lambda (d1 d2)
    (let ((cmbnd '()))
      (dict-for-each d1
		     (lambda (k v)
		     (when (dict-has-key? d2 k)
		       (set! cmbnd (cons (list k
					    (car (dict-ref d1 k))
					    (car (dict-ref d2 k)))
				      cmbnd)))))
      (reverse cmbnd))))

(define Combined-EnePop-EneOrder
  (shared-key-combined-refs OrdrdEnergyNumsDict PopNums))

(require plot)
(require plot/utils)

(define clrz (color-seq "red" "blue" (length Combined-EnePop-EneOrder)))
(define line-interval-box
  (lambda (xstart xend y clr)
    (lines-interval (list (vector xstart 0) (vector xstart y))
                   (list (vector xend 0) (vector xend y))
                   #:color clr #:line1-color clr #:line2-color clr)))

(define boxes-x-accum-y
  (lambda (xl yl clrz)
    (define helper
      (lambda (accu-x xvals yvals clrz accu-bxs)
        (let ((xval (+ accu-x (car xvals)))
              (yval (car yvals))
              (clr (car clrz)))
          (cond
            ((empty? (cdr xvals)) (cons (line-interval-box accu-x xval yval clr) accu-bxs))
            (#t (helper xval (cdr xvals) (cdr yvals) (cdr clrz)
                       (cons (line-interval-box accu-x xval yval clr) accu-bxs)))))))
    (helper 0 xl yl clrz '())))

(require (only-in math/statistics mean))

(define mean-energy (mean
                     (map second Combined-EnePop-EneOrder)))


#;(parameterize ((plot-x-ticks (linear-ticks #:number 6)))
    (plot (list
           (boxes-x-accum-y (map third Combined-EnePop-EneOrder)
                           (map second Combined-EnePop-EneOrder)
                           clrz)
           (hrule mean-energy))
     #:width 800
     #:x-label "Population"
     #:y-label "Energy Consumption"))

#;(define pop-bar-label
  (lambda (k)
    (let ((xy (dict-ref Combined-EnePop-EneOrder k)))
      (point-label (vector (second xy) (first xy))
		   k
		   #:anchor 'left
                   #:angle 45))))

(define get-country-pop-label-point
  (lambda (k)
    (let* ((k-pop (second (dict-ref Combined-EnePop-EneOrder k)))
	   (k-pop-half (* .5 k-pop)))
      (define helper
	(lambda (accu-pop dct)
	  (cond
	   ((empty? dct) 0)
	   ((equal? k (first (car dct))) (+ accu-pop k-pop-half))
	   (#t (helper (+ accu-pop (second (cdar dct))) (cdr dct))))))
      (helper 0 Combined-EnePop-EneOrder))))

(define pop-bar-label
  (lambda (k)
    (let* ((x (get-country-pop-label-point k))
	   (y (first (dict-ref Combined-EnePop-EneOrder k))))
      (point-label (vector x y)
		   k
		   #:anchor 'left
                   #:size 8
		   #:angle 45))))

(define label-countries
  (list "United States" "Haiti" "Chile"  "Qatar" "Japan" "South Korea" "North Korea" "Nicaragua" "Sri Lanka" "Canada" "Sweden" "Angola" "Cuba" "Ghana" "Russia" "Poland" "Vietnam" "Spain" 
       "Zambia" "Afghanistan" "China" "India" "Nigeria" "Philippines" "Germany" "Brazil" "Indonesia" "Bangladesh" "Mexico" ))
                         

(parameterize ((plot-x-ticks (linear-ticks #:number 6)))
    (plot (list
           (boxes-x-accum-y (map third Combined-EnePop-EneOrder)
                           (map second Combined-EnePop-EneOrder)
                           clrz)
           (hrule mean-energy)
           (point-label (vector (* 3 (expt 10 9)) mean-energy)
                       "Average Energy Use"
                       #:color "gray"
                       #:anchor 'left
                       #:angle 45
                       #:point-size 2)
         (map pop-bar-label label-countries)
           )
     #:width 1200
     #:x-label "Population"
     #:y-label "Energy Consumption"
     #:x-max  (* 8 (expt 10 9))
     #:out-file "Population-EnergyConsumption-DrRacket-1.png"
     #:out-kind 'png
     ))
'''
#### Sample Org-mode R data for webscraping

** Population #+name: WikipediaPopulation #+BEGINSRC R :results output :exports both :session R-1 require(rvest) esurl <– “https://en.wikipedia.org/wiki/Listofcountriesanddependenciesbypopulation” espage <– readhtml(esurl) %>% htmlnodes(“table”) %>% htmltable(fill=TRUE)

# English Speaker data is in table number three in the list returned by rvest

estable <– espage[[1]] estable write.csv(estable, “WikipediaPopulation20190523.csv”) #+END_SRC


### data
#### Population

Country(or dependent territory),Population China,1397460000 India,1347550000 United States,329249000 Indonesia,268074600 Brazil,209942000 Pakistan,204754000 Nigeria,200962417 Bangladesh,166600000 Russia,146793744 Mexico,126577691 Japan,126200000 Philippines,107677000 Egypt,98702000 Ethiopia,98665000 Vietnam,95354000 Democratic Republic of the Congo,86727573 Germany,82979100 Iran,82471700 Turkey,82003882 France,66992000 Thailand,66366542 United Kingdom,66040229 Italy,60375749 South Africa,57725600 Tanzania,55890747 Myanmar,54339766 Kenya,52214791 South Korea,51811167 Colombia,49849818 Spain,46733038 Argentina,44938712 Algeria,43378027 Ukraine,42101650 Sudan,41546919 Uganda,40006700 Iraq,39127900 Poland,38413000 Canada,37486500 Morocco,35027200 Uzbekistan,33512925 Saudi Arabia,33413660 Malaysia,32735500 Peru,32495510 Venezuela,32219521 Afghanistan,31575018 Ghana,30280811 Angola,30175553 Nepal,29609623 Yemen,29579986 Mozambique,27909798 Ivory Coast,25823071 North Korea,25450000 Australia,25369400 Madagascar,25263000 Cameroon,24348251 Taiwan,23589192 Niger,22314743 Sri Lanka,21670112 Burkina Faso,20870060 Mali,19973000 Romania,19523621 Chile,19107216 Syria,18499181 Kazakhstan,18480432 Guatemala,17679735 Malawi,17563749 Zambia,17381168 Netherlands,17320000 Ecuador,17243800 Cambodia,16289270 Senegal,16209125 Chad,15692969 Somalia,15636171 Zimbabwe,15159624 South Sudan,12778250 Rwanda,12374397 Guinea,12218357 Benin,11733059 Haiti,11577779 Tunisia,11551448 Bolivia,11469896 Belgium,11463692 Cuba,11221060 Burundi,10953317 Greece,10741165 Czech Republic,10649800 Jordan,10421300 Dominican Republic,10358320 Portugal,10291027 Sweden,10255102 Azerbaijan,9981457 Hungary,9778371 United Arab Emirates,9682088 Belarus,9465300 Honduras,9158345 Israel,9032820 Tajikistan,8931000 Austria,8859992 Papua New Guinea,8558800 Switzerland,8542323 Sierra Leone,7901454 Togo,7538000 Hong Kong (China),7482500 Paraguay,7152703 Laos,7123205 Serbia,7001444 Bulgaria,7000039 El Salvador,6704864 Libya,6569864 Nicaragua,6393824 Kyrgyzstan,6389500 Lebanon,6065922 Turkmenistan,5942561 Denmark,5811413 Singapore,5638700 Congo,5542197 Finland,5518393 Central African Republic,5496011 Slovakia,5450421 Norway,5334762 Eritrea,5309659 Costa Rica,5058007 Palestine,4976684 New Zealand,4965870 Ireland,4857000 Oman,4685390 Liberia,4475353 Kuwait,4420110 Panama,4218808 Croatia,4105493 Mauritania,4077347 Georgia,3723500 Moldova,3547539 Uruguay,3518552 Bosnia and Herzegovina,3501774 Mongolia,3258962 Puerto Rico (U.S.),3195153 Armenia,2962100 Albania,2862427 Lithuania,2790472 Qatar,2772294 Jamaica,2726667 Namibia,2458936 Botswana,2338851 The Gambia,2228075 Gabon,2109099 Slovenia,2080908 North Macedonia,2075301 Lesotho,2007201 Latvia,1915100 Kosovo,1798506 Guinea-Bissau,1604528 Bahrain,1543300 East Timor,1387149 Trinidad and Tobago,1359193 Equatorial Guinea,1358276 Estonia,1324820 Mauritius,1265577 Eswatini,1093238 Djibouti,1078373 Fiji,884887 Comoros,873724 Cyprus,864200 Guyana,786508 Bhutan,741672 Solomon Islands,680806 Macau (China),667400 Montenegro,622359 Luxembourg,613894 Western Sahara,582478 Suriname,573085 Cape Verde,550483 Malta,475701 Transnistria,469000 Brunei,421300 Belize,398050 Bahamas,385340 Maldives,378114 Iceland,358780 Northern Cyprus,351965 Vanuatu,304500 Barbados,287010 New Caledonia (France),282200 French Polynesia (France),275918 Abkhazia[Note 20],244832 São Tomé and Príncipe,201784 Samoa,200874 Saint Lucia,180454 Guam (U.S.),172400 Curaçao (Netherlands),160012 Artsakh,148000 Kiribati,120100 Aruba (Netherlands),111849 Saint Vincent and the Grenadines,110520 Grenada,108825 Jersey (UK),105500 Federated States of Micronesia,105300 United States Virgin Islands (U.S.),104909 Antigua and Barbuda,104084 Tonga,100300 Seychelles,96762 Isle of Man (UK),83314 Andorra,76177 Dominica,74679 Bermuda (UK),64027 Cayman Islands (UK),63415 Guernsey (UK),62063 American Samoa (U.S.),56700 Northern Mariana Islands (U.S.),56200 Saint Kitts and Nevis,56345 Greenland (Denmark),55992 Marshall Islands,55500 South Ossetia,53532 Faroe Islands (Denmark),51540 Turks and Caicos Islands (UK),41369 Sint Maarten (Netherlands),40614 Liechtenstein,38380 Monaco,38300 Saint-Martin (France),35746 Gibraltar (UK),34879 San Marino,33422 British Virgin Islands (UK),32206 Palau,17900 Cook Islands (NZ),15200 Anguilla (UK),15174 Wallis and Futuna (France),11700 Nauru,11000 Tuvalu,10200 Saint Barthélemy (France),9793 Saint Pierre and Miquelon (France),6008 Saint Helena Ascensionand Tristan da Cunha (UK),5633 Montserrat (UK),5220 Falkland Islands (UK),3198 Christmas Island (Australia),1928 Norfolk Island (Australia),1756 Niue (NZ),1520 Tokelau (NZ),1400 Vatican City,800 Cocos (Keeling) Islands (Australia),538 Pitcairn Islands (UK),50


#### Energy Use

Country/Territory,2003 W Afghanistan,119.8 Albania,863.2 Algeria,1515.5 Angola,953.9 Argentina,2458.9 Armenia,1052.9 Australia,7446.8 Austria,5370.3 Azerbaijan,1740.8 Bahrain,10323.2 Bangladesh,278.1 Belarus,3890.4 Belgium,7436.6 Benin,549.6 Bolivia,981.7 Bosnia and Herzegovina,2267.9 Botswana,1501.6 Brazil,1814.1 Brunei Darussalam,11061.9 Bulgaria,3155.6 Cambodia,473.1 Cameroon,482.9 Canada,9825.2 Chile,2405.5 China,2405.5 Colombia,927.1 Congo,963.4 Costa Rica,1328.5 Cote d'Ivoire,645.6 Croatia,2572.2 Cuba,1298.5 Cyprus,2949.3 Czech Republic,5582.3 Denmark,4620.5 Dominican Republic,1118.4 Ecuador,1113.5 Egypt,1202.4 El Salvador,901.2 Eritrea,188.7 Estonia,5531.3 Ethiopia,532.9 Finland,9036.5 France,5366.2 Gabon,1887.4 Georgia,3142.8 Germany,5329.9 Ghana,508.9 Greece,3249.3 Guatemala,949.0 Haiti,304.9 Honduras,800.1 Hong Kong,2598.0 Hungary,3417.3 Iceland,22477.4 India,753.1 Indonesia,1153.7 Iran,3750.2 Iraq,1571.4 Ireland,4284.0 Israel,4001.3 Italy,3747.4 Jamaica,1504.7 Japan,5190.3 Jordan,1586.2 Kazakhstan,6118.0 Kenya,642.9 North Korea,1013.4 South Korea,6736.8 Kosovo,1826.9 Kuwait,16248.8 Kyrgyzstan,713.2 Latvia,2624.6 Lebanon,2031.8 Libya,4011.5 Lithuania,2805.2 Luxembourg,11107.3 Macedonia,1867.0 Malaysia,3405.5 Malta,2680.0 Mexico,2090.8 Moldova,972.8 Mongolia,1582.6 Montenegro,1735.5 Morocco,687.9 Mozambique,580.5 Myanmar,388.6 Namibia,935.3 Nepal,454.1 Netherlands,6685.0 New Zealand,5547.2 Nicaragua,721.6 Nigeria,950.1 Norway,8837.0 Oman,9569.7 Pakistan,648.8 Panama,1428.2 Paraguay,987.7 Peru,888.2 Philippines,577.8 Poland,3537.5 Portugal,2946.5 Qatar,17041.2 Romania,2173.1 Russia,6581.2 Saudi Arabia,8212.0 Senegal,362.1 Serbia,2850.5 Singapore,8595.2 Slovakia,4367.6 Slovenia,4686.7 South Africa,3645.1 Spain,3691.5 Sri Lanka,636.3 Sudan,493.9 Sweden,7280.4 Switzerland,4458.5 Syrian Arab Republic,1415.2 Tajikistan,446.7 Tanzania,596.1 Thailand,2261.9 Togo,593.9 Trinidad and Tobago,21187.0 Tunisia,1215.3 Turkey,1924.0 Turkmenistan,5626.3 Ukraine,3787.7 United Arab Emirates,11012.6 United Kingdom,4332.5 United States,9538.8 Uruguay,1652.1 Uzbekistan,2041.1 Venezuela,3553.0 Vietnam,907.2 Yemen,396.6 Zambia,835.9 Zimbabwe,1017.2 ```

#emacs #r #rvest #orgmode #datavisualization #DrRacket #Racekt #HiroakiKoide #KoideHiroaki #小出裕章 #エネルギー #Energy #Population