clojisr-examples.graph-gallery.density

clojisr-examples.graph-gallery.density - created by notespace, Thu Jul 09 12:06:00 CEST 2020.

R Graph Gallery - Density plot

Code from project by Yan Holtz

You can find here only translated code, please refer original text

Setup

(require '[clojisr.v1.r :as r :refer [r+ r* r r->clj clj->r bra colon]]
          '[clojisr.v1.require :refer [require-r]]
          '[clojisr.v1.applications.plotting :refer [plot->file]])
(require-r '[base :as base :refer [$ <-]]
            '[utils :as u]
            '[stats :as stats]
            '[graphics :as g]
            '[grDevices :as dev]
            '[tidyverse]
            '[dplyr :as dplyr]
            '[tidyr :as tidyr]
            '[ggplot2 :as gg]
            '[ggExtra :as gge]
            '[viridis :as viridis]
            '[extrafont]
            '[hrbrthemes :as th]
            '[datasets :refer :all])

WARNING: To use hrbrthemes you may want to:

  1. Install Arial Narrow or Roboto Condensed fonts.
  2. Register system fonts with extrafont::font_import() or (r.extrafont/font_import)
  3. Fix font database as described in here
  4. Call hrbrthemes::import_roboto_condensed() or (th/import_roboto_condensed)
  5. Restart session
(r.extrafont/loadfonts :quiet true)
(base/set-seed 7337)

GGPlot2

Most basic

(def data
   (u/read-table
     "https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/1_OneNum.csv"
     :header
     true))
(plot->file
   (str target-path "a.png")
   (r+ (gg/ggplot (dplyr/filter data '(< price 300)))
       (gg/aes :x 'price)
       (gg/geom_density :fill "#69b3a2" :color "#e9ecef" :alpha 0.8)))

Custom appearance

(plot->file
   (str target-path "b.png")
   (r+ (gg/ggplot (dplyr/filter data '(< price 300)))
       (gg/aes :x 'price)
       (gg/geom_density :fill "#69b3a2" :color "#e9ecef" :alpha 0.8)
       (gg/ggtitle "Night price distribution of Airbnb appartements")
       (th/theme_ipsum_rc)))

Mirror density chart

(plot->file
   (str target-path "c.png")
   (let [data (base/data-frame :var1 (stats/rnorm 1000)
                               :var2 (stats/rnorm 1000 :mean 2))]
     (r+ (gg/ggplot data (gg/aes :x 'x))
         (gg/geom_density (gg/aes :x 'var1 :y '..density..)
                          :fill
                          "#69b3a2")
         (gg/geom_label (gg/aes :x 4.5 :y 0.25 :label "variable1")
                        :color
                        "#69b3a2")
         (gg/geom_density (gg/aes :x 'var2 :y '-..density..)
                          :fill
                          "#404080")
         (gg/geom_label (gg/aes :x 4.5 :y -0.25 :label "variable2")
                        :color
                        "#404080")
         (th/theme_ipsum_rc)
         (gg/xlab "value of x"))))

Multi density chart

(plot->file (str target-path "d.png")
             (r+ (gg/ggplot :data
                            gg/diamonds
                            (gg/aes :x 'price :group 'cut :fill 'cut))
                 (gg/geom_density :adjust 1.5)
                 (th/theme_ipsum_rc)))
(plot->file (str target-path "e.png")
             (r+ (gg/ggplot :data
                            gg/diamonds
                            (gg/aes :x 'price :group 'cut :fill 'cut))
                 (gg/geom_density :adjust 1.5 :alpha 0.4)
                 (th/theme_ipsum_rc)))

Multi density with annotation

(def data
   (->
     "https://raw.githubusercontent.com/zonination/perceptions/master/probly.csv"
     (u/read-table :header true :sep ",")
     (tidyr/gather :key "text" :value "value")
     (dplyr/mutate :text '(gsub "\\\\." " " text))
     (dplyr/mutate :value '(round (as.numeric value) 0))
     (dplyr/filter '(%in% text
                          ["Almost No Chance" "About Even" "Probable"
                           "Almost Certainly"]))))
(plot->file
   (str target-path "f.png")
   (let [annot (base/data-frame :text ["Almost No Chance" "About Even"
                                       "Probable" "Almost Certainly"]
                                :x [5 53 65 79]
                                :y [0.15 0.4 0.06 0.1])]
     (r+ (gg/ggplot data (gg/aes :x 'value :color 'text :fill 'text))
         (gg/geom_density :alpha 0.6)
         (viridis/scale_fill_viridis :discrete true)
         (viridis/scale_color_viridis :discrete true)
         (gg/geom_text :data
                       annot
                       (gg/aes :x 'x :y 'y :label 'text :color 'text)
                       :hjust 0
                       :size 4.5)
         (th/theme_ipsum_rc)
         (gg/theme :legend.position "none")
         (gg/xlab "")
         (gg/ylab "Assigned Probability (%)"))))

Small multiple

(plot->file (str target-path "g.png")
             (r+ (gg/ggplot :data
                            gg/diamonds
                            (gg/aes :x 'price :group 'cut :fill 'cut))
                 (gg/geom_density :adjust 1.5)
                 (th/theme_ipsum_rc)
                 (gg/facet_wrap '(formula nil cut))
                 (gg/theme :legend.position "none"
                           :panel.spacing (gg/unit 0.1 "lines")
                           :axis.ticks.x (gg/element_blank))))

Stacked density chart

(plot->file (str target-path "h.png")
             (r+ (gg/ggplot :data
                            gg/diamonds
                            (gg/aes :x 'price :group 'cut :fill 'cut))
                 (gg/geom_density :adjust 1.5 :position "fill")
                 (th/theme_ipsum_rc)))

Marginal distribution

(u/head mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
(def marginal-plot
   (r+ (gg/ggplot mtcars (gg/aes :x 'wt :y 'mpg :color 'cyl :size 'cyl))
       (gg/geom_point)
       (gg/theme :legend.position "none")))
(plot->file (str target-path "i.png")
             (gge/ggMarginal marginal-plot :type "density"))
(plot->file
   (str target-path "j.png")
   (gge/ggMarginal marginal-plot :margins "x" :color "purple" :size 4))

clojisr-examples.graph-gallery.density - created by notespace, Thu Jul 09 12:06:00 CEST 2020.