Fishery Decline Visualization

Working with a table full of data worthy of attention is great for keeping focus. Getting data into “long form” as is done with R might not be necessary with Racket, at least not with the simple data I work with for visualizations. The code for this visualization was getting too complex for a few days, I had to go back and get the data into a convenient form. Racket's ->plot-label *number* *digits* makes it easy to arrange for helpful labels.

(“1954” (“mullet” 54453.75 90.75625000000001)

Fishery Decline 1950 - 1956

The data from Harada Masazumi's book provides hints to imagine how terrible environmental damage becomes in the years before the first human diseases are recognized. I would like to work with similar data for the decline in other species. There must be equivalent figures for honeybees related to Colony Collapse Disorder.

Minamata disease was first discovered in Minamata city in Kumamoto prefecture, Japan, in 1956. It was caused by the release of methylmercury in the industrial wastewater from the Chisso Corporation's chemical factory, which continued from 1932 to 1968. This highly toxic chemical bioaccumulated in shellfish and fish in Minamata Bay and the Shiranui Sea, which, when eaten by the local population, resulted in mercury poisoning. While cat, dog, pig, and human deaths continued for 36 years, the government and company did little to prevent the pollution. The animal effects were severe enough in cats that they came to be named as having “dancing cat fever”. (fn:1)

I think it was in the early 1950s that the Chisso corporation change the industrial processes that used methyl mercury. The corporation achieved a sort of short-term efficiency. I wonder what “efficient” processes were changed in the years leading up to mass bee disappearance and disease, and how people are being influenced. I've seen suggestions that a major cause may be a new sort of pesticide: “neonicotinoids”? Agricultural chemicals link a variety of catastrophes, Chisso means nitrogen in Japanese, and after electricity for mining, it was fertilizer that the corporation started with. The massive dead zone in the Gulf of Mexico comes to mind: nitrogen run-off. “AgroEcology of Barbarism” or maybe “AgroEcology or Extinction” is the appropriate thought to have....

Fishery Decline 1950- 1956: DrRacket Screenshot

code

#lang racket

(define my-home (find-system-path 'home-dir))
(define learning-directory "Learning-Minamata")

;; 単位 貫 カン 3.75kg 8.3lb
(define data-file "HM-1-p11-FisheryFigures.csv")
;; 魚種,昭和25〜28年平均,29年,30年,31年
;; ボラ,16000,14521,10136,5901
;; エビ,4727,2425,1558,945
;; 片ロイワシ,44515,27076,12536,6926
;; コノシロ,8457,1811,1615,318
;; タチ,13851,7931,6535,5354
;; タコ,3896,2430,2033,1179
;; イカ,3293,2517,1480,1043
;; カキ,2659,1973,1427,429
;; ナマコ,2750,2302,1630,535
;; ハモ,2083,1726,1243,603
;; カニ,1441,1702,1024,600
;; その他,18789,8102,4731,1660
;; 計,122460,74516,45948,25493


(define data-path (build-path my-home learning-directory data-file))

(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 fishery-data (get-data data-path))
;(define headers (first fishery-data))	
;; '("魚種" "昭和25〜28年平均" "29年" "30年" "31年")
(define headers-en
  '("fish" "1950-53avg " "1954" "1955" "1956"))
(define fish-en '("mullet" "shrimp" "anchovy" "shad"
                          "hairtail" "octopus" "squid" 
                          "oyster" "sea slug" "sea eel" "crab" "other" "total"))

(define replace-firsts
  (lambda (orig-list new-firsts)
    (define replace-first  ;; Racket-style guide "lambda is cute but..."
      (lambda (lst frst)
        (cons frst (rest lst))))
    (map replace-first orig-list new-firsts)))

(define (replace-headers-firsts table headers firsts)
  (cons headers
	(replace-firsts (cdr table) firsts)))

(define (貫string->kg-number str)
  (* 3.75 (string->number str)))
(define (貫strings-row->kg-numbers row)
  (define head (car row))
  (cons head
	(map 貫string->kg-number (cdr row))))

(define fishery-data-en
  (cons headers-en
	(replace-firsts
	 (map 貫strings-row->kg-numbers
	      (cdr fishery-data))
	 fish-en)))
;; '(("fish" "1950-53avg " "1954" "1955" "1956")
;;   ("mullet" 60000.0 54453.75 38010.0 22128.75)
;;   ("shrimp" 17726.25 9093.75 5842.5 3543.75)
;;   ("anchovy" 166931.25 101535.0 47010.0 25972.5)
;;   ("shad" 31713.75 6791.25 6056.25 1192.5)
;;   ("hairtail" 51941.25 29741.25 24506.25 20077.5)
;;   ("octopus" 14610.0 9112.5 7623.75 4421.25)
;;   ("squid" 12348.75 9438.75 5550.0 3911.25)
;;   ("oyster" 9971.25 7398.75 5351.25 1608.75)
;;   ("sea slug" 10312.5 8632.5 6112.5 2006.25)
;;   ("sea eel" 7811.25 6472.5 4661.25 2261.25)
;;   ("crab" 5403.75 6382.5 3840.0 2250.0)
;;   ("other" 70458.75 30382.5 17741.25 6225.0)
;;   ("total" 459225.0 279435.0 172305.0 95598.75))

;;   ("crab" 5403.75 6382.5 3840.0 2250.0)
(define (percentages-from-first-value row)
  (define fish (car row))
  (define standard (second row))
    (define percent-of-standard
      (lambda (num)
	(* 100 (/ num standard))))
    (cons fish
	  (map percent-of-standard (cdr row))))

(define (data-row->plots-row row)
  (define percents (cdr (percentages-from-first-value row)))
  (define fish (car row))
  (define figures (cdr row))
  (define (list-fish-and-numbers figr pcnt)
    (list fish figr pcnt))
  (map list-fish-and-numbers figures percents))
    
(define (transpose-table table)
  (define (helper ot nt)
    (cond
     ((empty? (car ot)) (reverse nt))
     (#t (helper (map cdr ot) (cons (map car ot) nt)))))
  (helper table '()))

(define (data-table->plots-table table)
  ;  '("fish" "1950-53avg " "1954" "1955" "1956")
  (define headers (cdr (car table)))
  (define plot-var-rows
    (map data-row->plots-row (cdr table)))
  (define merged-row-table
    (cons headers plot-var-rows))
  (transpose-table merged-row-table))

(define years-alst
  (data-table->plots-table fishery-data-en))

(require plot/utils)

(define (percent->diff-label num)
  (if (= num 100)
      ""
      (string-append
       (->plot-label (- (- 100 num)) 0)
       "%")))

(define (kg-fig->kg-label num)
  (string-append (->plot-label num 0) "kg"))

(require plot)

(define horizontal-line
  (lambda (x y #:clr (clr "black")) ;; lne-wdh lne-stl ..etc
    (lines (list (vector 0 y) (vector x y)) #:color clr)))

(define labeled-point
  (lambda (x y l #:ancr (ancr 'left) #:fnt-sze (fnt-sze 8) #:pnt-sze (pnt-sze 5)
            #:pnt-clr (pnt-clr 0) #:lbl-angl (lbl-angl 0) )
    (point-label (vector x y) l #:anchor ancr #:size fnt-sze #:point-size pnt-sze
                #:point-color pnt-clr #:angle lbl-angl)))

(define (plot-a-fish fish-row y)
  (define fish (first fish-row))
  (define x-percent (third fish-row))
  (define kglabel (kg-fig->kg-label (second fish-row)))
  (define fish-kglabel (string-append fish " " kglabel))
  (define %label (percent->diff-label x-percent))
  (list
   (horizontal-line x-percent y)
   (points (list (vector x-percent y)) #:sym 'fullcircle)
   (labeled-point 0 y fish-kglabel #:ancr 'bottom-left #:pnt-sze 0)
   ;(labeled-point 0 y kglabel #:ancr 'top-left #:pnt-sze 0)
   (labeled-point 100 y %label #:ancr 'bottom-right #:pnt-sze 0 #:fnt-sze 10)
   (labeled-point 100 y fish #:ancr 'bottom-left #:pnt-sze 0 #:fnt-sze 10)
   (lines (list (vector x-percent y) (vector 100 y)) #:color "red")))
;; (plot (plot-a-fish (second (second years-alst)) 2) #:x-max 120)

(define (plot-a-year row y-center)
  (define x-label (car row))
  (define y-limit .45)
  (define ys (linear-seq
	      (- y-center y-limit) (+ y-center y-limit)
	      (length (cdr row))))
  (define (helper rows ys plots)
    (cond
     ((empty? rows) (reverse plots))
     (#t (helper (cdr rows) (cdr ys)
		 (cons (plot-a-fish (car rows) (car ys)) plots)))))
  (list (labeled-point 0 y-center x-label #:fnt-sze 12 #:ancr 'right #:lbl-angl (/ pi 4))
     (horizontal-line (- 20) (+ .05 y-limit y-center) #:clr "gray")  
	(helper (cdr row) ys '())))

(define (plot-years table)
  (define len (length table))
  (define y-seq (linear-seq 1  len len))
  (define (helper rows ys plots)
    (cond
     ((empty? rows) (reverse plots))
     (#t (helper (cdr rows) (cdr ys)
		 (cons (plot-a-year (car rows) (car ys))
		       plots)))))
  (helper table y-seq '()))
    
(parameterize
    ((plot-title "Harada _水俣病_ p.11 Fishery Decline '53-'56")
     (plot-x-axis? #f)
     (plot-x-far-axis? #f)
     (plot-x-label #f)
     (plot-y-axis? #f)
     (plot-y-far-axis? #f)
     (plot-y-label #f)
     (plot-width 600)
     (plot-height 1200)
      )
  (plot (plot-years years-alst)
   ;(plot-a-fish (second (second years-alst)) 2)
       	#:x-min -20 #:x-max 120
	#:y-min .5 #:y-max 4.5
        #:out-file "year-rows-HM-p11-4rkt.png"
        #:out-kind 'png))

Data Table

(from a page of html from org-mode)

Earlier Attempt with same data

#DrRacket #DataVisualization #Minamata #HaradaMasazumi #MasazumiHarada #水俣病 #原田正純 #データビジュアリゼーション