-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
205 lines (131 loc) · 5.4 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# geopalette <a href='https://github.com/baynec2/geopalette'><img src='man/hex-geopalette.png' align="right" height="139" /></a>
<!-- badges: start -->
[![R-CMD-check](https://github.com/baynec2/geopalette/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/baynec2/geopalette/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
The goal of geopalette is to provide a fun new way to make color palletes based on a user specified location! In short, this package contains everything necessary to pull down satellite information from a given location, extract the color data from it, and then make a color palette.
Is this necessary, or the best way to choose a color palette? No of course not, but it might be kinda cool!
## Installation
You can install the development version of geopalette like so:
``` r
devtools::install_github(baynec2/geopalette)
```
## Examples
```{r example}
library(geopalette)
## basic example code
```
First, lets look at the colors from the town I grew up in, Culpeper VA.
To get the pallete we need to first build the right url that can get the desired image from the google API. We can do this using google_image_request_url(). After that, we can read the image using read_image_url(). Then we determine the unique_colors(), sort the colors, and finally plot.
```{r}
library(ggplot2)
culpeper = google_image_request_url("Culpeper,VA","city") %>%
read_image_url() %>%
determine_unique_colors() %>%
sort_colors() %>%
plot_unique_colors()+
ggtitle("Culpeper")
culpeper
```
What if you wanted to visually inspect the image that the pallete is extracted from? You could do that using the show_image function as shown below.
```{r}
google_image_request_url("Culpeper,VA","city") %>%
read_image_url() %>%
show_image()
```
I live in San Diego now. As another example, lets look at the colors found there.
```{r}
Sandiego = google_image_request_url("SanDiego,CA","city") %>%
read_image_url() %>%
determine_unique_colors() %>%
sort_colors() %>%
plot_unique_colors()+
ggtitle("Sandiego")
Sandiego
```
Perhaps not surprisingly, the colors found in San diego are very different looking from those in Culpeper.
While these plots represent the unique colors found at each location, we still want to be able to create a palette from them. This should work by sorting the colors by similarity and then grouping into 8 distinct groups to form the palette.
```{r}
culpeper = google_image_request_url("Culpeper,VA","city") %>%
read_image_url() %>%
determine_unique_colors() %>%
sort_colors()
culpeper_pal = create_palette(culpeper)
plot_unique_colors(culpeper_pal(8))
```
Now let's look at the same thing when using san diego as a source image.
```{r}
sandiego = google_image_request_url("Sandiego,CA","city") %>%
read_image_url() %>%
determine_unique_colors() %>%
sort_colors()
sandiego_pal = create_palette(sandiego)
plot_unique_colors(sandiego_pal(8))
```
## BaseR Plotting
You can use these palettes with base R graphics like so:
```{r}
plot(iris$Petal.Length, iris$Petal.Width, pch=21,bg = sandiego_pal(3)[iris$Species])
legend(1, 2.5, legend=unique(iris$Species),
col=unique(sandiego_pal(3)[iris$Species]), lty=1:2, cex=0.8)
```
## GGplot2
There is support for ggplot2 graphics through the scale_fill_geopalette() and the scale_color_geopalette() functions.
For plots that need to be colored by fill, such as bar graphs, use the scale_fill_geopalette() function like so:
```{r}
p1 = mtcars %>%
as.data.frame() %>%
tibble::rownames_to_column("car") %>%
ggplot(aes(car,mpg,fill = car))+
geom_col()+
scale_fill_geopalette("Culpeper,VA",zoom = "city")+
theme(axis.text.x = element_blank())
p1
```
Let's do the same for San Diego just to see what it looks like.
```{r}
p1 = mtcars %>%
as.data.frame() %>%
tibble::rownames_to_column("car") %>%
ggplot(aes(car,mpg,fill = car))+
geom_col()+
scale_fill_geopalette("SanDiego,CA",zoom = "city")+
theme(axis.text.x = element_blank())
p1
```
For plots that have colored points instead of filled, like a scatter plot, we can use scale_color_geopalette() as shown below. If the values are continuous instead of discrete, we need to change the discrete argument to FALSE.
```{r}
p1 = mtcars %>%
as.data.frame() %>%
tibble::rownames_to_column("car") %>%
ggplot(aes(hp,mpg,color = mpg))+
geom_point()+
scale_color_geopalette("Culpeper,VA",zoom = "city",discrete = FALSE)
p1
```
Lastly, lets see what this looks like for san diego.
```{r}
p1 = mtcars %>%
as.data.frame() %>%
tibble::rownames_to_column("car") %>%
ggplot(aes(hp,mpg,color = mpg))+
geom_point()+
scale_color_geopalette("SanDiego,CA",zoom = "city",discrete = FALSE)
p1
```
## Future work
There are a few problems with the current iteration of geopallete that I hope to fix in the future if I have time. These are listed below:
1. Often times not a very good separation of colors in the palette. When plotting, it can be hard to distinuguish one group from another.
2. Continuous color palettes are often not intuitivly sorted (sorting colors is not a trivial task).
3. White can be in the palette, causing these points to be non visable.