|
| 1 | +library(dplyr) |
| 2 | +library(palmerpenguins) |
| 3 | +library(ggplot2) |
| 4 | + |
| 5 | +# Load data |
| 6 | + |
| 7 | +data(penguins) |
| 8 | + |
| 9 | +penguins <- penguins |> |
| 10 | + filter(!is.na(sex)) |
| 11 | + |
| 12 | +# Default plot |
| 13 | + |
| 14 | +ggplot(penguins, aes(body_mass_g, |
| 15 | + bill_depth_mm, |
| 16 | + color = species)) + |
| 17 | + geom_point() |
| 18 | + |
| 19 | +# Themes #### |
| 20 | + |
| 21 | +ggplot(penguins, aes(body_mass_g, |
| 22 | + bill_depth_mm, |
| 23 | + color = species)) + |
| 24 | + geom_point() + |
| 25 | + theme_bw() |
| 26 | + |
| 27 | +ggplot(penguins, aes(body_mass_g, |
| 28 | + bill_depth_mm, |
| 29 | + color = species)) + |
| 30 | + geom_point() + |
| 31 | + theme_void() |
| 32 | + |
| 33 | +?theme_void |
| 34 | + |
| 35 | +# Modifying theme elements #### |
| 36 | + |
| 37 | +ggplot(penguins, aes(body_mass_g, |
| 38 | + bill_depth_mm, |
| 39 | + color = species)) + |
| 40 | + geom_point() + |
| 41 | + theme(panel.background = element_blank()) |
| 42 | + |
| 43 | + |
| 44 | +ggplot(penguins, aes(body_mass_g, |
| 45 | + bill_depth_mm, |
| 46 | + color = species)) + |
| 47 | + geom_point() + |
| 48 | + theme(panel.background = element_blank(), |
| 49 | + panel.grid.major = element_line(color = "black", linewidth = .5)) |
| 50 | + |
| 51 | + |
| 52 | +ggplot(penguins, aes(body_mass_g, |
| 53 | + bill_depth_mm, |
| 54 | + color = species)) + |
| 55 | + geom_point() + |
| 56 | + theme(panel.background = element_blank()) + |
| 57 | + theme(panel.grid.major = element_line(color = "black", linewidth = .5)) + |
| 58 | + theme(panel.grid.major.x = element_line(color = "red", linewidth = .1)) |
| 59 | + |
| 60 | +# Facetting #### |
| 61 | + |
| 62 | +ggplot(penguins, aes(body_mass_g, |
| 63 | + bill_depth_mm, |
| 64 | + color = species)) + |
| 65 | + geom_point() + |
| 66 | + theme(panel.background = element_blank()) + |
| 67 | + facet_wrap(vars(island)) |
| 68 | + |
| 69 | + |
| 70 | +ggplot(penguins, aes(body_mass_g, |
| 71 | + bill_depth_mm, |
| 72 | + color = species)) + |
| 73 | + geom_point() + |
| 74 | + theme(panel.background = element_blank()) + |
| 75 | + facet_wrap(vars(island), |
| 76 | + ncol = 1) |
| 77 | + |
| 78 | + |
| 79 | +ggplot(penguins, aes(body_mass_g, |
| 80 | + bill_depth_mm, |
| 81 | + color = species)) + |
| 82 | + geom_point() + |
| 83 | + theme(panel.background = element_blank()) + |
| 84 | + facet_grid(rows = vars(island), |
| 85 | + cols = vars(sex)) |
| 86 | + |
| 87 | +ggplot(penguins, aes(body_mass_g, |
| 88 | + bill_depth_mm, |
| 89 | + color = species)) + |
| 90 | + geom_point() + |
| 91 | + theme(panel.background = element_blank()) + |
| 92 | + facet_grid(rows = vars(island), |
| 93 | + cols = vars(sex), |
| 94 | + switch = "y") |
| 95 | + |
| 96 | +ggplot(penguins, aes(body_mass_g, |
| 97 | + bill_depth_mm, |
| 98 | + color = species)) + |
| 99 | + geom_point() + |
| 100 | + theme(panel.background = element_blank()) + |
| 101 | + facet_grid(rows = vars(island), |
| 102 | + cols = vars(sex), |
| 103 | + switch = "y", |
| 104 | + scales = "free_y") |
| 105 | + |
| 106 | +# Modifying facet themes |
| 107 | + |
| 108 | +ggplot(penguins, aes(body_mass_g, |
| 109 | + bill_depth_mm, |
| 110 | + color = species)) + |
| 111 | + geom_point() + |
| 112 | + theme(panel.background = element_blank()) + |
| 113 | + facet_grid(rows = vars(island), |
| 114 | + cols = vars(sex), |
| 115 | + switch = "y") + |
| 116 | + theme(strip.background = element_blank()) |
| 117 | + |
| 118 | +ggplot(penguins, aes(body_mass_g, |
| 119 | + bill_depth_mm, |
| 120 | + color = species)) + |
| 121 | + geom_point() + |
| 122 | + theme(panel.background = element_blank()) + |
| 123 | + facet_grid(rows = vars(island), |
| 124 | + cols = vars(sex), |
| 125 | + switch = "y") + |
| 126 | + theme(legend.position = "bottom") |
| 127 | + |
| 128 | + |
| 129 | +# Counterintuitive marks and channels! #### |
| 130 | + |
| 131 | +ggplot(penguins, aes(sex, species, size = body_mass_g)) + |
| 132 | + geom_jitter() + |
| 133 | + theme(panel.background = element_blank()) |
| 134 | + |
| 135 | + |
| 136 | +# Better marks and channels #### |
| 137 | + |
| 138 | +ggplot(penguins, aes(body_mass_g)) + |
| 139 | + geom_histogram() + |
| 140 | + theme(panel.background = element_blank()) + |
| 141 | + facet_grid(rows = vars(species), |
| 142 | + cols = vars(sex), |
| 143 | + switch = "y") |
| 144 | + |
| 145 | +# Marks and channels options |
| 146 | + |
| 147 | +ggplot(penguins, aes(body_mass_g, |
| 148 | + bill_depth_mm, |
| 149 | + shape = species)) + |
| 150 | + geom_point() + |
| 151 | + theme(panel.background = element_blank()) |
| 152 | + |
| 153 | +ggplot(penguins, aes(body_mass_g, |
| 154 | + bill_depth_mm, |
| 155 | + color = species)) + |
| 156 | + geom_point() + |
| 157 | + theme(panel.background = element_blank()) |
| 158 | + |
| 159 | +# cols4all |
| 160 | + |
| 161 | +install.packages("cols4all", dependencies = TRUE) |
| 162 | + |
| 163 | +library(cols4all) |
| 164 | + |
| 165 | +c4a_gui() |
| 166 | + |
| 167 | +ggplot(penguins, aes(body_mass_g, |
| 168 | + bill_depth_mm, |
| 169 | + color = species)) + |
| 170 | + geom_point() + |
| 171 | + theme(panel.background = element_blank()) + |
| 172 | + scale_color_discrete_c4a_cat(palette = "carto.vivid") |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | +# esquisse #### |
| 177 | + |
| 178 | +install.packages("esquisse") |
| 179 | + |
| 180 | +esquisse::esquisser(penguins) |
| 181 | + |
0 commit comments