library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
# Paste your string of values into the parentheses for each color
red <- c(0,208,1,58,2,127,3,58,4,65,5,151,6,83,7,243,8,153,9,237,10,233,11,164,12,218,13,185,14,166,15,197,16,183,17,104,18,249,19,108,20,126,21,130,22,228,23,156,24,187,25,203,26,186,27,164,28,157,29,199,30,194,31,200,32,217,33,205,34,192,35,202,36,145,37,202,38,187,39,203,40,109,41,139,42,175,43,207,44,212,45,204,46,167,47,155,48,171,49,118,50,149,51,182,52,228,53,132,54,217,55,221,56,191,57,50,58,187,59,129,60,68,61,204,62,91,63,233,64,187,65,154,66,141,67,157,68,127,69,184)
green <- c(0,191,1,50,2,96,3,46,4,49,5,138,6,63,7,176,8,117,9,148,10,224,11,115,12,177,13,137,14,121,15,139,16,150,17,81,18,229,19,72,20,88,21,99,22,210,23,90,24,121,25,165,26,158,27,129,28,124,29,170,30,175,31,159,32,201,33,171,34,202,35,205,36,119,37,191,38,165,39,176,40,93,41,121,42,138,43,193,44,206,45,167,46,138,47,128,48,130,49,77,50,100,51,97,52,189,53,93,54,179,55,187,56,148,57,30,58,149,59,96,60,71,61,175,62,74,63,172,64,153,65,97,66,95,67,117,68,94,69,135)
blue <- c(0,176,1,47,2,75,3,46,4,49,5,94,6,65,7,164,8,90,9,105,10,239,11,98,12,157,13,115,14,96,15,139,16,123,17,72,18,206,19,63,20,72,21,84,22,191,23,59,24,72,25,139,26,142,27,115,28,110,29,159,30,169,31,128,32,189,33,156,34,205,35,206,36,107,37,180,38,148,39,165,40,86,41,111,42,131,43,174,44,196,45,140,46,123,47,112,48,113,49,66,50,86,51,61,52,180,53,85,54,171,55,160,56,126,57,34,58,127,59,79,60,75,61,156,62,71,63,149,64,132,65,71,66,65,67,113,68,85,69,69)
# create R, G, B variables to use later based on values pasted above, removing indices
R <- red[c(FALSE, TRUE)]
G <- green[c(FALSE, TRUE)]
B <- blue[c(FALSE, TRUE)]
# Create a data frame of the RGB values
df <- tibble(R,G,B)
# make a version of the data frame with hex_codes
df_hex <- df %>% mutate(hex_code = rgb(R/255,G/255,B/255))
#create a 3-D scatterplot of the data
plot_ly(df_hex, x = ~R, y = ~G, z= ~B, marker = list(color = ~hex_code)) %>%
add_markers() %>%
layout(scene = list(xaxis = list(title = 'Red', nticks = 4, range = list(0,255)),
yaxis = list(title = 'Green', nticks = 4, range = list(0,255)),
zaxis = list(title = 'Blue', nticks = 4, range = list(0,255))))
#```{r} # Paste your string of values into the parentheses for each color red <- c(0,170,1,69,2,88,3,754,205,5,235,6,65,7,152,8,200,9,203,10,152,11,215,12,161,13,218,14,195,15,224,16,175,17,167,18,202,19,209,20,177,21,203,22,172,23,151,24,157,25,175,26,216,27,154)
green <- c(0,143,1,59,2,72,3,60,4,163,5,220,6,53,7,116,8,140,9,157,10,116,11,194,12,128,13,210,14,163,15,209,16,144,17,136,18,193,19,195,20,148,21,187,22,138,23,103,24,117,25,138,26,146,27,91)
blue <- c(0,122,1,59,2,63,3,53,4,141,5,191,6,48,7,82,8,122,9,132,10,96,11,176,12,107,13,182,14,117,15,170,16,120,17,109,18,182,19,179,20,131,21,174,22,111,23,94,24,104,25,92,26,10)
R <- red[c(FALSE, TRUE)] G <- green[c(FALSE, TRUE)] B <- blue[c(FALSE, TRUE)]
df <- tibble(R,G,B)
df_hex <- df %>% mutate(hex_code = rgb(R/255,G/255,B/255))
#create a 3-D scatterplot of the data plot_ly(df_hex, x = ~R, y = ~G, z= ~B, marker = list(color = ~hex_code)) %>% add_markers() %>% layout(scene = list(xaxis = list(title = ‘Red’, nticks = 4, range = list(0,255)), yaxis = list(title = ‘Green’, nticks = 4, range = list(0,255)), zaxis = list(title = ‘Blue’, nticks = 4, range = list(0,255)))) #```
# cluster data with kmeans
df$cluster <- factor(kmeans(df,4)$cluster)
# view a small sample of data to see how it worked
sample_n(df, 10)
## # A tibble: 10 × 4
## R G B cluster
## <dbl> <dbl> <dbl> <fct>
## 1 129 96 79 2
## 2 187 149 127 3
## 3 185 137 115 3
## 4 197 139 139 3
## 5 65 49 49 4
## 6 199 170 159 1
## 7 118 77 66 4
## 8 127 96 75 2
## 9 149 100 86 2
## 10 228 210 191 1
# create 3-D scatterplot color-coded by cluster
plot_ly(df, x=~R, y=~G, z=~B, color=~cluster) %>% add_markers(size=1.5)
# create new data frame grouped by cluster with average colors for each skin tone cluster
df_clust <- df %>% group_by(cluster) %>% mutate(hex_avg = rgb(mean(R)/255, mean(G)/255, mean(B)/255)) %>% group_by(cluster, hex_avg) %>% summarize()
## `summarise()` has grouped output by 'cluster'. You can override using the
## `.groups` argument.
# create color palate
hex_pal <- df_clust$hex_avg
# create a bar graph of the clusters
df %>% group_by(cluster) %>%
ggplot(aes(cluster, fill=cluster)) +
geom_bar() + scale_fill_manual(values=hex_pal) +
labs(title = "Frequency of Skin Tones", x = "Skin Tone Cluster", y = "Frequency") +
theme_minimal()