├── .DS_Store ├── Austin ├── Austin Rent Analysis.R ├── Imagine_Austin_Indicators.csv ├── formattable.R ├── formattable.gif └── readme.md ├── Heatmap ├── incidentHeatmap.R └── readme.md ├── Holidays ├── background.jpg ├── Holidays.csv ├── animation.Rmd ├── animation_files │ └── figure-markdown_github │ │ ├── unnamed-chunk-3-1.png │ │ ├── unnamed-chunk-4-1.png │ │ └── unnamed-chunk-6-1.gif └── readme.md ├── IWD_2018 ├── 2015_equalityIndex.csv ├── EUEqualityData.R ├── Gender_equality_over_time.csv ├── IWD_2018.md ├── NUTS_2013_01M_SH │ ├── NUTS_2013_UML.png │ ├── data │ │ ├── NUTS_AT_2013.cpg │ │ ├── NUTS_AT_2013.csv │ │ ├── NUTS_AT_2013.dbf │ │ ├── NUTS_AT_2013.dbf.xml │ │ ├── NUTS_BN_01M_2013.dbf │ │ ├── NUTS_BN_01M_2013.prj │ │ ├── NUTS_BN_01M_2013.sbn │ │ ├── NUTS_BN_01M_2013.sbx │ │ ├── NUTS_BN_01M_2013.shp │ │ ├── NUTS_BN_01M_2013.shp.xml │ │ ├── NUTS_BN_01M_2013.shx │ │ ├── NUTS_JOIN_LI_2013.cpg │ │ ├── NUTS_JOIN_LI_2013.dbf │ │ ├── NUTS_JOIN_LI_2013.prj │ │ ├── NUTS_JOIN_LI_2013.sbn │ │ ├── NUTS_JOIN_LI_2013.sbx │ │ ├── NUTS_JOIN_LI_2013.shp │ │ ├── NUTS_JOIN_LI_2013.shp.xml │ │ ├── NUTS_JOIN_LI_2013.shx │ │ ├── NUTS_LB_2013.cpg │ │ ├── NUTS_LB_2013.dbf │ │ ├── NUTS_LB_2013.prj │ │ ├── NUTS_LB_2013.sbn │ │ ├── NUTS_LB_2013.sbx │ │ ├── NUTS_LB_2013.shp │ │ ├── NUTS_LB_2013.shp.xml │ │ ├── NUTS_LB_2013.shx │ │ ├── NUTS_RG_01M_2013.dbf │ │ ├── NUTS_RG_01M_2013.prj │ │ ├── NUTS_RG_01M_2013.sbn │ │ ├── NUTS_RG_01M_2013.sbx │ │ ├── NUTS_RG_01M_2013.shp │ │ ├── NUTS_RG_01M_2013.shp.xml │ │ ├── NUTS_RG_01M_2013.shx │ │ ├── NUTS_RG_BN_01M_2013.dbf │ │ ├── NUTS_RG_BN_01M_2013.dbf.xml │ │ ├── NUTS_SEPA_LI_2013.cpg │ │ ├── NUTS_SEPA_LI_2013.dbf │ │ ├── NUTS_SEPA_LI_2013.prj │ │ ├── NUTS_SEPA_LI_2013.sbn │ │ ├── NUTS_SEPA_LI_2013.sbx │ │ ├── NUTS_SEPA_LI_2013.shp │ │ ├── NUTS_SEPA_LI_2013.shp.xml │ │ └── NUTS_SEPA_LI_2013.shx │ ├── metadata │ │ ├── Metadata_NUTS_2013.pdf │ │ ├── Metadata_NUTS_2013.xml │ │ ├── NUTS_AT_2013.xml │ │ ├── NUTS_BN_01M_2013.xml │ │ ├── NUTS_LB_2013.xml │ │ ├── NUTS_RG_01M_2013.xml │ │ ├── NUTS_RG_BN_01M_2013.xml │ │ └── NUTS_SEPA_LI_2013.xml │ ├── readme.rtf │ └── readme.txt └── readme.md ├── Patchwork ├── .DS_Store ├── miniPatchworkVerticalRectangle.png ├── miniPatchworkWideRectangle.png ├── patchwork.Rmd ├── patchworkTwitter.png ├── patchworkTwitterFrame.png ├── patchwork_files │ └── figure-markdown_github │ │ ├── unnamed-chunk-10-1.png │ │ ├── unnamed-chunk-10-2.png │ │ ├── unnamed-chunk-10-3.png │ │ ├── unnamed-chunk-10-4.png │ │ ├── unnamed-chunk-11-1.png │ │ ├── unnamed-chunk-11-2.png │ │ ├── unnamed-chunk-13-1.png │ │ ├── unnamed-chunk-13-2.png │ │ ├── unnamed-chunk-5-1.png │ │ ├── unnamed-chunk-7-1.png │ │ ├── unnamed-chunk-7-2.png │ │ └── unnamed-chunk-9-1.png ├── rHexAmeliaMN.png ├── readme.md ├── top10KTwitter.png ├── top10KTwitterFrame.png └── twitter_post.png ├── README.md ├── Templates └── eventDocumentation.md ├── datatable ├── 2015_Annual_Performance_Report_Key_Indicators.csv └── readme.md ├── files ├── Think.R ├── cloud.csv ├── garfield.csv ├── ibm_think_tile.csv ├── little_heart_cloud.svg ├── lmd-loves-cloud.csv └── think_image_coords_final.csv ├── gghighlight ├── ggplot2_and_gghighlight.R └── readme.md ├── ggmap ├── .DS_Store ├── Updated-ggmap-08_30_2019__No_Key_files │ ├── .DS_Store │ └── figure-markdown_github │ │ ├── unnamed-chunk-10-1.png │ │ ├── unnamed-chunk-11-1.png │ │ ├── unnamed-chunk-12-1.png │ │ ├── unnamed-chunk-13-1.png │ │ ├── unnamed-chunk-14-1.png │ │ ├── unnamed-chunk-14-2.png │ │ ├── unnamed-chunk-4-1.png │ │ ├── unnamed-chunk-5-1.png │ │ ├── unnamed-chunk-6-1.png │ │ ├── unnamed-chunk-7-1.png │ │ ├── unnamed-chunk-8-1.png │ │ └── unnamed-chunk-9-1.png ├── i2Sample.csv ├── incidentMap.R ├── n.csv └── readme.md ├── ggplotly ├── Median_Earnings_of_Creative_Sector_Occupations__CLL.B.1.csv ├── plotlyExample_files │ └── figure-markdown_github │ │ ├── unnamed-chunk-3-1.png │ │ └── unnamed-chunk-4-1.png ├── readme.md └── readme_files │ └── figure-markdown_github │ ├── unnamed-chunk-1-1.png │ ├── unnamed-chunk-1-2.png │ ├── unnamed-chunk-2-1.png │ └── unnamed-chunk-3-1.png ├── gtrendsR ├── .DS_Store ├── Bachelor trends ├── bachelorListing.csv ├── gtrends.Rmd ├── gtrends.md ├── gtrends_files │ ├── .DS_Store │ └── figure-markdown_github │ │ ├── unnamed-chunk-3-1.png │ │ ├── unnamed-chunk-5-1.png │ │ ├── unnamed-chunk-6-1.gif │ │ └── unnamed-chunk-9-1.png ├── images │ ├── Arie.png │ ├── BIP.png │ ├── Becca.png │ ├── Colton.png │ ├── Hannah.png │ ├── Nick.png │ └── Rachel.png ├── readme.Rmd ├── readme.md └── readme_files │ └── figure-markdown_github │ ├── unnamed-chunk-3-1.png │ ├── unnamed-chunk-5-1.png │ ├── unnamed-chunk-6-1.gif │ └── unnamed-chunk-9-1.png ├── iconmap ├── .DS_Store ├── BasketballCourts.csv ├── LondonParkAmenities.csv ├── MasterList.csv ├── babyPoolFinal.png ├── duckie.png ├── iconMap_NoKey_files │ └── figure-markdown_github │ │ ├── unnamed-chunk-2-1.png │ │ ├── unnamed-chunk-3-1.png │ │ ├── unnamed-chunk-5-1.png │ │ ├── unnamed-chunk-6-1.png │ │ └── unnamed-chunk-7-1.png ├── readme.md ├── splash.png ├── splashFinal.png ├── swimmer.png └── swimmerFinal.png ├── inspectdf ├── readme.Rmd ├── readme.md └── readme_files │ └── figure-markdown_github │ ├── unnamed-chunk-10-1.png │ ├── unnamed-chunk-11-1.png │ ├── unnamed-chunk-12-1.png │ ├── unnamed-chunk-13-1.png │ ├── unnamed-chunk-14-1.png │ ├── unnamed-chunk-15-1.png │ ├── unnamed-chunk-16-1.png │ ├── unnamed-chunk-2-1.png │ ├── unnamed-chunk-2-2.png │ ├── unnamed-chunk-3-1.png │ ├── unnamed-chunk-4-1.png │ ├── unnamed-chunk-5-1.png │ ├── unnamed-chunk-6-1.png │ ├── unnamed-chunk-7-1.png │ ├── unnamed-chunk-8-1.png │ └── unnamed-chunk-9-1.png ├── lacroix ├── FIZZ_Stock.csv ├── lacroix_animation.R └── readme.md ├── sets ├── Toronto_Seniors_Survey_2017_Results.csv ├── readme.md ├── readme_files │ └── figure-markdown_github │ │ ├── unnamed-chunk-1-1.png │ │ ├── unnamed-chunk-1-2.png │ │ ├── unnamed-chunk-10-1.png │ │ ├── unnamed-chunk-12-1.png │ │ ├── unnamed-chunk-2-1.png │ │ ├── unnamed-chunk-3-1.png │ │ ├── unnamed-chunk-5-1.png │ │ ├── unnamed-chunk-6-1.png │ │ ├── unnamed-chunk-7-1.png │ │ ├── unnamed-chunk-8-1.png │ │ └── unnamed-chunk-9-1.png └── seniorTransportation.csv ├── simple_eda ├── dataExplorerGifLg.gif ├── exploreData.Rmd ├── exploreData.nb.html └── readme.md ├── twitter_wordcloud ├── R_word_clouds.R ├── handmaiden.jpeg ├── handmaidens.jpg ├── handmaidens2.jpeg ├── readme.md ├── resistance.jpeg ├── sods18.jpg ├── sods18.png ├── spiceGirls.jpeg ├── sun2.jpg ├── sunglasses.jpg ├── wwlogo.jpg ├── wwlogowithname.jpg └── wwmaze2.jpg └── usmap ├── .DS_Store ├── .Rhistory ├── readme.Rmd ├── readme.md └── readme_files ├── .DS_Store └── figure-markdown_github ├── unnamed-chunk-4-1.png └── unnamed-chunk-5-1.png /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/.DS_Store -------------------------------------------------------------------------------- /Austin/Austin Rent Analysis.R: -------------------------------------------------------------------------------- 1 | 2 | #Install and Load Packages 3 | 4 | install.packages("ggplot2") 5 | install.packages("data.table") 6 | install.packages("tidyr") 7 | 8 | library(ggplot2) 9 | library(data.table) 10 | library(tidyr) 11 | 12 | #Download the Austin indicator data set 13 | #Original data set from: https://data.austintexas.gov/City-Government/Imagine-Austin-Indicators/apwj-7zty/data 14 | 15 | austinData= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Austin/Imagine_Austin_Indicators.csv', data.table=FALSE, header = TRUE, stringsAsFactors = FALSE) 16 | 17 | #Attach the column names 18 | attach(austinData) 19 | 20 | 21 | #Filter to include only Median Gross Rent 22 | aD2 <- austinData[`Indicator Name` == "Median Gross Rent", ] 23 | 24 | #Use gather function of tidyr for easier line graph plotting 25 | aD2 <- aD2 %>% 26 | gather(year, value, '2007':'2017') 27 | 28 | #Create a line graph 29 | 30 | p <- ggplot(aD2, aes(x=year, y=value, group=1)) + 31 | geom_line() + 32 | labs(x = "Median Gross Rent in Austin", 33 | y = "Year") + 34 | theme_bw() + 35 | theme_minimal() 36 | 37 | p 38 | 39 | #Export the new filtered and gathered data set 40 | write.csv(aD2,'aD2.csv') 41 | 42 | #Export the graph 43 | p + ggsave("aD2Plot.pdf") 44 | 45 | ###Export code for notebooks 46 | 47 | #export to csv 48 | write.csv(aD2,"aD2Data.csv") 49 | project$save_data('aD2Data.csv',"aD2Data.csv", overwrite=TRUE) 50 | 51 | 52 | #Export the graph 53 | p + ggsave("aD2LineGraph.pdf") 54 | project$save_data('aD2LineGraph.pdf',"aD2LineGraph.pdf",overwrite=TRUE) 55 | 56 | 57 | -------------------------------------------------------------------------------- /Austin/formattable.R: -------------------------------------------------------------------------------- 1 | #Great Resources on the formattable package 2 | #https://cran.r-project.org/web/packages/formattable/README.html 3 | #https://github.com/renkun-ken/formattable/issues/51 4 | #http://bioinfo.iric.ca/create-a-nice-looking-table-using-r/ (shows you how to create a number of formats yourself) 5 | #http://www.glyphicons.com/ 6 | 7 | #Data Source 8 | #https://data.austintexas.gov/City-Government/Imagine-Austin-Indicators/apwj-7zty 9 | 10 | install.packages("data.table") 11 | install.packages("dplyr") 12 | install.packages("formattable") 13 | install.packages("tidyr") 14 | 15 | #Load the libraries 16 | library(data.table) 17 | library(dplyr) 18 | library(formattable) 19 | library(tidyr) 20 | 21 | #Set a few color variables to make our table more visually appealing 22 | customGreen0 = "#DeF7E9" 23 | customGreen = "#71CA97" 24 | customRed = "#ff7f7f" 25 | 26 | #Download the Austin indicator data set 27 | #Original data set from: https://data.austintexas.gov/City-Government/Imagine-Austin-Indicators/apwj-7zty/data 28 | 29 | austinData= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Austin/Imagine_Austin_Indicators.csv', data.table=FALSE, header = TRUE, stringsAsFactors = FALSE) 30 | 31 | head(austinData) 32 | attach(austinData) 33 | 34 | #Modify the data set 35 | 36 | i1 <- austinData %>% 37 | filter(`Indicator Name` %in% 38 | c('Prevalence of Obesity', 'Prevalence of Tobacco Use', 39 | 'Prevalence of Cardiovascular Disease', 'Prevalence of Diabetes')) %>% 40 | select(c(`Indicator Name`, `2011`, `2012`, `2013`, `2014`, `2015`, `2016`)) %>% 41 | mutate (Average = round(rowMeans( 42 | cbind(`2011`, `2012`, `2013`, `2014`, `2015`, `2016`), na.rm=T),2), 43 | `Improvement` = round((`2011`-`2016`)/`2011`*100,2)) 44 | i1 45 | 46 | #0) Throw it in the formattable function 47 | formattable(i1) 48 | 49 | 50 | #1) First Data Table 51 | 52 | formattable(i1, 53 | align =c("l","c","c","c","c", "c", "c", "c", "r"), 54 | list(`Indicator Name` = formatter( 55 | "span", style = ~ style(color = "grey",font.weight = "bold")) 56 | )) 57 | 58 | 59 | #2) Add the color mapping for all 2011 to 2016. 60 | formattable(i1, align =c("l","c","c","c","c", "c", "c", "c", "r"), list( 61 | `Indicator Name` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 62 | `2011`= color_tile(customGreen, customGreen0), 63 | `2012`= color_tile(customGreen, customGreen0), 64 | `2013`= color_tile(customGreen, customGreen0), 65 | `2014`= color_tile(customGreen, customGreen0), 66 | `2015`= color_tile(customGreen, customGreen0), 67 | `2016`= color_tile(customGreen, customGreen0) 68 | )) 69 | 70 | 71 | #3) Add the color bar to the average column 72 | 73 | formattable(i1, align =c("l","c","c","c","c", "c", "c", "c", "r"), list( 74 | `Indicator Name` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 75 | `2011`= color_tile(customGreen, customGreen0), 76 | `2012`= color_tile(customGreen, customGreen0), 77 | `2013`= color_tile(customGreen, customGreen0), 78 | `2014`= color_tile(customGreen, customGreen0), 79 | `2015`= color_tile(customGreen, customGreen0), 80 | `2016`= color_tile(customGreen, customGreen0), 81 | `Average` = color_bar(customRed) 82 | )) 83 | 84 | 85 | #4) Add sign formatter to improvement over time 86 | 87 | #Create your own formatter as the examples given: 88 | #The one below is using the base of sign_formatter from the vignette: https://cran.r-project.org/web/packages/formattable/vignettes/formattable-data-frame.html 89 | #just a slight modification to have our color scheme and bolding 90 | #Bioinfo.irc.ca has some great examples too: http://bioinfo.iric.ca/create-a-nice-looking-table-using-r/ 91 | 92 | improvement_formatter <- 93 | formatter("span", 94 | style = x ~ style( 95 | font.weight = "bold", 96 | color = ifelse(x > 0, customGreen, ifelse(x < 0, customRed, "black")))) 97 | 98 | 99 | 100 | formattable(i1, align =c("l","c","c","c","c", "c", "c", "c", "r"), list( 101 | `Indicator Name` = 102 | formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 103 | `2011`= color_tile(customGreen, customGreen0), 104 | `2012`= color_tile(customGreen, customGreen0), 105 | `2013`= color_tile(customGreen, customGreen0), 106 | `2014`= color_tile(customGreen, customGreen0), 107 | `2015`= color_tile(customGreen, customGreen0), 108 | `2016`= color_tile(customGreen, customGreen0), 109 | `Average` = color_bar(customRed), 110 | `Improvement` = improvement_formatter 111 | )) 112 | 113 | 114 | 115 | #5) For improvement formatter add icons 116 | 117 | # Up and down arrow with greater than comparison from the vignette 118 | 119 | improvement_formatter <- formatter("span", 120 | style = x ~ style(font.weight = "bold", 121 | color = ifelse(x > 0, customGreen, ifelse(x < 0, customRed, "black"))), 122 | x ~ icontext(ifelse(x>0, "arrow-up", "arrow-down"), x) 123 | ) 124 | 125 | 126 | formattable(i1, align =c("l","c","c","c","c", "c", "c", "c", "r"), list( 127 | `Indicator Name` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 128 | `2011`= color_tile(customGreen, customGreen0), 129 | `2012`= color_tile(customGreen, customGreen0), 130 | `2013`= color_tile(customGreen, customGreen0), 131 | `2014`= color_tile(customGreen, customGreen0), 132 | `2015`= color_tile(customGreen, customGreen0), 133 | `2016`= color_tile(customGreen, customGreen0), 134 | `Average` = color_bar(customRed), 135 | `Improvement` = improvement_formatter 136 | )) 137 | 138 | ##OR 139 | #Thumbs up comparing to max value 140 | 141 | improvement_formatter <- formatter("span", 142 | style = x ~ style(font.weight = "bold", 143 | color = ifelse(x > 0, customGreen, ifelse(x < 0, customRed, "black"))), 144 | x ~ icontext(ifelse(x == max(x), "thumbs-up", ""), x) 145 | ) 146 | 147 | 148 | formattable(i1, align =c("l","c","c","c","c", "c", "c", "c", "r"), list( 149 | `Indicator Name` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 150 | `2011`= color_tile(customGreen, customGreen0), 151 | `2012`= color_tile(customGreen, customGreen0), 152 | `2013`= color_tile(customGreen, customGreen0), 153 | `2014`= color_tile(customGreen, customGreen0), 154 | `2015`= color_tile(customGreen, customGreen0), 155 | `2016`= color_tile(customGreen, customGreen0), 156 | `Average` = color_bar(customRed), 157 | `Improvement` = improvement_formatter 158 | )) 159 | 160 | #6) Add a star to the max value. Use if/else value = max(value) 161 | 162 | 163 | improvement_formatter <- formatter("span", 164 | style = x ~ style(font.weight = "bold", 165 | color = ifelse(x > 0, customGreen, ifelse(x < 0, customRed, "black"))), 166 | x ~ icontext(ifelse(x == max(x), "thumbs-up", ""), x) 167 | ) 168 | 169 | ## Based on Name 170 | 171 | formattable(i1, align =c("l","c","c","c","c", "c", "c", "c", "r"), list( 172 | `Indicator Name` = formatter("span", 173 | style = x ~ style(color = "gray"), 174 | x ~ icontext(ifelse(x == "Prevalence of Tobacco Use", "star", ""), x)), 175 | `2011`= color_tile(customGreen, customGreen0), 176 | `2012`= color_tile(customGreen, customGreen0), 177 | `2013`= color_tile(customGreen, customGreen0), 178 | `2014`= color_tile(customGreen, customGreen0), 179 | `2015`= color_tile(customGreen, customGreen0), 180 | `2016`= color_tile(customGreen, customGreen0), 181 | `Average` = color_bar(customRed), 182 | `Improvement` = improvement_formatter 183 | )) 184 | 185 | 186 | ##7) Compare column to column 187 | 188 | #Drop the rest and show just 2015 and 2016 189 | 190 | i2 <- austinData %>% 191 | filter(`Indicator Name` %in% c('Prevalence of Obesity', 'Prevalence of Tobacco Use', 'Prevalence of Cardiovascular Disease', 'Prevalence of Diabetes')) %>% 192 | select(c(`Indicator Name`, `2015`, `2016`)) 193 | 194 | head(i2) 195 | 196 | ## Again the x is removed b/c you need to reference two column values, so you need to list them explicitly 197 | 198 | formattable(i2, align =c("l","c","c"), list( 199 | `Indicator Name` = formatter("span", 200 | style = ~ style(color = "gray")), 201 | `2016`= formatter("span", style = ~ style(color = ifelse(`2016` >`2015`, "red", "green")), 202 | ~ icontext(ifelse(`2016` >`2015`,"arrow-up", "arrow-down"), `2016`)) 203 | 204 | )) 205 | 206 | ##8) Extras 207 | 208 | formattable(i1, align =c("l","c","c","c","c", "c", "c", "c", "r"), list( 209 | `Indicator Name` = formatter("span", 210 | style = ~ style(color = "gray"), 211 | ~ icontext(ifelse(`Improvement` > 0, "star", ""), `Indicator Name`)), 212 | `2011`= color_tile(customGreen, customGreen0), 213 | `2012`= color_tile(customGreen, customGreen0), 214 | `2013`= color_tile(customGreen, customGreen0), 215 | `2014`= color_tile(customGreen, customGreen0), 216 | `2015`= color_tile(customGreen, customGreen0), 217 | `2016`= color_tile(customGreen, customGreen0), 218 | `Average` = color_bar(customRed), 219 | `Improvement` = improvement_formatter 220 | )) 221 | 222 | ### Compare to an external value 223 | 224 | max = max(i1$Improvement) 225 | 226 | formattable(i1, align =c("l","c","c","c","c", "c", "c", "c", "r"), list( 227 | `Indicator Name` = formatter("span", 228 | style = ~ style(color = "gray"), 229 | ~ icontext(ifelse(`Improvement` == max, "star", ""), `Indicator Name`)), 230 | `2011`= color_tile(customGreen, customGreen0), 231 | `2012`= color_tile(customGreen, customGreen0), 232 | `2013`= color_tile(customGreen, customGreen0), 233 | `2014`= color_tile(customGreen, customGreen0), 234 | `2015`= color_tile(customGreen, customGreen0), 235 | `2016`= color_tile(customGreen, customGreen0), 236 | `Average` = color_bar(customRed), 237 | `Improvement` = improvement_formatter 238 | )) 239 | 240 | 241 | #### Make animation 242 | 243 | ## Help from here: https://cran.r-project.org/web/packages/magick/vignettes/intro.html#installing_magick 244 | 245 | install.packages("magick") 246 | library(magick) 247 | 248 | #Read in images 249 | 250 | d0 <- image_scale(image_read("i_datatable0.png"), "x150") 251 | d1 <- image_scale(image_read("i_datatable1.png"), "x150") 252 | d2 <- image_scale(image_read("i_datatable2.png"), "x150") 253 | d3 <- image_scale(image_read("i_datatable3.png"), "x150") 254 | d4 <- image_scale(image_read("i_datatable4.png"), "x150") 255 | d5 <- image_scale(image_read("i_datatable5.png"), "x150") 256 | d6 <- image_scale(image_read("i_datatable6.png"), "x150") 257 | d7 <- image_scale(image_read("i_datatable7.png"), "x150") 258 | 259 | 260 | #Image transition with no morph 261 | i <- image_animate(c(d0, d1, d2, d3,d4, d5, d6, d7,d7), fps = 1) 262 | image_write(i, "formattable.gif") 263 | i 264 | 265 | #Image transition with morph 266 | frames <- image_morph(c(dt1, dt2, dt3,dt4, dt6, dt7), frames = 15) 267 | ia1 <- image_animate(frames) 268 | image_write(ia1, "datatable.gif") -------------------------------------------------------------------------------- /Austin/formattable.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Austin/formattable.gif -------------------------------------------------------------------------------- /Austin/readme.md: -------------------------------------------------------------------------------- 1 | Exploring data available through [Austin Texas Data](https://data.austintexas.gov/) 2 | 3 | Contains [Austin Imagine Indicators Data](https://data.austintexas.gov/City-Government/Imagine-Austin-Indicators/apwj-7zty) 4 | 5 | Full tutorial here: [Create stylish tables in R using formattable](https://www.littlemissdata.com/blog/prettytables) 6 | 7 | -------------------------------------------------------------------------------- /Heatmap/incidentHeatmap.R: -------------------------------------------------------------------------------- 1 | #################### Import and Install Packages #################### 2 | 3 | install.packages("plyr") 4 | install.packages("lubridate") 5 | install.packages("ggplot2") 6 | install.packages("dplyr") 7 | 8 | 9 | library(plyr) 10 | library(lubridate) 11 | library(ggplot2) 12 | library(dplyr) 13 | #################### Set Variables and Import Data #################### 14 | 15 | #https://catalog.data.gov/dataset/seattle-police-department-911-incident-response-52779 16 | 17 | incidents <-read.table("https://data.seattle.gov/api/views/3k2p-39jp/rows.csv?accessType=DOWNLOAD", head=TRUE, sep=",", fill=TRUE, stringsAsFactors=F) 18 | 19 | #If the above data set is unavailable please use this code 20 | #df= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/ggmap/i2Sample.csv', stringsAsFactors = FALSE) 21 | #incidents <- df 22 | 23 | 24 | col1 = "#d8e1cf" 25 | col2 = "#438484" 26 | head(incidents) 27 | attach(incidents) 28 | str(incidents) 29 | 30 | #################### Transform #################### 31 | 32 | 33 | #Convert dates using lubridate 34 | 35 | incidents$ymd <-mdy_hms(Event.Clearance.Date) 36 | incidents$month <- month(incidents$ymd, label = TRUE) 37 | incidents$year <- year(incidents$ymd) 38 | incidents$wday <- wday(incidents$ymd, label = TRUE) 39 | incidents$hour <- hour(incidents$ymd) 40 | attach(incidents) 41 | head(incidents) 42 | 43 | #################### Heatmap Incidents Per Hour #################### 44 | 45 | #create summary table for heatmap - Day/Hour Specific 46 | 47 | dayHour <- ddply(incidents, c( "hour", "wday"), summarise, 48 | N = length(ymd) 49 | ) 50 | dayHour$wday <- factor(dayHour$wday, levels=rev(levels(dayHour$wday))) 51 | 52 | attach(dayHour) 53 | 54 | 55 | #overall summary 56 | ggplot(dayHour, aes(hour, wday)) + geom_tile(aes(fill = N),colour = "white", na.rm = TRUE) + 57 | scale_fill_gradient(low = col1, high = col2) + 58 | guides(fill=guide_legend(title="Total Incidents")) + 59 | theme_bw() + theme_minimal() + 60 | labs(title = "Histogram of Seattle Incidents by Day of Week and Hour", 61 | x = "Incidents Per Hour", y = "Day of Week") + 62 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 63 | 64 | 65 | 66 | #################### Heatmap Incidents Year and Month #################### 67 | 68 | #create summary table for heatmap - Month/Year Specific 69 | 70 | yearMonth <- ddply(incidents, c( "year", "month" ), summarise, 71 | N = length(ymd) 72 | ) 73 | 74 | yearMonth$month <- factor(summaryGroup$month, levels=rev(levels(summaryGroup$month))) 75 | 76 | attach(yearMonth) 77 | 78 | 79 | #overall summary 80 | ggplot(yearMonth, aes(year, month)) + geom_tile(aes(fill = N),colour = "white") + 81 | scale_fill_gradient(low = col1, high = col2) + 82 | guides(fill=guide_legend(title="Total Incidents")) + 83 | labs(title = "Histogram of Seattle Incidents by Year and Month", 84 | x = "Month", y = "Year") + 85 | theme_bw() + theme_minimal() + 86 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 87 | 88 | 89 | #################### Heatmap Incidents Per Hour by Incident Group #################### 90 | 91 | #create summary table for heatmap - Group Specific 92 | 93 | groupSummary <- ddply(incidents, c( "Event.Clearance.Group", "hour"), summarise, 94 | N = length(ymd) 95 | ) 96 | 97 | #overall summary 98 | ggplot(groupSummary, aes( hour,Event.Clearance.Group)) + geom_tile(aes(fill = N),colour = "white") + 99 | scale_fill_gradient(low = col1, high = col2) + 100 | guides(fill=guide_legend(title="Total Incidents")) + 101 | labs(title = "Histogram of Seattle Incidents by Event and Hour", 102 | x = "Hour", y = "Event") + 103 | theme_bw() + theme_minimal() + 104 | theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /Heatmap/readme.md: -------------------------------------------------------------------------------- 1 | A tutorial showing how to do time based heatmaps in R 2 | 3 | Tutorial available [here](https://www.littlemissdata.com/blog/heatmaps) 4 | -------------------------------------------------------------------------------- /Holidays/ background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Holidays/ background.jpg -------------------------------------------------------------------------------- /Holidays/animation.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Happy Holidays 2019" 3 | author: Laura Ellis 4 | date: Dec 24 2019 5 | output: 6 | md_document: 7 | variant: markdown_github 8 | --- 9 | 10 | # Load all packages 11 | 12 | ```{r} 13 | library(data.table) 14 | library(tidyverse) 15 | library(gganimate) 16 | library(ggimage) 17 | library(magick) 18 | library(gifski) 19 | ``` 20 | 21 | # Download the data 22 | 23 | Csv created with a tool called [coordinator](https://spotify.github.io/coordinator/), by [Aliza Aufrichtig](https://twitter.com/alizauf). It allows us to convert SVG graphics to data co-ordinates 24 | 25 | ```{r} 26 | df= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Holidays/Holidays.csv', stringsAsFactors = FALSE) 27 | ``` 28 | 29 | #Create the Plot 30 | 31 | Classic ggplot scatterplot. Remove all lines an chart aspects with theme_void. Increase the chart size beyond the plotted points using the xlim() and ylim() functions. This will allow us to nicely center the text on our background image. 32 | 33 | ```{r} 34 | # Basic scatter plot 35 | p <-ggplot(df, aes(x=x, y=y)) + 36 | geom_point(colour = '#BF0000', size = 0.3) + 37 | xlim(0, 18000) + 38 | ylim(0, 10000) + 39 | theme_void() 40 | p 41 | 42 | ``` 43 | 44 | 45 | # Add the background Holiday Image 46 | 47 | Add the holiday background using the ggbackground funcion in the ggimage package. I got this tip from a [great blog post](https://guangchuangyu.github.io/2018/04/setting-ggplot2-background-with-ggbackground/) by [Guangchuang Yu](https://guangchuangyu.github.io/) 48 | 49 | ```{r} 50 | 51 | img <- "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Holidays/%20background.jpg" 52 | imgRead <- image_read(img) 53 | ggbackground(p, img) 54 | 55 | ``` 56 | 57 | 58 | 59 | # Animation 60 | 61 | Create the animation with the gganimate package. Use transition_manual vs transition_reveal because it allows us to specify cumulative=TRUE to keep all data already shown. 62 | 63 | 64 | ```{r} 65 | t <-ggplot(df, aes(x=x, y=y)) + 66 | theme_void() + 67 | theme(plot.background = element_rect(fill = '#CA302F')) + 68 | geom_point(colour = 'white', size = 1) + 69 | transition_manual(as.numeric(x), cumulative=TRUE) 70 | 71 | ``` 72 | 73 | 74 | # Save a gif 75 | 76 | Use the gifski package to save the gif. 77 | 78 | ```{r} 79 | 80 | gif <- animate(t, end_pause = 25, width = 800, height = 400, fps = 8) 81 | gif 82 | anim_save("HappyHolidays.gif", gif) 83 | 84 | ``` 85 | 86 | 87 | -------------------------------------------------------------------------------- /Holidays/animation_files/figure-markdown_github/unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Holidays/animation_files/figure-markdown_github/unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /Holidays/animation_files/figure-markdown_github/unnamed-chunk-4-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Holidays/animation_files/figure-markdown_github/unnamed-chunk-4-1.png -------------------------------------------------------------------------------- /Holidays/animation_files/figure-markdown_github/unnamed-chunk-6-1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Holidays/animation_files/figure-markdown_github/unnamed-chunk-6-1.gif -------------------------------------------------------------------------------- /Holidays/readme.md: -------------------------------------------------------------------------------- 1 | Load all packages 2 | ================= 3 | 4 | ``` r 5 | library(data.table) 6 | library(tidyverse) 7 | ``` 8 | 9 | ## ── Attaching packages ─────────────────────────────────────────────────────────── tidyverse 1.2.1 ── 10 | 11 | ## ✔ ggplot2 3.2.0 ✔ purrr 0.3.2 12 | ## ✔ tibble 2.1.3 ✔ dplyr 0.8.3 13 | ## ✔ tidyr 0.8.3 ✔ stringr 1.4.0 14 | ## ✔ readr 1.3.1 ✔ forcats 0.4.0 15 | 16 | ## ── Conflicts ────────────────────────────────────────────────────────────── tidyverse_conflicts() ── 17 | ## ✖ dplyr::between() masks data.table::between() 18 | ## ✖ dplyr::filter() masks stats::filter() 19 | ## ✖ dplyr::first() masks data.table::first() 20 | ## ✖ dplyr::lag() masks stats::lag() 21 | ## ✖ dplyr::last() masks data.table::last() 22 | ## ✖ purrr::transpose() masks data.table::transpose() 23 | 24 | ``` r 25 | library(gganimate) 26 | library(ggimage) 27 | library(magick) 28 | ``` 29 | 30 | ## Linking to ImageMagick 6.9.9.39 31 | ## Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp 32 | ## Disabled features: fftw, ghostscript, x11 33 | 34 | ``` r 35 | library(gifski) 36 | ``` 37 | 38 | Download the data 39 | ================= 40 | 41 | Csv created with a tool called 42 | [coordinator](https://spotify.github.io/coordinator/), by [Aliza 43 | Aufrichtig](https://twitter.com/alizauf). It allows us to convert SVG 44 | graphics to data co-ordinates 45 | 46 | ``` r 47 | df= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Holidays/Holidays.csv', stringsAsFactors = FALSE) 48 | ``` 49 | 50 | \#Create the Plot 51 | 52 | Classic ggplot scatterplot. Remove all lines an chart aspects with 53 | theme\_void. Increase the chart size beyond the plotted points using the 54 | xlim() and ylim() functions. This will allow us to nicely center the 55 | text on our background image. 56 | 57 | ``` r 58 | # Basic scatter plot 59 | p <-ggplot(df, aes(x=x, y=y)) + 60 | geom_point(colour = '#BF0000', size = 0.3) + 61 | xlim(0, 18000) + 62 | ylim(0, 10000) + 63 | theme_void() 64 | p 65 | ``` 66 | 67 | ![](animation_files/figure-markdown_github/unnamed-chunk-3-1.png) 68 | 69 | Add the background Holiday Image 70 | ================================ 71 | 72 | Add the holiday background using the ggbackground funcion in the ggimage 73 | package. I got this tip from a [great blog 74 | post](https://guangchuangyu.github.io/2018/04/setting-ggplot2-background-with-ggbackground/) 75 | by [Guangchuang Yu](https://guangchuangyu.github.io/) 76 | 77 | ``` r 78 | img <- "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Holidays/%20background.jpg" 79 | imgRead <- image_read(img) 80 | ggbackground(p, img) 81 | ``` 82 | 83 | ![](animation_files/figure-markdown_github/unnamed-chunk-4-1.png) 84 | 85 | Animation 86 | ========= 87 | 88 | Create the animation with the gganimate package. Use transition\_manual 89 | vs transition\_reveal because it allows us to specify cumulative=TRUE to 90 | keep all data already shown. 91 | 92 | ``` r 93 | t <-ggplot(df, aes(x=x, y=y)) + 94 | theme_void() + 95 | theme(plot.background = element_rect(fill = '#CA302F')) + 96 | geom_point(colour = 'white', size = 1) + 97 | transition_manual(as.numeric(x), cumulative=TRUE) 98 | ``` 99 | 100 | Save a gif 101 | ========== 102 | 103 | Use the gifski package to save the gif. 104 | 105 | ``` r 106 | gif <- animate(t, end_pause = 25, width = 800, height = 400, fps = 8) 107 | gif 108 | ``` 109 | 110 | ![](animation_files/figure-markdown_github/unnamed-chunk-6-1.gif) 111 | 112 | ``` r 113 | anim_save("HappyHolidays.gif", gif) 114 | ``` 115 | -------------------------------------------------------------------------------- /IWD_2018/2015_equalityIndex.csv: -------------------------------------------------------------------------------- 1 | Country,Full Name,Year,Index 2 | BE,Belgium,2015,70.5 3 | BG,Bulgaria,2015,58 4 | CZ,Czech Republic,2015,53.6 5 | DK,Denmark,2015,76.8 6 | DE,Germany,2015,65.5 7 | EE,Estonia,2015,56.7 8 | IE,Ireland,2015,69.5 9 | EL,Greece,2015,50 10 | ES,Spain,2015,68.3 11 | FR,France,2015,72.6 12 | HR,Croatia,2015,53.1 13 | IT,Italy,2015,62.1 14 | CY,Cyprus,2015,55.1 15 | LV,Latvia,2015,57.9 16 | LT,Lithuania,2015,56.8 17 | LU,Luxembourg,2015,69 18 | HU,Hungary,2015,50.8 19 | MT,Malta,2015,60.1 20 | NL,Netherlands,2015,72.9 21 | AT,Austria,2015,63.3 22 | PL,Poland,2015,56.8 23 | PT,Portugal,2015,56 24 | RO,Romania,2015,52.4 25 | SI,Slovenia,2015,68.4 26 | SK,Slovakia,2015,52.4 27 | FI,Finland,2015,73 28 | SE,Sweden,2015,82.6 29 | UK,United Kingdom,2015,71.5 -------------------------------------------------------------------------------- /IWD_2018/EUEqualityData.R: -------------------------------------------------------------------------------- 1 | 2 | # Base code from -https://www.computerworld.com/article/3175623/data-analytics/mapping-in-r-just-got-a-whole-lot-easier.html 3 | 4 | install.packages("tmap") 5 | install.packages("tmaptools") 6 | install.packages("data.table") 7 | library(data.table) 8 | library("tmap") 9 | library("tmaptools") 10 | 11 | #Download the shape files from: 12 | #Replace the path with the path to your shape file 13 | eurGeoMap <- read_shape("/iwd2018/NUTS_2013_01M_SH/Data/NUTS_RG_01M_2013.shp", as.sf = TRUE, stringsAsFactors = FALSE) 14 | 15 | #Download the equality data directly from GH. 16 | #Equality data sourced from: https://data.europa.eu 17 | eurEquality= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/IWD_2018/2015_equalityIndex.csv') 18 | eurGeoMap$country <-substr(eurGeoMap$NUTS_ID, 1, 2) 19 | eurGeoMap$country 20 | eurEqualityMap <- append_data(eurGeoMap, eurEquality, key.shp = "country", key.data = "Country") 21 | 22 | tmap_mode("view") 23 | tm_shape(eurEqualityMap) + tm_polygons("Index", id = "country") -------------------------------------------------------------------------------- /IWD_2018/Gender_equality_over_time.csv: -------------------------------------------------------------------------------- 1 | Country,Full Name,Year,Index,,,,,,,,,,,, 2 | EU-28,All EU,2005,62,,,,,,,,,,,, 3 | BE,Belgium,2005,66,,,,,,,,,,,, 4 | BG,Bulgaria,2005,56,,,,,,,,,,,, 5 | CZ,Czech Republic,2005,53.6,,,,,,,,,,,, 6 | DK,Denmark,2005,74.6,,,,,,,,,,,, 7 | DE,Germany,2005,60,,,,,,,,,,,, 8 | EE,Estonia,2005,52.2,,,,,,,,,,,, 9 | IE,Ireland,2005,61.9,,,,,,,,,,,, 10 | EL,Greece,2005,46.8,,,,,,,,,,,, 11 | ES,Spain,2005,62.2,,,,,,,,,,,, 12 | FR,France,2005,65.2,,,,,,,,,,,, 13 | HR,Croatia,2005,50.3,,,,,,,,,,,, 14 | IT,Italy,2005,49.2,,,,,,,,,,,, 15 | CY,Cyprus,2005,45.9,,,,,,,,,,,, 16 | LV,Latvia,2005,53.4,,,,,,,,,,,, 17 | LT,Lithuania,2005,55.8,,,,,,,,,,,, 18 | LU,Luxembourg,2005,64.4,,,,,,,,,,,, 19 | HU,Hungary,2005,49.5,,,,,,,,,,,, 20 | MT,Malta,2005,56,,,,,,,,,,,, 21 | NL,Netherlands,2005,67.8,,,,,,,,,,,, 22 | AT,Austria,2005,59.5,,,,,,,,,,,, 23 | PL,Poland,2005,52.4,,,,,,,,,,,, 24 | PT,Portugal,2005,49.9,,,,,,,,,,,, 25 | RO,Romania,2005,49.9,,,,,,,,,,,, 26 | SI,Slovenia,2005,60.8,,,,,,,,,,,, 27 | SK,Slovakia,2005,52.5,,,,,,,,,,,, 28 | FI,Finland,2005,72,,,,,,,,,,,, 29 | SE,Sweden,2005,78.8,,,,,,,,,,,, 30 | UK,United Kingdom,2005,71.2,,,,,,,,,,,, 31 | EU-28,All EU,2010,63.8,,,,,,,,,,,, 32 | BE,Belgium,2010,69.3,,,,,,,,,,,, 33 | BG,Bulgaria,2010,55,,,,,,,,,,,, 34 | CZ,Czech Republic,2010,55.6,,,,,,,,,,,, 35 | DK,Denmark,2010,75.2,,,,,,,,,,,, 36 | DE,Germany,2010,62.6,,,,,,,,,,,, 37 | EE,Estonia,2010,53.4,,,,,,,,,,,, 38 | IE,Ireland,2010,65.4,,,,,,,,,,,, 39 | EL,Greece,2010,48.6,,,,,,,,,,,, 40 | ES,Spain,2010,66.4,,,,,,,,,,,, 41 | FR,France,2010,67.5,,,,,,,,,,,, 42 | HR,Croatia,2010,52.3,,,,,,,,,,,, 43 | IT,Italy,2010,53.3,,,,,,,,,,,, 44 | CY,Cyprus,2010,49,,,,,,,,,,,, 45 | LV,Latvia,2010,55.2,,,,,,,,,,,, 46 | LT,Lithuania,2010,54.9,,,,,,,,,,,, 47 | LU,Luxembourg,2010,61.2,,,,,,,,,,,, 48 | HU,Hungary,2010,52.4,,,,,,,,,,,, 49 | MT,Malta,2010,54.4,,,,,,,,,,,, 50 | NL,Netherlands,2010,74,,,,,,,,,,,, 51 | AT,Austria,2010,58.7,,,,,,,,,,,, 52 | PL,Poland,2010,55.5,,,,,,,,,,,, 53 | PT,Portugal,2010,53.7,,,,,,,,,,,, 54 | RO,Romania,2010,50.8,,,,,,,,,,,, 55 | SI,Slovenia,2010,62.7,,,,,,,,,,,, 56 | SK,Slovakia,2010,53,,,,,,,,,,,, 57 | FI,Finland,2010,73.1,,,,,,,,,,,, 58 | SE,Sweden,2010,80.1,,,,,,,,,,,, 59 | UK,United Kingdom,2010,68.7,,,,,,,,,,,, 60 | EU-28,All EU,2012,65,,,,,,,,,,,, 61 | BE,Belgium,2012,70.2,,,,,,,,,,,, 62 | BG,Bulgaria,2012,56.9,,,,,,,,,,,, 63 | CZ,Czech Republic,2012,56.7,,,,,,,,,,,, 64 | DK,Denmark,2012,75.6,,,,,,,,,,,, 65 | DE,Germany,2012,64.9,,,,,,,,,,,, 66 | EE,Estonia,2012,53.5,,,,,,,,,,,, 67 | IE,Ireland,2012,67.7,,,,,,,,,,,, 68 | EL,Greece,2012,50.1,,,,,,,,,,,, 69 | ES,Spain,2012,67.4,,,,,,,,,,,, 70 | FR,France,2012,68.9,,,,,,,,,,,, 71 | HR,Croatia,2012,52.6,,,,,,,,,,,, 72 | IT,Italy,2012,56.5,,,,,,,,,,,, 73 | CY,Cyprus,2012,50.6,,,,,,,,,,,, 74 | LV,Latvia,2012,56.2,,,,,,,,,,,, 75 | LT,Lithuania,2012,54.2,,,,,,,,,,,, 76 | LU,Luxembourg,2012,65.9,,,,,,,,,,,, 77 | HU,Hungary,2012,51.8,,,,,,,,,,,, 78 | MT,Malta,2012,57.8,,,,,,,,,,,, 79 | NL,Netherlands,2012,74,,,,,,,,,,,, 80 | AT,Austria,2012,61.3,,,,,,,,,,,, 81 | PL,Poland,2012,56.9,,,,,,,,,,,, 82 | PT,Portugal,2012,54.4,,,,,,,,,,,, 83 | RO,Romania,2012,51.2,,,,,,,,,,,, 84 | SI,Slovenia,2012,66.1,,,,,,,,,,,, 85 | SK,Slovakia,2012,52.4,,,,,,,,,,,, 86 | FI,Finland,2012,74.4,,,,,,,,,,,, 87 | SE,Sweden,2012,79.7,,,,,,,,,,,, 88 | UK,United Kingdom,2012,68.9,,,,,,,,,,,, 89 | EU-28,All EU,2015,66.2,,,,,,,,,,,, 90 | BE,Belgium,2015,70.5,,,,,,,,,,,, 91 | BG,Bulgaria,2015,58,,,,,,,,,,,, 92 | CZ,Czech Republic,2015,53.6,,,,,,,,,,,, 93 | DK,Denmark,2015,76.8,,,,,,,,,,,, 94 | DE,Germany,2015,65.5,,,,,,,,,,,, 95 | EE,Estonia,2015,56.7,,,,,,,,,,,, 96 | IE,Ireland,2015,69.5,,,,,,,,,,,, 97 | EL,Greece,2015,50,,,,,,,,,,,, 98 | ES,Spain,2015,68.3,,,,,,,,,,,, 99 | FR,France,2015,72.6,,,,,,,,,,,, 100 | HR,Croatia,2015,53.1,,,,,,,,,,,, 101 | IT,Italy,2015,62.1,,,,,,,,,,,, 102 | CY,Cyprus,2015,55.1,,,,,,,,,,,, 103 | LV,Latvia,2015,57.9,,,,,,,,,,,, 104 | LT,Lithuania,2015,56.8,,,,,,,,,,,, 105 | LU,Luxembourg,2015,69,,,,,,,,,,,, 106 | HU,Hungary,2015,50.8,,,,,,,,,,,, 107 | MT,Malta,2015,60.1,,,,,,,,,,,, 108 | NL,Netherlands,2015,72.9,,,,,,,,,,,, 109 | AT,Austria,2015,63.3,,,,,,,,,,,, 110 | PL,Poland,2015,56.8,,,,,,,,,,,, 111 | PT,Portugal,2015,56,,,,,,,,,,,, 112 | RO,Romania,2015,52.4,,,,,,,,,,,, 113 | SI,Slovenia,2015,68.4,,,,,,,,,,,, 114 | SK,Slovakia,2015,52.4,,,,,,,,,,,, 115 | FI,Finland,2015,73,,,,,,,,,,,, 116 | SE,Sweden,2015,82.6,,,,,,,,,,,, 117 | UK,United Kingdom,2015,71.5,,,,,,,,,,,, -------------------------------------------------------------------------------- /IWD_2018/IWD_2018.md: -------------------------------------------------------------------------------- 1 | asdfasdf 2 | -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/NUTS_2013_UML.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/NUTS_2013_UML.png -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_AT_2013.cpg: -------------------------------------------------------------------------------- 1 | UTF-8 -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_AT_2013.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_AT_2013.csv -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_AT_2013.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_AT_2013.dbf -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.dbf -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]] -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.sbn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.sbn -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.sbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.sbx -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.shp -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_BN_01M_2013.shx -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.cpg: -------------------------------------------------------------------------------- 1 | UTF-8 -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.dbf: -------------------------------------------------------------------------------- 1 | s o:NUTS_JOIN_N CNTR_CODECNUTS_LEV1_CNUTS_LEV2_CNUTS_LEV3_CMIN_SCALN SHAPE_LengF 1 CH021 1000000 1.57775855255e-001 2 CH021 1000000 3.62570709724e-002 3 CH01 CH011 10000000 9.09239458438e-002 4 CH02 CH022 20000000 1.05278079659e-001 5 CH02 CH022 3000000 7.30564468291e-002 6 CH02 CH022 3000000 8.92752426596e-002 7IT ITC ITC4 ITC42 1000000 8.59315681662e-002 8 ITC4C 3000000 2.95841202294e-001 9 ITI1 ITI18 3000000 2.58525830290e-001 10 ES1 ES13 ES130 3000000 3.70010403064e-001 11 ES213 10000000 3.08788851202e-001 12 ITI22 3000000 1.59128735850e-001 13 ES414 3000000 9.30578505962e-002 14ES ES5 ES51 ES512 3000000 1.44233457031e-001 15 ITI41 1000000 1.49365379353e-001 16 ES418 10000000 2.78813141320e-001 17 TR1 TR10 TR100 60000000 4.00046814916e-001 18 ES3 ES30 ES300 3000000 2.51803050775e-001 19 ES5 ES52 ES523 20000000 3.54917064307e-001 20 ES422 20000000 3.63448908067e-001 21 EL541 10000000 1.77090710449e-001 22 DE80N 3000000 8.98878139966e-002 23 DE93A 1000000 3.46970900003e-002 24 UKI4 1000000 6.16200797976e-002 25 DE731 1000000 5.88458436302e-002 26 BE2 BE22 BE223 10000000 2.99799061038e-001 27DE DE1 DE13 DE138 3000000 1.28501593337e-001 28 CH05 CH052 3000000 1.83254856221e-001 29 CH054 3000000 1.37732393457e-001 30 BE3 BE32 BE324 10000000 3.05218121150e-001 31 BE334 3000000 2.78323740346e-001 32 DE128 1000000 1.54545825357e-001 33 DE715 10000000 1.12281281808e-001 34 CH064 10000000 1.59410318538e-001 35 UKE1 UKE12 1000000 1.84458340135e-001 36 UKE UKE1 UKE13 1000000 2.62493368212e-001 37 UKI3 1000000 6.15326823709e-002 38 DK011 20000000 8.14019206624e-002 39 UKN02 60000000 1.59627481046e-001 41 UKD11 20000000 2.03459054351e-001 43 UKD7 60000000 2.68443872212e-001 44 DE5 DE50 DE501 60000000 4.69861235406e-001 45 DE401 1000000 4.15213314344e-002 46 DEE0D 3000000 1.55924260282e-001 47 DE91B 20000000 3.41177068180e-001 48 DE918 3000000 1.34402109782e-001 49 UKI5 1000000 7.57816891890e-002 50BE BE2 BE21 BE213 100000 1.06242417432e-002 51 UKJ41 1000000 7.22100717942e-002 52 DEE0B 100000 2.01480785519e-002 53BE BE2 BE21 BE213 3000000 3.97619014100e-002 54 DEB33 3000000 1.43910240544e-001 55 DEB33 1000000 8.81911622431e-002 56 DE115 3000000 1.07804984104e-001 57 DE123 1000000 2.03442475292e-002 58 DE124 1000000 1.77664341155e-002 59 DE134 1000000 4.07317631113e-002 60 DE11 DE11C 1000000 4.13280534478e-002 61 DE27C 3000000 2.13371133284e-001 62 CH052 10000000 2.37126667931e-001 63 CH057 1000000 5.48788347562e-002 64AT AT3 AT33 AT331 3000000 1.16953679814e-001 65 CH054 3000000 3.81745089819e-002 66 CH03 CH032 3000000 5.78907831913e-002 67 CH023 3000000 5.22332433946e-002 68 CH021 3000000 3.83232604817e-002 69 FR261 3000000 3.31905240810e-001 70 IS001 10000000 3.28452101396e-001 71 FR411 1000000 6.49399599679e-002 72 FR523 10000000 1.33418588807e-001 73BE BE2 BE21 BE213 1000000 7.72477693853e-003 74BE BE2 BE21 BE213 100000 1.11760251533e-002 75 BE336 20000000 9.42933292432e-002 76 DK01 60000000 2.65514876683e+000 77 DE6 DE60 DE600 3000000 1.36151463553e+000 78 DE80N 1000000 4.30244185557e-002 79 DEE0B 1000000 4.64476346922e-002 80 CH013 3000000 8.52486148808e-002 81 CH013 100000 6.88923033059e-002 82 DE24 DE245 1000000 2.28169349691e-002 83 DE24 DE245 100000 2.85285025175e-002 85 DE123 1000000 2.03626398188e-001 86 ITF32 3000000 7.04604224648e-002 88 BG323 3000000 1.18465284288e-001 90 BG422 1000000 1.23878946842e-002 91 BG421 100000 2.13924103225e-002 93 NO012 20000000 2.36171732159e-001 94 ES4 ES41 ES412 20000000 2.11474830095e-001 95 DE124 1000000 2.94458118305e-002 96 ES22 ES220 3000000 3.84394087968e-001 97 DE124 100000 1.62851275428e-002 98 ES22 ES220 3000000 3.41086898671e-001 99 CH02 CH023 3000000 9.28551673615e-002 100 DE6 DE60 DE600 3000000 6.73865450384e-002 101 CH022 3000000 9.01999606563e-002 102 DE80N 3000000 9.58314456761e-002 103 ES4 ES41 ES414 3000000 6.93715823793e-002 104 DE134 100000 1.51671749672e-002 105 DE134 100000 2.82979329645e-002 107 DE212 100000 1.15387694164e-002 108 DE215 100000 1.15387689733e-002 109 DE21K 3000000 6.42055187230e-002 110 DE21K 100000 1.31715347582e-002 111 DE21K 100000 1.00086009472e-002 112 DE26B 100000 1.63054562967e-002 113 DE26B 100000 2.85285025880e-002 114 DE93A 100000 2.55339511539e-002 115 DE94C 60000000 6.42665123792e-001 116 DEF0C 3000000 8.14019206625e-002 117 ITF52 60000000 1.78033608094e-001 118 ITI21 3000000 1.70841429156e-001 -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]] -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.sbn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.sbn -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.sbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.sbx -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.shp -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_JOIN_LI_2013.shx -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.cpg: -------------------------------------------------------------------------------- 1 | UTF-8 -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.dbf -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]] -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.sbn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.sbn -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.sbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.sbx -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.shp -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_LB_2013.shx -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.dbf -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]] -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.sbn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.sbn -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.sbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.sbx -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.shp -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_01M_2013.shx -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_BN_01M_2013.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_RG_BN_01M_2013.dbf -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.cpg: -------------------------------------------------------------------------------- 1 | UTF-8 -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.dbf -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]] -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.sbn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.sbn -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.sbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.sbx -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.shp -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/data/NUTS_SEPA_LI_2013.shx -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/metadata/Metadata_NUTS_2013.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/metadata/Metadata_NUTS_2013.pdf -------------------------------------------------------------------------------- /IWD_2018/NUTS_2013_01M_SH/readme.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/IWD_2018/NUTS_2013_01M_SH/readme.txt -------------------------------------------------------------------------------- /IWD_2018/readme.md: -------------------------------------------------------------------------------- 1 | A tutorial showing some analysis on some gender equality among EU countries to celebrate international womens day in 2018. 2 | Tutorial uses rawgraphs.io and tmap r package. 3 | 4 | Tutorial available [here](https://www.littlemissdata.com/blog/2018/iwd2018) 5 | -------------------------------------------------------------------------------- /Patchwork/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/.DS_Store -------------------------------------------------------------------------------- /Patchwork/miniPatchworkVerticalRectangle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/miniPatchworkVerticalRectangle.png -------------------------------------------------------------------------------- /Patchwork/miniPatchworkWideRectangle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/miniPatchworkWideRectangle.png -------------------------------------------------------------------------------- /Patchwork/patchwork.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Patchwork" 3 | author: "Laura Ellis" 4 | date: "3/2/2020" 5 | output: 6 | md_document: 7 | variant: markdown_github 8 | --- 9 | 10 | # Set Up 11 | 12 | ## Install and load all the packages 13 | 14 | Thank you to "Dusty" who posted the package "easypackages" on my [last tutorial](https://www.littlemissdata.com/blog/bacheloranalysis) 15 | 16 | ```{r} 17 | #install.packages("easypackages") 18 | library(easypackages) 19 | packages("tidyverse", "rtweet", "tidytext", "rtweet", "patchwork", "cran.stats", "data.table", 20 | "gameofthrones", "ggimage", "magick", "ggpubr", "jpeg", "png") 21 | 22 | ``` 23 | 24 | 25 | ## Set up our colour palette 26 | 27 | Using the beautiful Game of Thrones color palette from [Alejandro Jiménez](https://twitter.com/aljrico) in his ["gameofthrones" package.](https://github.com/aljrico/gameofthrones). Thank you to [Divya Seernani](https://twitter.com/DSeernani) for sharing! 28 | ```{r} 29 | 30 | #Set the palette using the beautiful GOT Arya palette from Alejandro Jiménez 31 | pal <- got(20, option = "Arya") 32 | 33 | #cherry pick a few extended 34 | c <-"#889999" 35 | c2 <- "#AAB7AF" 36 | ``` 37 | 38 | ## Add your twitter credentials 39 | Create your twitter authentication token by following the steps in [Michael Kearneys](https://mikewk.com/) [beautiful documentation](https://rtweet.info/articles/auth.html). Replace the "ADD YOUR CREDS" with your own credentials. 40 | 41 | ```{r} 42 | #create_token( 43 | # app = "ADD YOUR CREDS", 44 | # consumer_key = "ADD YOUR CREDS", 45 | # consumer_secret = "ADD YOUR CREDS") 46 | ``` 47 | 48 | # Create Plots 49 | 50 | ## 1st Plot - Create a plot of the tweet stats (favorites, retweets) 51 | 52 | ### Lookup the tweet and view stats 53 | 54 | 55 | ```{r} 56 | lt <-lookup_tweets('1229176433123168256') 57 | lt 58 | ``` 59 | 60 | ### Create a chart with the tweet stats 61 | 62 | ```{r} 63 | p1 <- lt %>% 64 | rename(Faves = favorite_count, RTs = retweet_count) %>% 65 | select(Faves, RTs) %>% #select only the desired columns 66 | gather("stat", "value") %>% #reformat to make the table long which is easier for bar charts to consume 67 | ggplot(aes(stat, value)) + #plot the bar chart 68 | geom_bar(stat="identity", fill=c2) + 69 | theme_classic() + 70 | labs(title = "Tweet Stats", 71 | x = "Tweet Statistic", y = "Total") 72 | 73 | p1 74 | ``` 75 | 76 | 77 | ## 2nd Plot - Create a plot of the tweet stats (favorites, retweets) 78 | 79 | ### Gather approx 1K of the retweet data 80 | 81 | The [get_retweets()](https://rtweet.info/reference/get_retweets.html) function only allows a max of 100 retweets to be pulled via the API at a time. This is a rate imposed by the twitter API. When pulling this data, I had quite a difficult time. Not only, did a lot of the suggested methods to getting cursors fail, the rate limiting wasn't consistent. Sometimes I was able to get close to 1K tweets in 100 batches. Sometimes it blocked me for 15 min intervals (as expected). Since this is just an example to show patchwork, I decided to just grab 1K of the retweets which is roughly half of the full set of retweets. Further, I should let you know that I did attempt to put it in a function, but I couldn't find an appropriate system wait time that would complete in a reasonable time and/or actually return the data. Please reach out if you have a better/proven method! In the meantime, here is my brute force method. 82 | 83 | ```{r} 84 | 85 | statusid <- '1229176433123168256' #set the first lowest retweet statusid to be the id of the original tweet 86 | rtweets <- get_retweets(statusid, n=100, parse = TRUE) # get 100 retweets 87 | min_id <- min(rtweets$status_id) 88 | 89 | rtweets2 <- get_retweets(statusid, n=100, max_id = min_id, parse = TRUE) # get 100 retweets 90 | min_id <- min(rtweets2$status_id) 91 | 92 | rtweets3 <- get_retweets(statusid, n=100, max_id = min_id, parse = TRUE) # get 100 retweets 93 | min_id <- min(rtweets3$status_id) 94 | 95 | rtweets4 <- get_retweets(statusid, n=100, max_id = min_id, parse = TRUE) # get 100 retweets 96 | min_id <- min(rtweets4$status_id) 97 | 98 | rtweets5 <- get_retweets(statusid, n=100, max_id = min_id, parse = TRUE) # get 100 retweets 99 | min_id <- min(rtweets5$status_id) 100 | 101 | rtweets6 <- get_retweets(statusid, n=100, max_id = min_id, parse = TRUE) # get 100 retweets 102 | min_id <- min(rtweets6$status_id) 103 | 104 | rtweets7 <- get_retweets(statusid, n=100, max_id = min_id, parse = TRUE) # get 100 retweets 105 | min_id <- min(rtweets7$status_id) 106 | 107 | rtweets8 <- get_retweets(statusid, n=100, max_id = min_id, parse = TRUE) # get 100 retweets 108 | min_id <- min(rtweets8$status_id) 109 | 110 | rtweets9 <- get_retweets(statusid, n=100, max_id = min_id, parse = TRUE) # get 100 retweets 111 | min_id <- min(rtweets9$status_id) 112 | 113 | rtweets10 <- get_retweets(statusid, n=100, max_id = min_id, parse = TRUE) # get 100 retweets 114 | min_id <- min(rtweets10$status_id) 115 | 116 | rtweet_table <-rbind(rtweets, rtweets2, rtweets3, rtweets4, rtweets5, rtweets6, rtweets7, rtweets8, rtweets9, rtweets10) 117 | nrow(rtweet_table) 118 | length(unique(rtweet_table$description)) 119 | 120 | ``` 121 | 122 | ### Graph the most common words used in the retweeters profile descriptions 123 | 124 | Most of the techniques used below to process the data and graph the data are taken from the [tidy text mining](https://www.tidytextmining.com/) book by [Julia Silge](https://twitter.com/juliasilge) and [David Robinson](https://twitter.com/drob) 125 | 126 | 127 | ```{r} 128 | 129 | data(stop_words) 130 | #Unnest the words - code via Tidy Text 131 | rtweet_table2 <- rtweet_table %>% 132 | unnest_tokens(word, description) %>% 133 | anti_join(stop_words) %>% 134 | count(word, sort = TRUE) %>% 135 | filter(!word %in% c('t.co', 'https')) 136 | 137 | 138 | p2 <- rtweet_table2 %>% 139 | filter(n> 50) %>% 140 | mutate(word = reorder(word, n)) %>% 141 | ggplot(aes(word, n)) + 142 | theme_classic() + 143 | geom_col(fill= c) + 144 | labs(title = "RT Profiles", 145 | x = "Key Words", y = "Total Occurances") + 146 | coord_flip() 147 | 148 | p2 149 | ``` 150 | 151 | 152 | 153 | ## 3rd Plot - Plot the patchwork CRAN download stats 154 | 155 | ### Gather the data 156 | 157 | To download the patchwork download stats, I used the ["cran.stats" package](https://www.rdocumentation.org/packages/cran.stats/versions/0.1). The examples to process the download stats were very easy and I just followed the [examples here](https://cran.r-project.org/web/packages/dlstats/vignettes/dlstats.html#download-stats-of-cran-packages). 158 | 159 | ```{r} 160 | 161 | dt = read_logs(start = as.Date("2020-02-01"), 162 | end = as.Date("2020-02-29"), 163 | verbose = TRUE) 164 | dim(dt) 165 | 166 | patchwork <- stats_logs(dt, type="daily", packages=c("patchwork"), 167 | dependency=TRUE, duration = 30L) 168 | ``` 169 | 170 | 171 | ### Plot the CRAN download data 172 | 173 | Using the ggplot, geom_line() function with just a little extra fancyness to annotate the graph with the annotate() function. Great annotation examples [here](https://ggplot2.tidyverse.org/reference/annotate.html) 174 | 175 | ```{r} 176 | p3 <- ggplot(patchwork, aes(x=key, y=tot_N, group=1)) + 177 | geom_line() + theme_classic() + theme(axis.text.x = element_text(angle = 60, hjust = 1)) + 178 | ylim(0, 1500) + 179 | labs(title = "Downloads of the R Patchwork Package", 180 | x = "Date", y = "Total Downloads") + 181 | annotate("rect", xmin = "2020-02-16", xmax = "2020-02-20", ymin = 400, ymax = 900, 182 | alpha = .3, fill = c2) + 183 | annotate( 184 | geom = "curve", alpha = 0.3, x = "2020-02-14", y = 650, xend = "2020-02-17", yend = 800, 185 | curvature = .3, arrow = arrow(length = unit(2, "mm")) 186 | ) + 187 | annotate(geom = "text", x = "2020-02-07", y = 650, label = "Nerd viral #rstats tweet", hjust = "left", alpha = 0.5) 188 | 189 | p3 190 | 191 | ``` 192 | 193 | 194 | # Add the plots to the same graphic using patchwork 195 | 196 | As is the focus of this post, when this package was [shared on twitter](https://twitter.com/LittleMissData/status/1229176433123168256), people were very excited about it. The [patchwork package](https://github.com/thomasp85/patchwork) was created by [Thomas Lin Pedersen](https://twitter.com/thomasp85). Not only is it incredibly easy to use, it comes with great [documentation](https://patchwork.data-imaginist.com/index.html) 197 | 198 | ## Try a few layouts 199 | 200 | Using the plots p1, p2, p3 created above, try a few layouts following the package documentation 201 | 202 | ```{r} 203 | p1/p2/p3 204 | p1 + p2 + p3 205 | p1/ (p2 +p3) 206 | 207 | #Final Layout 208 | p <- p3 / (p1 + p2) 209 | p 210 | ``` 211 | 212 | ## Annotate the final layout 213 | 214 | We will select the final layout from the above code block and then add some overall titles, captioning and formatting. This example was covered in the excellent [patchwork annotation guide](https://patchwork.data-imaginist.com/articles/guides/annotation.html) 215 | 216 | ```{r } 217 | # Change the values for the labels 218 | # Overall annotation 219 | 220 | p + plot_annotation( 221 | title = 'Patchwork Went Nerd Viral', 222 | caption = 'Source: @littlemissdata' 223 | ) 224 | 225 | 226 | p + plot_annotation( 227 | title = 'Patchwork Went Nerd Viral', 228 | caption = 'Source: @littlemissdata' 229 | ) & 230 | theme(text = element_text('mono')) 231 | ``` 232 | 233 | # Add an image to the patchwork graphic 234 | 235 | ## Bring in the image 236 | 237 | Using an empty plot and the background_image() function, you can bring an image into a graph object. Further, you can prevent image resizing with the coord_fixed() function. This is important so the actual image doesn't get resized with the patchwork placement. 238 | 239 | ```{r} 240 | ## Add an image 241 | 242 | twitter <- image_read('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Patchwork/twitter_post.png') 243 | twitter <- ggplot() + 244 | background_image(twitter) + coord_fixed() 245 | ``` 246 | 247 | ## Plot the image with patchwork 248 | 249 | ```{r } 250 | 251 | (twitter + p3) / (p1 + p2 ) 252 | pF <- twitter + (p3/ (p1 + p2)) 253 | 254 | pF + plot_annotation( 255 | title = 'Patchwork Went Nerd Viral', 256 | caption = 'Source: @littlemissdata' 257 | ) & 258 | theme(text = element_text(family ='mono', size = 9)) 259 | 260 | ``` 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | -------------------------------------------------------------------------------- /Patchwork/patchworkTwitter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchworkTwitter.png -------------------------------------------------------------------------------- /Patchwork/patchworkTwitterFrame.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchworkTwitterFrame.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-10-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-10-1.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-10-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-10-2.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-10-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-10-3.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-10-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-10-4.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-11-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-11-1.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-11-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-11-2.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-13-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-13-1.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-13-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-13-2.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-5-1.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-7-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-7-1.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-7-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-7-2.png -------------------------------------------------------------------------------- /Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-9-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/patchwork_files/figure-markdown_github/unnamed-chunk-9-1.png -------------------------------------------------------------------------------- /Patchwork/rHexAmeliaMN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/rHexAmeliaMN.png -------------------------------------------------------------------------------- /Patchwork/top10KTwitter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/top10KTwitter.png -------------------------------------------------------------------------------- /Patchwork/top10KTwitterFrame.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/top10KTwitterFrame.png -------------------------------------------------------------------------------- /Patchwork/twitter_post.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/Patchwork/twitter_post.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Various R Tutorials 2 | 3 | This is an area to host the content for various R tutorials posted on [littlemissdata.com](littlemissdata.com). 4 | 5 | 6 | -------------------------------------------------------------------------------- /Templates/eventDocumentation.md: -------------------------------------------------------------------------------- 1 | 2 | # Event: Button Click 3 | 4 | ## What Is It? 5 | 6 | This event shows that 7 | 8 | ### When did we start tracking this data? 9 | 10 | July 12th, 2018 11 | 12 | ### What attributes are tracked? 13 | 14 | | Attribute | Description | Examples | 15 | | ------- | ------------- | ------------- | 16 | | xyz | xyz | xyz | 17 | | xyz | xyz | xyz | 18 | 19 | 20 | ## When Does the Data Load? 21 | 22 | Within minutes after the user ... 23 | 24 | ### Change Log 25 | 26 | Date | Data Changed | Change Description 27 | --------- | ----------- | ----------- 28 | Oct. 18th, 2018 | Event released 29 | July 12th, 2018 | Event expanded to xyz 30 | 31 | ## Where is This Data Tracked? 32 | 33 | ### Postgres Warehouse 34 | 35 | schema.event 36 | 37 | ### XYZ Tool 38 | 39 | Under the event "*xyz" 40 | 41 | ## Shortcomings of the Data? 42 | 43 | Currently ... 44 | 45 | ## Examples 46 | 47 | Links 48 | 49 | ## Advanced Information 50 | 51 | Not Applicable 52 | -------------------------------------------------------------------------------- /datatable/readme.md: -------------------------------------------------------------------------------- 1 | A tutorial about making pretty tables in R using the datatable package. 2 | 3 | The data set is from the [Austin 2015 Annual Performance Review](https://data.austintexas.gov/Budget-and-Finance/2015-Annual-Performance-Report-Key-Indicators/dgbw-ce4x/data) 4 | 5 | Full tutorial here: [Create stylish tables in R using formattable](https://www.littlemissdata.com/blog/prettytables) 6 | -------------------------------------------------------------------------------- /files/Think.R: -------------------------------------------------------------------------------- 1 | ########## Pre-Steps ########## 2 | 3 | # 1) Create a photo and convert to svg with any tool. I used: http://pngtosvg.com/ 4 | # 2) Convert the svg to csv data coordinates: https://spotify.github.io/coordinator/ 5 | # 3) Save the csv on DSX or post online as I did 6 | 7 | ########## Import Packages ########## 8 | install.packages("data.table") 9 | install.packages("ggplot2") 10 | 11 | ########## Load Packages ########## 12 | 13 | library(data.table) 14 | library(ggplot2) 15 | 16 | ########## Import Data ########## 17 | 18 | df= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/files/think_image_coords_final.csv', stringsAsFactors = FALSE) 19 | 20 | # alternative 1 21 | # df= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/files/lmd-loves-cloud.csv', stringsAsFactors = FALSE) 22 | 23 | # alternative 2 24 | # df= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/files/ibm_think_tile.csv' , stringsAsFactors = FALSE) 25 | 26 | 27 | #Ensure it loads and attach the column names 28 | summary(df) 29 | dim(df) 30 | attach(df) 31 | 32 | ########## Plot the Data ########## 33 | 34 | # Basic scatter plot 35 | p <-ggplot(df, aes(x=x, y=y)) + 36 | geom_point(colour = '#006699', size = 0.05) 37 | p 38 | 39 | #flip the scale 40 | p <-p + scale_y_reverse() 41 | p 42 | 43 | #make it pretty 44 | p + 45 | theme(panel.background = element_rect(fill = 'white')) + 46 | theme(axis.line=element_blank(), 47 | axis.text.x=element_blank(), 48 | axis.text.y=element_blank(), 49 | axis.ticks=element_blank(), 50 | axis.title.x=element_blank(), 51 | axis.title.y=element_blank(), 52 | legend.position="none", 53 | panel.background=element_blank(), 54 | panel.border=element_blank(), 55 | panel.grid.major=element_blank(), 56 | panel.grid.minor=element_blank(), 57 | plot.background=element_blank()) 58 | 59 | 60 | -------------------------------------------------------------------------------- /gghighlight/ggplot2_and_gghighlight.R: -------------------------------------------------------------------------------- 1 | install.packages("lubridate") 2 | install.packages("ggplot2") 3 | install.packages("ggmap") 4 | install.packages("data.table") 5 | install.packages("ggrepel") 6 | install.packages("dplyr") 7 | install.packages("magrittr") 8 | 9 | 10 | library(lubridate) 11 | library(ggplot2) 12 | library(ggmap) 13 | library(dplyr) 14 | library(data.table) 15 | library(ggrepel) 16 | library(magrittr) 17 | 18 | 19 | # Download the main crime incident dataset 20 | 21 | incidents= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/ggmap/i2Sample.csv', stringsAsFactors = FALSE) 22 | str(incidents) 23 | attach(incidents) 24 | 25 | # Create some color variables for graphing later 26 | custGrey = "#A9A9A9" 27 | 28 | #add year to the incidents data frame 29 | incidents$ymd <-mdy_hms(Event.Clearance.Date) 30 | incidents$month <- lubridate::month(incidents$ymd) 31 | incidents$year <- year(incidents$ymd) 32 | incidents$wday <- lubridate::wday(incidents$ymd, label = TRUE) 33 | incidents$hour <- hour(incidents$ymd) 34 | 35 | #Create a more manageable data frame with only 2017 data 36 | i2 <- incidents[year>=2017, ] 37 | 38 | #Only include complete cases 39 | i2[complete.cases(i2), ] 40 | 41 | attach(i2) 42 | head(i2) 43 | 44 | #Group the data into a new data frame which has the count of events per month by subgroup 45 | 46 | groupSummaries <- i2 %>% 47 | group_by(month, Event.Clearance.SubGroup) %>% 48 | summarize(N = length(Event.Clearance.SubGroup)) 49 | 50 | #View the new data set 51 | 52 | head(groupSummaries, n=100) 53 | attach(groupSummaries) 54 | 55 | #Graph the data set through ggplot 2 56 | 57 | ggplot(groupSummaries, aes(x=month, y=N, color=Event.Clearance.SubGroup) )+ 58 | geom_line() + 59 | theme(legend.position="bottom",legend.text=element_text(size=7), 60 | legend.title = element_blank()) + 61 | scale_x_discrete(name ="Month", 62 | limits=c(3,6,9,12)) 63 | 64 | # Create a data frame with only events types that have had a peak of 95 calls in a month or more 65 | 66 | groupSummariesF <- groupSummaries %>% 67 | group_by(Event.Clearance.SubGroup) %>% 68 | filter(max(N) > 95) %>% 69 | ungroup() 70 | 71 | head(groupSummariesF) 72 | 73 | ### The Old School Way: LAYERING ### 74 | 75 | # Create a layered plot with one layer of grey data for the full data set and one layer of color data for the subset data set 76 | 77 | ggplot() + 78 | geom_line(aes(month, N, group = Event.Clearance.SubGroup), 79 | data = groupSummaries, colour = alpha("grey", 0.7)) + 80 | geom_line(aes(month, N, group = Event.Clearance.SubGroup, colour = Event.Clearance.SubGroup), 81 | data = groupSummariesF) +  82 | scale_x_discrete(name ="Month", 83 | limits=c(3,6,9,12)) + 84 | theme(legend.position="bottom",legend.text=element_text(size=7), 85 | legend.title = element_blank()) 86 | 87 | ### The New School Way: GGHIGHLIGHT ### 88 | 89 | install.packages("gghighlight") 90 | library(gghighlight) 91 | 92 | ggplot(groupSummaries, aes(month, N, colour = Event.Clearance.SubGroup)) + 93 | geom_line() + 94 | gghighlight(max(N) > 95, label_key = Event.Clearance.SubGroup) + 95 | scale_x_discrete(name ="Month", 96 | limits=c(3,6,9,12)) 97 | 98 | ## Try a scatterplot chart 99 | 100 | ggplot(groupSummaries, aes(month, N, colour = Event.Clearance.SubGroup, use_group_by=FALSE)) + 101 | geom_point() + 102 | gghighlight(N > 200, label_key = Event.Clearance.SubGroup) + 103 | scale_x_discrete(name ="Month", 104 | limits=c(3,6,9,12)) 105 | 106 | ## Try a histogram chart 107 | 108 | ggplot(groupSummaries, aes(N, fill = Event.Clearance.SubGroup)) + 109 | geom_histogram() + 110 | theme(legend.position="bottom",legend.text=element_text(size=7), 111 | legend.title = element_blank()) + 112 | gghighlight(N > 100, label_key = Event.Clearance.SubGroup, use_group_by = FALSE) + 113 | facet_wrap(~ Event.Clearance.SubGroup) 114 | 115 | 116 | -------------------------------------------------------------------------------- /gghighlight/readme.md: -------------------------------------------------------------------------------- 1 | ## GGhighlight 2 | 3 | A tutorial showing how to highlight a portion of your data on ggplot2 the old school way (layering) and the new school way (gghighlight). 4 | 5 | Tutorial available [here](https://www.littlemissdata.com/blog/highlight) 6 | -------------------------------------------------------------------------------- /ggmap/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/.DS_Store -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/.DS_Store -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-10-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-10-1.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-11-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-11-1.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-12-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-12-1.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-13-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-13-1.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-14-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-14-1.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-14-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-14-2.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-4-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-4-1.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-5-1.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-6-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-6-1.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-7-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-7-1.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-8-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-8-1.png -------------------------------------------------------------------------------- /ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-9-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggmap/Updated-ggmap-08_30_2019__No_Key_files/figure-markdown_github/unnamed-chunk-9-1.png -------------------------------------------------------------------------------- /ggmap/incidentMap.R: -------------------------------------------------------------------------------- 1 | 2 | #################### Import and Install Packages #################### 3 | 4 | install.packages("lubridate") 5 | install.packages("ggplot2") 6 | install.packages("data.table") 7 | install.packages("ggrepel") 8 | install.packages("dplyr") 9 | 10 | 11 | library(lubridate) 12 | library(ggplot2) 13 | library(dplyr) 14 | library(data.table) 15 | library(ggrepel) 16 | 17 | #################### Import Data and Create Sample #################### 18 | 19 | #1) Download the main data set with the crime data 20 | 21 | #If you want to include the full data set from the website, we would run this code: 22 | #incidents <-read.table("https://data.seattle.gov/api/views/3k2p-39jp/rows.csv?accessType=DOWNLOAD", head=TRUE, sep=",", fill=TRUE, stringsAsFactors=F) 23 | 24 | #However, I took a sample of the data and uploaded to GH just to make the data set a little smaller for faster 25 | # processing time. 26 | #Code for sampling: 27 | #i2Sample <- incidents[sample(1:nrow(incidents), 50000, replace=FALSE),] 28 | #write.csv(i2Sample, "i2Sample.csv") 29 | 30 | #If the above data set is unavailable please use this code 31 | df= read.csv('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/ggmap/i2Sample.csv', stringsAsFactors = FALSE) 32 | incidents <- df 33 | 34 | #2) Download the extra data set with the most dangerous Seattle cities as per: 35 | # https://housely.com/dangerous-neighborhoods-seattle/ 36 | 37 | n <- read.csv('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/ggmap/n.csv', stringsAsFactors = FALSE) 38 | 39 | # Look at the data sets 40 | dim(incidents) 41 | head(incidents) 42 | attach(incidents) 43 | 44 | 45 | dim(n) 46 | head(n) 47 | attach(n) 48 | 49 | col1 = "#011f4b" 50 | col2 = "#6497b1" 51 | col3 = "#b3cde0" 52 | col4 = "#CC0000" 53 | 54 | #################### Transform #################### 55 | #add year to the incidents data frame 56 | incidents$ymd <-mdy_hms(Event.Clearance.Date) 57 | incidents$year <- year(incidents$ymd) 58 | 59 | unique(incidents$year) 60 | 61 | #Create a more manageable data frame with only 2017 and 2018 data 62 | i2 <- incidents %>% 63 | filter(year>=2017 & year<=2018) 64 | 65 | #Only include complete cases 66 | i2[complete.cases(i2), ] 67 | 68 | #create a display label to the n data frame (dangerous neighbourhoods) 69 | n$label <-paste(Rank, Location, sep="-") 70 | 71 | attach(i2) 72 | attach(n) 73 | 74 | head(i2) 75 | head(n) 76 | 77 | #################### GGMap Install and Load #################### 78 | 79 | #More Q&A - https://github.com/dkahle/ggmap/issues/51 80 | 81 | #Get the latest Install 82 | 83 | if(!requireNamespace("devtools")) install.packages("devtools") 84 | devtools::install_github("dkahle/ggmap", ref = "tidyup", force=TRUE) 85 | library("ggmap") 86 | ggmap::register_google(key = "SET YOUR KEY HERE") 87 | 88 | #If you get a failure then pls restart R and run the library and register google commands again. 89 | 90 | 91 | 92 | #################### Maps #################### 93 | 94 | ##1) Create a map with all of the crime locations plotted. 95 | 96 | p <- ggmap(get_googlemap(center = c(lon = -122.335167, lat = 47.608013), 97 | zoom = 11, scale = 2, 98 | maptype ='terrain', 99 | color = 'color')) 100 | p + geom_point(aes(x = Longitude, y = Latitude, colour = Initial.Type.Group), data = i2, size = 0.5) + 101 | theme(legend.position="bottom") 102 | 103 | ##2) Deal with the heavy population by using alpha to make the points transparent. 104 | #Stick to one color as the colors are going to get faded out with alpha anyway 105 | #therefore no need for a legend 106 | p + geom_point(aes(x = Longitude, y = Latitude), colour = col1, data = i2, alpha=0.25, size = 0.5) + 107 | theme(legend.position="none") 108 | 109 | 110 | # #3) Do the same as above but do big points on the graph for icons, 111 | # but use the shape to identify the "most dangerous" neighbourhoods. 112 | # Need to use the factor version + scale_shape_manual to get more than 6 default shapes 113 | 114 | n$Neighbourhood <- factor(n$Location) 115 | p + geom_point(aes(x = Longitude, y = Latitude), colour = col1, data = i2, alpha=0.25, size = 0.5) + 116 | theme(legend.position="bottom") + 117 | geom_point(aes(x = x, y = y, shape=Neighbourhood, stroke = 2), colour=col4, data = n, size =3) + 118 | scale_shape_manual(values=1:nlevels(n$Neighbourhood)) 119 | 120 | ## 4) Don't use shapes, instead use labels. Need to use geom_label_repel, 121 | # since there are multiple layers using different data sets. 122 | 123 | p + geom_point(aes(x = Longitude, y = Latitude), colour = col1, data = i2, alpha=0.25, size = 0.5) + 124 | theme(legend.position="bottom") + 125 | geom_point(aes(x = x, y = y, stroke = 2), colour=col4, data = n, size =2.5) + 126 | geom_label_repel( 127 | aes(x, y, label = label), 128 | data=n, 129 | family = 'Times', 130 | size = 4, 131 | box.padding = 0.2, point.padding = 0.3, 132 | segment.color = 'grey50') 133 | 134 | 135 | ## 5) Do a more fair check to see if the neighbourhoods are more "dangerous" b/c of dangerous 136 | # crimes (vs all incidents) 137 | 138 | unique(i2$Event.Clearance.Group) 139 | i2Dangerous <-filter(i2, Event.Clearance.Group %in% c('TRESPASS', 'ASSAULTS', 'SUSPICIOUS CIRCUMSTANCES', 140 | 'BURGLARY', 'PROWLER', 'ASSAULTS', 'PROPERTY DAMAGE', 141 | 'ARREST', 'NARCOTICS COMPLAINTS','THREATS, HARASSMENT', 'WEAPONS CALLS', 142 | 'PROSTITUTION' , 'ROBBERY', 'FAILURE TO REGISTER (SEX OFFENDER)', 'LEWD CONDUCT', 143 | 'HOMICIDE')) 144 | 145 | attach(i2Dangerous) 146 | head(i2Dangerous) 147 | 148 | 149 | p + geom_point(aes(x = Longitude, y = Latitude), colour = col1, data = i2Dangerous, alpha=0.25, size = 0.5) + 150 | theme(legend.position="bottom") + 151 | geom_point(aes(x = x, y = y, stroke = 2), colour=col4, data = n, size =1.5) + 152 | geom_label_repel( 153 | aes(x, y, label = label), 154 | data=n, 155 | family = 'Times', 156 | size = 3, 157 | box.padding = 0.2, point.padding = 0.3, 158 | segment.color = 'grey50') 159 | 160 | 161 | 162 | ## 6) View in a density Plot 163 | p + stat_density2d( 164 | aes(x = Longitude, y = Latitude, fill = ..level.., alpha = 0.25), 165 | size = 0.01, bins = 30, data = i2Dangerous, 166 | geom = "polygon" 167 | ) + 168 | geom_point(aes(x = x, y = y, stroke = 2), colour=col4, data = n, size =1.5) + 169 | geom_label_repel( 170 | aes(x, y, label = label), 171 | data=n, 172 | family = 'Times', 173 | size = 3, 174 | box.padding = 0.2, point.padding = 0.3, 175 | segment.color = 'grey50') 176 | 177 | 178 | #7) Density with alpha set to level 179 | ## have to make a lot of bins given the difference in volumes. Add geom_density to put in grid lines 180 | p + stat_density2d( 181 | aes(x = Longitude, y = Latitude, fill = ..level.., alpha = 0.25), 182 | size = 0.1, bins = 40, data = i2Dangerous, 183 | geom = "polygon" 184 | ) + 185 | geom_density2d(data = i2, 186 | aes(x = Longitude, y = Latitude), size = 0.3) + 187 | geom_point(aes(x = x, y = y, stroke = 2), colour=col4, data = n, size =1.5)+ 188 | geom_label_repel( 189 | aes(x, y, label = label), 190 | data=n, 191 | family = 'Times', 192 | size = 3, 193 | box.padding = 0.2, point.padding = 0.3, 194 | segment.color = 'grey50') 195 | 196 | 197 | #8) Density chart with graph lines and facet wrap 198 | 199 | i2Sub <-filter(i2, Event.Clearance.Group %in% c('TRAFFIC RELATED CALLS', 'DISTURBANCES', 'SUSPICIOUS CIRCUMSTANCES', 'MOTOR VEHICLE COLLISION INVESTIGATION')) 200 | dim(i2Sub) 201 | attach(i2Sub) 202 | 203 | p + stat_density2d( 204 | aes(x = Longitude, y = Latitude, fill = ..level.., alpha =..level..), 205 | size = 0.2, bins = 30, data = i2Sub, 206 | geom = "polygon" 207 | ) + 208 | geom_density2d(data = i2Sub, 209 | aes(x = Longitude, y = Latitude), size = 0.3) + 210 | facet_wrap(~ Event.Clearance.Group, nrow=2) 211 | 212 | #9) Switch the background in ggmap, 213 | #use maptype = roadmap, terrain, hybrid, satelitte 214 | 215 | #9a) roadmap + black geom_point 216 | p + geom_point(aes(x = Longitude, y = Latitude), colour = "black", data = i2, alpha=0.3, size = 0.5) + 217 | theme(legend.position="none") 218 | 219 | #9b) satellite + red geom_point 220 | p + geom_point(aes(x = Longitude, y = Latitude), colour = col4, data = i2, alpha=0.3, size = 0.5) + 221 | theme(legend.position="none") 222 | 223 | 224 | # 10a) terrain-lines + blue density 225 | center = c(lon = -122.335167, lat = 47.608013) 226 | qmap(center, zoom = 11, source = "stamen", maptype = "terrain-lines") + 227 | stat_density2d( 228 | aes(x = Longitude, y = Latitude, fill = ..level..), 229 | alpha = 0.25, size = 0.2, bins = 30, data = i2, 230 | geom = "polygon" 231 | ) + geom_density2d(data = i2, 232 | aes(x = Longitude, y = Latitude), size = 0.3) + 233 | scale_fill_gradient(low = "light blue", high= "dark blue") 234 | 235 | # 10b) watercolor + red geom_point 236 | center = c(lon = -122.335167, lat = 47.608013) 237 | qmap(center, zoom = 11, source = "stamen", maptype = "watercolor") + 238 | geom_point(aes(x = Longitude, y = Latitude), colour = col4, data = i2, alpha=0.3, size = 0.5) + 239 | theme(legend.position="none") 240 | 241 | 242 | # Extra) Check the subset of top 4 all incidents 243 | 244 | # Geompoint facet wrap 245 | p + geom_point(aes(x = Longitude, y = Latitude, colour = Event.Clearance.Group), data = i2Sub, alpha=0.1, size = 2) + 246 | theme(legend.position="bottom") + 247 | facet_wrap(~ Event.Clearance.Group, nrow=2) 248 | 249 | 250 | #Useful background Articles on ggmaps 251 | # a) https://journal.r-project.org/archive/2013-1/kahle-wickham.pdf 252 | # b) quick guide: https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/ggmap/ggmapCheatsheet.pdf 253 | 254 | -------------------------------------------------------------------------------- /ggmap/n.csv: -------------------------------------------------------------------------------- 1 | x,y,Location,Rank 2 | -122.325736,47.531094,South Park,1 3 | -122.263976,47.512255,Rainier Beach,2 4 | -122.281492,47.538004,Othello,3 5 | -122.300937,47.552585,Beacon Hill,4 6 | -122.328778,47.601723,Yesler,5 7 | 122.295186,47.719293,Lake City,6 8 | -122.34053,47.466838,Burien,7 9 | -122.355116,47.508567,White Center,8 10 | -122.239106,47.492418,Skyway,9 11 | -122.346354,47.676696,Aurora Ave,10 -------------------------------------------------------------------------------- /ggplotly/Median_Earnings_of_Creative_Sector_Occupations__CLL.B.1.csv: -------------------------------------------------------------------------------- 1 | SOC Code,Occupation,2016 Median hourly earnings,2017 Median hourly earnings 2 | 11-2011,Advertising and promotions managers,30.11,29.73 3 | 13-1011,"Agents and business managers of artists, performers, and athletes",21.83,22.31 4 | 25-1099,Postsecondary teachers,26.92,26.31 5 | 25-4011,Archivists,26.66,27.31 6 | 25-4012,Curators,32.56,30.46 7 | 25-4013,Museum technicians and conservators,24.33,25.74 8 | 25-4021,Librarians,29.83,30.4 9 | 25-4031,Library technicians,14.66,17.47 10 | 25-9011,Audio-visual and multimedia collections specialists,28.15,28.42 11 | 27-1011,Art directors,18.52,18.09 12 | 27-1012,Craft artists,6.79,6.88 13 | 27-1013,"Fine artists, including painters, sculptors, and illustrators",9.89,9.53 14 | 27-1014,Multimedia artists and animators,14.18,22.13 15 | 27-1019,"Artists and related workers, all other",11.29,11.14 16 | 27-1021,Commercial and industrial designers,26.07,25.75 17 | 27-1022,Fashion designers,28.58,29.46 18 | 27-1024,Graphic designers,20.24,20.66 19 | 27-1026,Merchandise displayers and window trimmers,11.62,12.67 20 | 27-1027,Set and exhibit designers,21.12,22.34 21 | 27-1029,"Designers, all other",23.78,24.12 22 | 27-2011,Actors,16.45,15.76 23 | 27-2012,Producers and directors,22.5,24.34 24 | 27-2031,Dancers,14.74,16 25 | 27-2032,Choreographers,26.36,27.91 26 | 27-2041,Music directors and composers,15.52,15.28 27 | 27-2042,Musicians and singers,17.49,17.68 28 | 27-2099,"Entertainers and performers, sports, and related workers *",21.07,21.1 29 | 27-3011,Radio and television announcers,19.79,19.43 30 | 27-3041,Editors,22.35,23.34 31 | 27-3043,Writers and authors,17.45,17.52 32 | 27-3099,"Media and communication workers, all other",22.3,22.15 33 | 27-4011,Audio and video equipment technicians,21.31,20.1 34 | 27-4013,Radio operators *,22.52,17.6 35 | 27-4014,Sound engineering technicians,24.25,25.53 36 | 27-4021,Photographers,15.69,15.68 37 | 27-4031,"Camera operators, television, video, and motion picture",22.37,21.39 38 | 27-4032,Film and video editors,27.53,27.72 39 | 27-4099,"Media and communication equipment workers, all other",34.37,34.37 40 | 39-3031,"Ushers, lobby attendants, and ticket takers",9.63,9.77 41 | 39-3092,Costume attendants,22.39,27.52 42 | 39-3099,"Entertainment attendants and related workers, all other",11.02,10.83 43 | 39-5091,"Makeup artists, theatrical and performance",29.1,28.01 44 | 43-4121,"Library assistants, clerical",13.88,13.98 45 | 49-9063,Musical instrument repairers and tuners,11.82,12.47 46 | 51-6051,"Sewers, hand *",9.6,9.36 47 | 51-6052,"Tailors, dressmakers, and custom sewers *",11.87,11.78 48 | 51-7011,Cabinetmakers and bench carpenters *,14.24,15.22 49 | 51-7031,"Model makers, wood *",24.23,25.53 50 | 51-9051,"Furnace, kiln, oven, drier, and kettle operators and tenders *",17.39,19.02 51 | 51-9071,Jewelers and precious stone and metal workers,14.93,15.32 52 | 51-9151,Photographic process workers and processing machine operators *,12.95,13.8 53 | 51-9194,Etchers and engravers *,14.45,14.59 54 | 51-9195,"Molders, shapers, and casters (except metal and plastic) *",13.89,15.57 55 | Total,,20.24,20.1 56 | -------------------------------------------------------------------------------- /ggplotly/plotlyExample_files/figure-markdown_github/unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggplotly/plotlyExample_files/figure-markdown_github/unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /ggplotly/plotlyExample_files/figure-markdown_github/unnamed-chunk-4-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggplotly/plotlyExample_files/figure-markdown_github/unnamed-chunk-4-1.png -------------------------------------------------------------------------------- /ggplotly/readme.md: -------------------------------------------------------------------------------- 1 | Install and Load Packages 2 | ------------------------- 3 | 4 | ``` r 5 | #installs for the tutorial 6 | #install.packages("plotly") 7 | #install.packages("tidyverse") 8 | #install.packages("htmlwidgets") 9 | 10 | #installs for the rmd file 11 | #install.packages("webshot") 12 | #webshot::install_phantomjs() 13 | 14 | library(plotly) 15 | ``` 16 | 17 | ## Loading required package: ggplot2 18 | 19 | ## 20 | ## Attaching package: 'plotly' 21 | 22 | ## The following object is masked from 'package:ggplot2': 23 | ## 24 | ## last_plot 25 | 26 | ## The following object is masked from 'package:stats': 27 | ## 28 | ## filter 29 | 30 | ## The following object is masked from 'package:graphics': 31 | ## 32 | ## layout 33 | 34 | ``` r 35 | library(tidyverse) 36 | ``` 37 | 38 | ## ── Attaching packages ─────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ── 39 | 40 | ## ✔ tibble 2.0.1 ✔ purrr 0.3.0 41 | ## ✔ tidyr 0.8.2 ✔ dplyr 0.7.8 42 | ## ✔ readr 1.2.1 ✔ stringr 1.4.0 43 | ## ✔ tibble 2.0.1 ✔ forcats 0.3.0 44 | 45 | ## ── Conflicts ────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── 46 | ## ✖ dplyr::filter() masks plotly::filter(), stats::filter() 47 | ## ✖ dplyr::lag() masks stats::lag() 48 | 49 | ``` r 50 | library(htmlwidgets) 51 | ``` 52 | 53 | Import Data 54 | ----------- 55 | 56 | ``` r 57 | creatives <- read.csv(file="https://raw.githubusercontent.com/lgellis/MiscTutorial/master/ggplotly/Median_Earnings_of_Creative_Sector_Occupations__CLL.B.1.csv", 58 | header=TRUE, sep=",", stringsAsFactors = FALSE) 59 | 60 | head(creatives) 61 | ``` 62 | 63 | ## SOC.Code 64 | ## 1 11-2011 65 | ## 2 13-1011 66 | ## 3 25-1099 67 | ## 4 25-4011 68 | ## 5 25-4012 69 | ## 6 25-4013 70 | ## Occupation 71 | ## 1 Advertising and promotions managers 72 | ## 2 Agents and business managers of artists, performers, and athletes 73 | ## 3 Postsecondary teachers 74 | ## 4 Archivists 75 | ## 5 Curators 76 | ## 6 Museum technicians and conservators 77 | ## X2016.Median.hourly.earnings X2017.Median.hourly.earnings 78 | ## 1 30.11 29.73 79 | ## 2 21.83 22.31 80 | ## 3 26.92 26.31 81 | ## 4 26.66 27.31 82 | ## 5 32.56 30.46 83 | ## 6 24.33 25.74 84 | 85 | Format Data and create interactive scatterplot 86 | ---------------------------------------------- 87 | 88 | ``` r 89 | ##### Data processing 90 | #Rename the columns and create a new column for year over year change 91 | 92 | creatives <- creatives %>% 93 | rename(Median_2016 = X2016.Median.hourly.earnings, Median_2017 = X2017.Median.hourly.earnings) %>% 94 | rowwise() %>% 95 | mutate(Percent_Improvement = round((Median_2017-Median_2016)/Median_2016*100,2)) 96 | 97 | 98 | ##### Create a scatterPlot 99 | #Note - The chart is not interactive in .md format. Please view on littlemissdata.com/blog/interactiveplots 100 | 101 | scatterPlot <- creatives %>% 102 | ggplot(aes(x = Median_2017, y = Percent_Improvement)) + 103 | geom_point(alpha=0.7, colour = "#51A0D5") + 104 | labs(x = "Median Occupation Hourly Wage in 2017", 105 | y = "% Improvement Year over Year (2016 to 2017)", 106 | title = "Austin Creative Occupations Median Hourly Wage") + 107 | geom_hline(yintercept=0, linetype="dashed", color = "#2C528C", size=0.5) + 108 | theme_classic() 109 | 110 | ggplotly(scatterPlot) 111 | ``` 112 | 113 | ![](plotlyExample_files/figure-markdown_github/unnamed-chunk-3-1.png) 114 | 115 | Adding text formatting for the mouseover 116 | ---------------------------------------- 117 | 118 | ``` r 119 | #Note - The chart is not interactive in .md format. Please view on littlemissdata.com/blog/interactiveplots 120 | scatterPlot <- creatives %>% 121 | ggplot(aes(x = Median_2017, y = Percent_Improvement, 122 | text = paste( 123 | "Occupation: ", Occupation, "\n", 124 | "2017: ", Median_2017, "\n", 125 | "2016: ", Median_2016, "\n", 126 | "% Improvement Year over Year: ", Percent_Improvement, "\n", 127 | sep = "" 128 | ))) + 129 | geom_point(alpha=0.7, colour = "#51A0D5") + 130 | labs(x = "Median Occupation Hourly Wage in 2017", 131 | y = "% Improvement Year over Year (2016 to 2017)", 132 | title = "Austin Creative Occupations Median Hourly Wage") + 133 | geom_hline(yintercept=0, linetype="dashed", color = "#2C528C", size=0.5) + 134 | theme_classic() 135 | 136 | ggplotly(scatterPlot, tooltip = "text") 137 | ``` 138 | 139 | ![](plotlyExample_files/figure-markdown_github/unnamed-chunk-4-1.png) 140 | 141 | System Information 142 | ------------------ 143 | 144 | ``` r 145 | sessionInfo() 146 | ``` 147 | 148 | ## R version 3.5.2 (2018-12-20) 149 | ## Platform: x86_64-apple-darwin15.6.0 (64-bit) 150 | ## Running under: macOS High Sierra 10.13.6 151 | ## 152 | ## Matrix products: default 153 | ## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib 154 | ## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib 155 | ## 156 | ## locale: 157 | ## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 158 | ## 159 | ## attached base packages: 160 | ## [1] stats graphics grDevices utils datasets methods base 161 | ## 162 | ## other attached packages: 163 | ## [1] bindrcpp_0.2.2 htmlwidgets_1.3 forcats_0.3.0 stringr_1.4.0 164 | ## [5] dplyr_0.7.8 purrr_0.3.0 readr_1.2.1 tidyr_0.8.2 165 | ## [9] tibble_2.0.1 tidyverse_1.2.1 plotly_4.8.0 ggplot2_3.1.0 166 | ## 167 | ## loaded via a namespace (and not attached): 168 | ## [1] Rcpp_1.0.0 lubridate_1.7.4 lattice_0.20-38 169 | ## [4] ps_1.3.0 assertthat_0.2.0 digest_0.6.18 170 | ## [7] mime_0.6 R6_2.4.0 cellranger_1.1.0 171 | ## [10] plyr_1.8.4 backports_1.1.2 evaluate_0.13 172 | ## [13] httr_1.4.0 pillar_1.3.1 rlang_0.3.1 173 | ## [16] lazyeval_0.2.1 readxl_1.1.0 rstudioapi_0.9.0 174 | ## [19] data.table_1.11.8 callr_3.1.1 rmarkdown_1.11 175 | ## [22] labeling_0.3 webshot_0.5.1 munsell_0.5.0 176 | ## [25] shiny_1.2.0 broom_0.5.1 compiler_3.5.2 177 | ## [28] httpuv_1.4.5.1 modelr_0.1.2 xfun_0.5 178 | ## [31] pkgconfig_2.0.2 htmltools_0.3.6 tidyselect_0.2.5 179 | ## [34] viridisLite_0.3.0 crayon_1.3.4 withr_2.1.2 180 | ## [37] later_0.8.0 grid_3.5.2 nlme_3.1-137 181 | ## [40] jsonlite_1.6 xtable_1.8-3 gtable_0.2.0 182 | ## [43] magrittr_1.5 scales_1.0.0 cli_1.0.1 183 | ## [46] stringi_1.3.1 promises_1.0.1 xml2_1.2.0 184 | ## [49] generics_0.0.2 tools_3.5.2 Cairo_1.5-9 185 | ## [52] glue_1.3.0 hms_0.4.2 crosstalk_1.0.0 186 | ## [55] processx_3.2.1 yaml_2.2.0 colorspace_1.4-0 187 | ## [58] rvest_0.3.2 knitr_1.21 bindr_0.1.1 188 | ## [61] haven_2.0.0 189 | -------------------------------------------------------------------------------- /ggplotly/readme_files/figure-markdown_github/unnamed-chunk-1-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggplotly/readme_files/figure-markdown_github/unnamed-chunk-1-1.png -------------------------------------------------------------------------------- /ggplotly/readme_files/figure-markdown_github/unnamed-chunk-1-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggplotly/readme_files/figure-markdown_github/unnamed-chunk-1-2.png -------------------------------------------------------------------------------- /ggplotly/readme_files/figure-markdown_github/unnamed-chunk-2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggplotly/readme_files/figure-markdown_github/unnamed-chunk-2-1.png -------------------------------------------------------------------------------- /ggplotly/readme_files/figure-markdown_github/unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/ggplotly/readme_files/figure-markdown_github/unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /gtrendsR/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/.DS_Store -------------------------------------------------------------------------------- /gtrendsR/Bachelor trends: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/Bachelor trends -------------------------------------------------------------------------------- /gtrendsR/bachelorListing.csv: -------------------------------------------------------------------------------- 1 | season,topic,startDate,endDate 2 | Nick Viall,The Bachelor,1/2/17,3/13/17 3 | Arie Luyendyk Jr,The Bachelor,1/1/18,3/6/18 4 | Colton Underwood,The Bachelor,1/7/19,3/12/19 5 | Rachel Lindsay,The Bachelorette,5/22/17,8/7/17 6 | Becca Kufrin,The Bachelorette,5/28/18,8/6/18 7 | Hannah Brown,The Bachelorette,5/13/19,7/30/19 8 | Season 4,Bachelor in Paradise,8/14/17,9/11/17 9 | Season 5,Bachelor in Paradise,8/7/18,9/11/18 10 | Season 6,Bachelor in Paradise,8/5/19,9/17/19 -------------------------------------------------------------------------------- /gtrendsR/gtrends.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Bachelor Ratings" 3 | author: Laura Ellis 4 | date: Sept 24, 2019 5 | output: 6 | md_document: 7 | variant: markdown_github 8 | --- 9 | 10 | #Introduction 11 | 12 | In this blog post, I will be analyzing the relative popularity of the bachelor franchise seasons as measured by their google search popularity. 13 | 14 | In this tutorial I will be using the [gtrendsR package](https://cran.r-project.org/web/packages/gtrendsR/gtrendsR.pdf) to gather Google trend information, dplyr to format the data, ggplot2 to create the graphs, gganimate to make an animation and geom_image to create custom lollipop charts. 15 | 16 | 17 | #Load all of the packages and install if necessary 18 | 19 | In my [latest blog post](https://www.littlemissdata.com/blog/iconmap), someone kindly suggested that I do an auto check to install all necessary packages before loading them. After a quick search, I found this code below to efficiently install and load packages in [Vikram Baliga's Blog](https://www.vikram-baliga.com/blog/2015/7/19/a-hassle-free-way-to-verify-that-r-packages-are-installed-and-loaded) 20 | 21 | ```{r} 22 | #specify the packages of interest 23 | packages = c("gtrendsR","tidyverse","gifski", "gganimate", "ggimage", "lubridate") 24 | 25 | #use this function to check if each package is on the local machine 26 | #if a package is installed, it will be loaded 27 | #if any are not, the missing package(s) will be installed and loaded 28 | package.check <- lapply(packages, FUN = function(x) { 29 | if (!require(x, character.only = TRUE)) { 30 | install.packages(x, dependencies = TRUE) 31 | library(x, character.only = TRUE) 32 | } 33 | }) 34 | 35 | #verify they are loaded 36 | search() 37 | ``` 38 | 39 | # Set the color variables 40 | 41 | To ensure consistent and effective color formatting, I am setting the color variables up front. 42 | 43 | ```{r} 44 | pink <- "#FF8DC6" 45 | blue <- "#56C1FF" 46 | yellow <- "#FAE232" 47 | ``` 48 | 49 | # Gather the ratings data for: The Bachelor, The Bachelorette and Bachelor in Paradise 50 | 51 | Using the gtrendsR package, load weekly US ratings for "Bachelor in Paradise", "The Bachelor" and "The Bachelorette". The hits are calculated with a relative max of 100 to show the relative max hits over the time range and search subjects. 52 | 53 | Plot the trends with the plot() function. 54 | 55 | ```{r} 56 | bachTrends <- gtrends(c("Bachelor in Paradise", "The Bachelor", "The Bachelorette"), geo ="US") 57 | plot(bachTrends) 58 | bachTrendsInterest <- bachTrends$interest_over_time 59 | ``` 60 | 61 | # Transform the data 62 | 63 | Filter to data that is 2017+ 64 | Convert hits to numeric as the default is character 65 | 66 | 67 | ```{r} 68 | trends <- bachTrendsInterest %>% 69 | filter(year(date)>2016) %>% 70 | mutate(date = ymd(date), 71 | hits = as.numeric(hits)) 72 | ``` 73 | 74 | # Create the same plot with ggplot2 75 | 76 | Create the basic plot of relative search popularity by search critera with the ggplot2 package. Transitioning to ggplot2 in order to use the ggplot2 features and complimentary packages like ggimage and gganimate. 77 | 78 | ```{r} 79 | #Frequency plot by keyword 80 | p <- ggplot() + 81 | geom_line(data=trends, aes(x=date, y=hits, group=keyword, color = keyword)) + 82 | scale_color_manual(values=c( yellow, blue, pink)) + 83 | theme_classic() + 84 | theme(legend.position="bottom") + 85 | labs(title = "The Bachelor Franchise Popularity ", 86 | subtitle = "Using data to find the most dramatic season ever!", 87 | caption = "Source: @littlemissdata", 88 | x = "Date", y = "Hits") 89 | p 90 | ``` 91 | 92 | # Create an animation 93 | 94 | Take the basic plot and make an animation out of it with the gganimate() package. 95 | 96 | ```{r} 97 | t <- p + 98 | transition_reveal(as.numeric(date)) 99 | gif <- animate(t, end_pause = 25, width = 800, height = 400, fps = 8) 100 | gif 101 | anim_save("Bachelor trends", gif) 102 | 103 | ``` 104 | 105 | # Bring in meta data about bachelor franchise shows 106 | 107 | We are going to bring in a data set which has the start dates for every single season of the Bachelor franchise. We will then do some data munging to find the closest ratings date to the season start date. With this info we will join the bachelor season metadata to the ratings table. 108 | 109 | ```{r} 110 | ## Add lollipops 111 | x <-read.csv("https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/bachelorListing.csv", stringsAsFactors = FALSE) 112 | 113 | # Turn the dates into proper dates. 114 | #Ratings are only tracked on sundays so get the closest Sunday for ratings 115 | x <-x %>% 116 | mutate(startDate = ymd(as.Date(startDate, "%m/%d/%y")), 117 | endDate = ymd(as.Date(endDate, "%m/%d/%y")), 118 | ratingStartDate = floor_date(startDate, "weeks"), 119 | ratingEndDate = floor_date(endDate, "weeks")) 120 | x 121 | 122 | #Ratings are typically highest at the beginning 123 | x<-left_join(x, trends, by = c("topic"= "keyword", "ratingEndDate"="date")) 124 | 125 | ``` 126 | 127 | # Get the images for each of the seasons 128 | 129 | I have some plans to layer on a lollipop graph with the image of the bachelor season and display it at the height of the number of hits on the last day of the season. As such, I need to assign an image to every single season. 130 | 131 | ```{r} 132 | 133 | x <-x %>% 134 | mutate(Image = case_when(season == "Nick Viall" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Nick.png", 135 | season == "Arie Luyendyk Jr" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Arie.png", 136 | season == "Colton Underwood" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Colton.png", 137 | season == "Rachel Lindsay" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Rachel.png", 138 | season == "Becca Kufrin" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Becca.png", 139 | season == "Hannah Brown" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Hannah.png", 140 | topic == "Bachelor in Paradise" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/BIP.png")) 141 | 142 | 143 | ``` 144 | 145 | 146 | # Create the mega chart 147 | 148 | Create a fun graph to display the relative ratings for each season by layering on a lollipop chart to represent the seasons and their relative search popularity for the last week of the season. Use the geom_segment() function to set the lollipop stem and the geom_image() function to set the lolipop circle with the image representing the season. 149 | 150 | ```{r, fig.width = 8, fig.height=8} 151 | p <- ggplot() + 152 | geom_line(data=trends, aes(x=date, y=hits, group=keyword, color = keyword), size=1) + 153 | scale_color_manual(values=c(yellow, blue, pink)) + 154 | geom_segment(data=x, aes(x=ratingEndDate, 155 | xend=ratingEndDate, 156 | y=0, 157 | yend=hits, 158 | color=topic), size=1) + 159 | geom_image(data=x, aes(x=ratingEndDate, y=hits, image=Image), size=0.105) + 160 | theme_classic() + 161 | labs(title = "The Bachelor Franchise Popularity ", 162 | subtitle = "Using data to find the most dramatic season ever!", 163 | caption = "Source: @littlemissdata", 164 | x = "Date", y = "Hits") + 165 | theme(legend.position="none", 166 | plot.title = element_text(size = 12, face = "bold"), 167 | plot.subtitle = element_text(size=10, face = "italic"), 168 | plot.caption = element_text(size = 8, face = "italic") ) 169 | 170 | p 171 | ``` 172 | 173 | 174 | -------------------------------------------------------------------------------- /gtrendsR/gtrends.md: -------------------------------------------------------------------------------- 1 | \#Introduction 2 | 3 | In this blog post, I will be analyzing the relative popularity of the 4 | bachelor franchise seasons as measured by their google search 5 | popularity. 6 | 7 | In this tutorial I will be using the [gtrendsR 8 | package](https://cran.r-project.org/web/packages/gtrendsR/gtrendsR.pdf) 9 | to gather Google trend information, dplyr to format the data, ggplot2 to 10 | create the graphs, gganimate to make an animation and geom\_image to 11 | create custom lollipop charts. 12 | 13 | \#Load all of the packages and install if necessary 14 | 15 | In my [latest blog post](https://www.littlemissdata.com/blog/iconmap), 16 | someone kindly suggested that I do an auto check to install all 17 | necessary packages before loading them. After a quick search, I found 18 | this code below to efficiently install and load packages in [Vikram 19 | Baliga’s 20 | Blog](https://www.vikram-baliga.com/blog/2015/7/19/a-hassle-free-way-to-verify-that-r-packages-are-installed-and-loaded) 21 | 22 | ``` r 23 | #specify the packages of interest 24 | packages = c("gtrendsR","tidyverse","gifski", "gganimate", "ggimage", "lubridate") 25 | 26 | #use this function to check if each package is on the local machine 27 | #if a package is installed, it will be loaded 28 | #if any are not, the missing package(s) will be installed and loaded 29 | package.check <- lapply(packages, FUN = function(x) { 30 | if (!require(x, character.only = TRUE)) { 31 | install.packages(x, dependencies = TRUE) 32 | library(x, character.only = TRUE) 33 | } 34 | }) 35 | ``` 36 | 37 | ## Loading required package: gtrendsR 38 | 39 | ## Loading required package: tidyverse 40 | 41 | ## ── Attaching packages ─────────────────────────────────────────────────────────────── tidyverse 1.2.1 ── 42 | 43 | ## ✔ ggplot2 3.2.0 ✔ purrr 0.3.2 44 | ## ✔ tibble 2.1.3 ✔ dplyr 0.8.3 45 | ## ✔ tidyr 0.8.3 ✔ stringr 1.4.0 46 | ## ✔ readr 1.3.1 ✔ forcats 0.4.0 47 | 48 | ## ── Conflicts ────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── 49 | ## ✖ dplyr::filter() masks stats::filter() 50 | ## ✖ dplyr::lag() masks stats::lag() 51 | 52 | ## Loading required package: gifski 53 | 54 | ## Loading required package: gganimate 55 | 56 | ## Loading required package: ggimage 57 | 58 | ## Loading required package: lubridate 59 | 60 | ## 61 | ## Attaching package: 'lubridate' 62 | 63 | ## The following object is masked from 'package:base': 64 | ## 65 | ## date 66 | 67 | ``` r 68 | #verify they are loaded 69 | search() 70 | ``` 71 | 72 | ## [1] ".GlobalEnv" "package:lubridate" "package:ggimage" 73 | ## [4] "package:gganimate" "package:gifski" "package:forcats" 74 | ## [7] "package:stringr" "package:dplyr" "package:purrr" 75 | ## [10] "package:readr" "package:tidyr" "package:tibble" 76 | ## [13] "package:ggplot2" "package:tidyverse" "package:gtrendsR" 77 | ## [16] "package:stats" "package:graphics" "package:grDevices" 78 | ## [19] "package:utils" "package:datasets" "package:methods" 79 | ## [22] "Autoloads" "package:base" 80 | 81 | Set the color variables 82 | ======================= 83 | 84 | To ensure consistent and effective color formatting, I am setting the 85 | color variables up front. 86 | 87 | ``` r 88 | pink <- "#FF8DC6" 89 | blue <- "#56C1FF" 90 | yellow <- "#FAE232" 91 | ``` 92 | 93 | Gather the ratings data for: The Bachelor, The Bachelorette and Bachelor in Paradise 94 | ==================================================================================== 95 | 96 | Using the gtrendsR package, load weekly US ratings for “Bachelor in 97 | Paradise”, “The Bachelor” and “The Bachelorette”. The hits are 98 | calculated with a relative max of 100 to show the relative max hits over 99 | the time range and search subjects. 100 | 101 | Plot the trends with the plot() function. 102 | 103 | ``` r 104 | bachTrends <- gtrends(c("Bachelor in Paradise", "The Bachelor", "The Bachelorette"), geo ="US") 105 | plot(bachTrends) 106 | ``` 107 | 108 | ![](gtrends_files/figure-markdown_github/unnamed-chunk-3-1.png) 109 | 110 | ``` r 111 | bachTrendsInterest <- bachTrends$interest_over_time 112 | ``` 113 | 114 | Transform the data 115 | ================== 116 | 117 | Filter to data that is 2017+ Convert hits to numeric as the default is 118 | character 119 | 120 | ``` r 121 | trends <- bachTrendsInterest %>% 122 | filter(year(date)>2016) %>% 123 | mutate(date = ymd(date), 124 | hits = as.numeric(hits)) 125 | ``` 126 | 127 | ## Warning: NAs introduced by coercion 128 | 129 | Create the same plot with ggplot2 130 | ================================= 131 | 132 | Create the basic plot of relative search popularity by search critera 133 | with the ggplot2 package. Transitioning to ggplot2 in order to use the 134 | ggplot2 features and complimentary packages like ggimage and gganimate. 135 | 136 | ``` r 137 | #Frequency plot by keyword 138 | p <- ggplot() + 139 | geom_line(data=trends, aes(x=date, y=hits, group=keyword, color = keyword)) + 140 | scale_color_manual(values=c( yellow, blue, pink)) + 141 | theme_classic() + 142 | theme(legend.position="bottom") + 143 | labs(title = "The Bachelor Franchise Popularity ", 144 | subtitle = "Using data to find the most dramatic season ever!", 145 | caption = "Source: @littlemissdata", 146 | x = "Date", y = "Hits") 147 | p 148 | ``` 149 | 150 | ![](gtrends_files/figure-markdown_github/unnamed-chunk-5-1.png) 151 | 152 | Create an animation 153 | =================== 154 | 155 | Take the basic plot and make an animation out of it with the gganimate() 156 | package. 157 | 158 | ``` r 159 | t <- p + 160 | transition_reveal(as.numeric(date)) 161 | gif <- animate(t, end_pause = 25, width = 800, height = 400, fps = 8) 162 | ``` 163 | 164 | ## Warning: Removed 2 rows containing missing values (geom_path). 165 | 166 | ## Warning: Removed 4 rows containing missing values (geom_path). 167 | 168 | ## Warning: Removed 6 rows containing missing values (geom_path). 169 | 170 | ## Warning: Removed 1 rows containing missing values (geom_path). 171 | 172 | ## Warning: Removed 2 rows containing missing values (geom_path). 173 | 174 | ## Warning: Removed 3 rows containing missing values (geom_path). 175 | 176 | ## Warning: Removed 5 rows containing missing values (geom_path). 177 | 178 | ``` r 179 | gif 180 | ``` 181 | 182 | ![](gtrends_files/figure-markdown_github/unnamed-chunk-6-1.gif) 183 | 184 | ``` r 185 | anim_save("Bachelor trends", gif) 186 | ``` 187 | 188 | Bring in meta data about bachelor franchise shows 189 | ================================================= 190 | 191 | We are going to bring in a data set which has the start dates for every 192 | single season of the Bachelor franchise. We will then do some data 193 | munging to find the closest ratings date to the season start date. With 194 | this info we will join the bachelor season metadata to the ratings 195 | table. 196 | 197 | ``` r 198 | ## Add lollipops 199 | x <-read.csv("https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/bachelorListing.csv", stringsAsFactors = FALSE) 200 | 201 | # Turn the dates into proper dates. 202 | #Ratings are only tracked on sundays so get the closest Sunday for ratings 203 | x <-x %>% 204 | mutate(startDate = ymd(as.Date(startDate, "%m/%d/%y")), 205 | endDate = ymd(as.Date(endDate, "%m/%d/%y")), 206 | ratingStartDate = floor_date(startDate, "weeks"), 207 | ratingEndDate = floor_date(endDate, "weeks")) 208 | x 209 | ``` 210 | 211 | ## season topic startDate endDate 212 | ## 1 Nick Viall The Bachelor 2017-01-02 2017-03-13 213 | ## 2 Arie Luyendyk Jr The Bachelor 2018-01-01 2018-03-06 214 | ## 3 Colton Underwood The Bachelor 2019-01-07 2019-03-12 215 | ## 4 Rachel Lindsay The Bachelorette 2017-05-22 2017-08-07 216 | ## 5 Becca Kufrin The Bachelorette 2018-05-28 2018-08-06 217 | ## 6 Hannah Brown The Bachelorette 2019-05-13 2019-07-30 218 | ## 7 Season 4 Bachelor in Paradise 2017-08-14 2017-09-11 219 | ## 8 Season 5 Bachelor in Paradise 2018-08-07 2018-09-11 220 | ## 9 Season 6 Bachelor in Paradise 2019-08-05 2019-09-17 221 | ## ratingStartDate ratingEndDate 222 | ## 1 2017-01-01 2017-03-12 223 | ## 2 2017-12-31 2018-03-04 224 | ## 3 2019-01-06 2019-03-10 225 | ## 4 2017-05-21 2017-08-06 226 | ## 5 2018-05-27 2018-08-05 227 | ## 6 2019-05-12 2019-07-28 228 | ## 7 2017-08-13 2017-09-10 229 | ## 8 2018-08-05 2018-09-09 230 | ## 9 2019-08-04 2019-09-15 231 | 232 | ``` r 233 | #Ratings are typically highest at the beginning 234 | x<-left_join(x, trends, by = c("topic"= "keyword", "ratingEndDate"="date")) 235 | ``` 236 | 237 | Get the images for each of the seasons 238 | ====================================== 239 | 240 | I have some plans to layer on a lollipop graph with the image of the 241 | bachelor season and display it at the height of the number of hits on 242 | the last day of the season. As such, I need to assign an image to every 243 | single season. 244 | 245 | ``` r 246 | x <-x %>% 247 | mutate(Image = case_when(season == "Nick Viall" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Nick.png", 248 | season == "Arie Luyendyk Jr" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Arie.png", 249 | season == "Colton Underwood" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Colton.png", 250 | season == "Rachel Lindsay" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Rachel.png", 251 | season == "Becca Kufrin" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Becca.png", 252 | season == "Hannah Brown" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Hannah.png", 253 | topic == "Bachelor in Paradise" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/BIP.png")) 254 | ``` 255 | 256 | Create the mega chart 257 | ===================== 258 | 259 | Create a fun graph to display the relative ratings for each season by 260 | layering on a lollipop chart to represent the seasons and their relative 261 | search popularity for the last week of the season. Use the 262 | geom\_segment() function to set the lollipop stem and the geom\_image() 263 | function to set the lolipop circle with the image representing the 264 | season. 265 | 266 | ``` r 267 | p <- ggplot() + 268 | geom_line(data=trends, aes(x=date, y=hits, group=keyword, color = keyword), size=1) + 269 | scale_color_manual(values=c(yellow, blue, pink)) + 270 | geom_segment(data=x, aes(x=ratingEndDate, 271 | xend=ratingEndDate, 272 | y=0, 273 | yend=hits, 274 | color=topic), size=1) + 275 | geom_image(data=x, aes(x=ratingEndDate, y=hits, image=Image), size=0.105) + 276 | theme_classic() + 277 | labs(title = "The Bachelor Franchise Popularity ", 278 | subtitle = "Using data to find the most dramatic season ever!", 279 | caption = "Source: @littlemissdata", 280 | x = "Date", y = "Hits") + 281 | theme(legend.position="none", 282 | plot.title = element_text(size = 12, face = "bold"), 283 | plot.subtitle = element_text(size=10, face = "italic"), 284 | plot.caption = element_text(size = 8, face = "italic") ) 285 | 286 | p 287 | ``` 288 | 289 | ![](gtrends_files/figure-markdown_github/unnamed-chunk-9-1.png) 290 | -------------------------------------------------------------------------------- /gtrendsR/gtrends_files/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/gtrends_files/.DS_Store -------------------------------------------------------------------------------- /gtrendsR/gtrends_files/figure-markdown_github/unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/gtrends_files/figure-markdown_github/unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /gtrendsR/gtrends_files/figure-markdown_github/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/gtrends_files/figure-markdown_github/unnamed-chunk-5-1.png -------------------------------------------------------------------------------- /gtrendsR/gtrends_files/figure-markdown_github/unnamed-chunk-6-1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/gtrends_files/figure-markdown_github/unnamed-chunk-6-1.gif -------------------------------------------------------------------------------- /gtrendsR/gtrends_files/figure-markdown_github/unnamed-chunk-9-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/gtrends_files/figure-markdown_github/unnamed-chunk-9-1.png -------------------------------------------------------------------------------- /gtrendsR/images/Arie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/images/Arie.png -------------------------------------------------------------------------------- /gtrendsR/images/BIP.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/images/BIP.png -------------------------------------------------------------------------------- /gtrendsR/images/Becca.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/images/Becca.png -------------------------------------------------------------------------------- /gtrendsR/images/Colton.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/images/Colton.png -------------------------------------------------------------------------------- /gtrendsR/images/Hannah.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/images/Hannah.png -------------------------------------------------------------------------------- /gtrendsR/images/Nick.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/images/Nick.png -------------------------------------------------------------------------------- /gtrendsR/images/Rachel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/images/Rachel.png -------------------------------------------------------------------------------- /gtrendsR/readme.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Bachelor Ratings" 3 | author: Laura Ellis 4 | date: Sept 24, 2019 5 | output: 6 | md_document: 7 | variant: markdown_github 8 | --- 9 | 10 | #Introduction 11 | 12 | In this blog post, I will be analyzing the relative popularity of the bachelor franchise seasons as measured by their google search popularity. 13 | 14 | In this tutorial I will be using the [gtrendsR package](https://cran.r-project.org/web/packages/gtrendsR/gtrendsR.pdf) to gather Google trend information, dplyr to format the data, ggplot2 to create the graphs, gganimate to make an animation and geom_image to create custom lollipop charts. 15 | 16 | 17 | #Load all of the packages and install if necessary 18 | 19 | In my [latest blog post](https://www.littlemissdata.com/blog/iconmap), someone kindly suggested that I do an auto check to install all necessary packages before loading them. After a quick search, I found this code below to efficiently install and load packages in [Vikram Baliga's Blog](https://www.vikram-baliga.com/blog/2015/7/19/a-hassle-free-way-to-verify-that-r-packages-are-installed-and-loaded) 20 | 21 | ```{r} 22 | #specify the packages of interest 23 | packages = c("gtrendsR","tidyverse","gifski", "gganimate", "ggimage", "lubridate") 24 | 25 | #use this function to check if each package is on the local machine 26 | #if a package is installed, it will be loaded 27 | #if any are not, the missing package(s) will be installed and loaded 28 | package.check <- lapply(packages, FUN = function(x) { 29 | if (!require(x, character.only = TRUE)) { 30 | install.packages(x, dependencies = TRUE) 31 | library(x, character.only = TRUE) 32 | } 33 | }) 34 | 35 | #verify they are loaded 36 | search() 37 | ``` 38 | 39 | # Set the color variables 40 | 41 | To ensure consistent and effective color formatting, I am setting the color variables up front. 42 | 43 | ```{r} 44 | pink <- "#FF8DC6" 45 | blue <- "#56C1FF" 46 | yellow <- "#FAE232" 47 | ``` 48 | 49 | # Gather the ratings data for: The Bachelor, The Bachelorette and Bachelor in Paradise 50 | 51 | Using the gtrendsR package, load weekly US ratings for "Bachelor in Paradise", "The Bachelor" and "The Bachelorette". The hits are calculated with a relative max of 100 to show the relative max hits over the time range and search subjects. 52 | 53 | Plot the trends with the plot() function. 54 | 55 | ```{r} 56 | bachTrends <- gtrends(c("Bachelor in Paradise", "The Bachelor", "The Bachelorette"), geo ="US") 57 | plot(bachTrends) 58 | bachTrendsInterest <- bachTrends$interest_over_time 59 | ``` 60 | 61 | # Transform the data 62 | 63 | Filter to data that is 2017+ 64 | Convert hits to numeric as the default is character 65 | 66 | 67 | ```{r} 68 | trends <- bachTrendsInterest %>% 69 | filter(year(date)>2016) %>% 70 | mutate(date = ymd(date), 71 | hits = as.numeric(hits)) 72 | ``` 73 | 74 | # Create the same plot with ggplot2 75 | 76 | Create the basic plot of relative search popularity by search critera with the ggplot2 package. Transitioning to ggplot2 in order to use the ggplot2 features and complimentary packages like ggimage and gganimate. 77 | 78 | ```{r} 79 | #Frequency plot by keyword 80 | p <- ggplot() + 81 | geom_line(data=trends, aes(x=date, y=hits, group=keyword, color = keyword)) + 82 | scale_color_manual(values=c( yellow, blue, pink)) + 83 | theme_classic() + 84 | theme(legend.position="bottom") + 85 | labs(title = "The Bachelor Franchise Popularity ", 86 | subtitle = "Using data to find the most dramatic season ever!", 87 | caption = "Source: @littlemissdata", 88 | x = "Date", y = "Hits") 89 | p 90 | ``` 91 | 92 | # Create an animation 93 | 94 | Take the basic plot and make an animation out of it with the gganimate() package. 95 | 96 | ```{r} 97 | t <- p + 98 | transition_reveal(as.numeric(date)) 99 | gif <- animate(t, end_pause = 25, width = 800, height = 400, fps = 8) 100 | gif 101 | anim_save("Bachelor trends", gif) 102 | 103 | ``` 104 | 105 | # Bring in meta data about bachelor franchise shows 106 | 107 | We are going to bring in a data set which has the start dates for every single season of the Bachelor franchise. We will then do some data munging to find the closest ratings date to the season start date. With this info we will join the bachelor season metadata to the ratings table. 108 | 109 | ```{r} 110 | ## Add lollipops 111 | x <-read.csv("https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/bachelorListing.csv", stringsAsFactors = FALSE) 112 | 113 | # Turn the dates into proper dates. 114 | #Ratings are only tracked on sundays so get the closest Sunday for ratings 115 | x <-x %>% 116 | mutate(startDate = ymd(as.Date(startDate, "%m/%d/%y")), 117 | endDate = ymd(as.Date(endDate, "%m/%d/%y")), 118 | ratingStartDate = floor_date(startDate, "weeks"), 119 | ratingEndDate = floor_date(endDate, "weeks")) 120 | x 121 | 122 | #Ratings are typically highest at the beginning 123 | x<-left_join(x, trends, by = c("topic"= "keyword", "ratingEndDate"="date")) 124 | 125 | ``` 126 | 127 | # Get the images for each of the seasons 128 | 129 | I have some plans to layer on a lollipop graph with the image of the bachelor season and display it at the height of the number of hits on the last day of the season. As such, I need to assign an image to every single season. 130 | 131 | ```{r} 132 | 133 | x <-x %>% 134 | mutate(Image = case_when(season == "Nick Viall" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Nick.png", 135 | season == "Arie Luyendyk Jr" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Arie.png", 136 | season == "Colton Underwood" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Colton.png", 137 | season == "Rachel Lindsay" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Rachel.png", 138 | season == "Becca Kufrin" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Becca.png", 139 | season == "Hannah Brown" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Hannah.png", 140 | topic == "Bachelor in Paradise" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/BIP.png")) 141 | 142 | 143 | ``` 144 | 145 | 146 | # Create the mega chart 147 | 148 | Create a fun graph to display the relative ratings for each season by layering on a lollipop chart to represent the seasons and their relative search popularity for the last week of the season. Use the geom_segment() function to set the lollipop stem and the geom_image() function to set the lolipop circle with the image representing the season. 149 | 150 | ```{r, fig.width = 8, fig.height=8} 151 | p <- ggplot() + 152 | geom_line(data=trends, aes(x=date, y=hits, group=keyword, color = keyword), size=1) + 153 | scale_color_manual(values=c(yellow, blue, pink)) + 154 | geom_segment(data=x, aes(x=ratingEndDate, 155 | xend=ratingEndDate, 156 | y=0, 157 | yend=hits, 158 | color=topic), size=1) + 159 | geom_image(data=x, aes(x=ratingEndDate, y=hits, image=Image), size=0.105) + 160 | theme_classic() + 161 | labs(title = "The Bachelor Franchise Popularity ", 162 | subtitle = "Using data to find the most dramatic season ever!", 163 | caption = "Source: @littlemissdata", 164 | x = "Date", y = "Hits") + 165 | theme(legend.position="none", 166 | plot.title = element_text(size = 12, face = "bold"), 167 | plot.subtitle = element_text(size=10, face = "italic"), 168 | plot.caption = element_text(size = 8, face = "italic") ) 169 | 170 | p 171 | ``` 172 | 173 | 174 | -------------------------------------------------------------------------------- /gtrendsR/readme.md: -------------------------------------------------------------------------------- 1 | \#Introduction 2 | 3 | In this blog post, I will be analyzing the relative popularity of the 4 | bachelor franchise seasons as measured by their google search 5 | popularity. 6 | 7 | In this tutorial I will be using the [gtrendsR 8 | package](https://cran.r-project.org/web/packages/gtrendsR/gtrendsR.pdf) 9 | to gather Google trend information, dplyr to format the data, ggplot2 to 10 | create the graphs, gganimate to make an animation and geom\_image to 11 | create custom lollipop charts. 12 | 13 | \#Load all of the packages and install if necessary 14 | 15 | In my [latest blog post](https://www.littlemissdata.com/blog/iconmap), 16 | someone kindly suggested that I do an auto check to install all 17 | necessary packages before loading them. After a quick search, I found 18 | this code below to efficiently install and load packages in [Vikram 19 | Baliga’s 20 | Blog](https://www.vikram-baliga.com/blog/2015/7/19/a-hassle-free-way-to-verify-that-r-packages-are-installed-and-loaded) 21 | 22 | ``` r 23 | #specify the packages of interest 24 | packages = c("gtrendsR","tidyverse","gifski", "gganimate", "ggimage", "lubridate") 25 | 26 | #use this function to check if each package is on the local machine 27 | #if a package is installed, it will be loaded 28 | #if any are not, the missing package(s) will be installed and loaded 29 | package.check <- lapply(packages, FUN = function(x) { 30 | if (!require(x, character.only = TRUE)) { 31 | install.packages(x, dependencies = TRUE) 32 | library(x, character.only = TRUE) 33 | } 34 | }) 35 | ``` 36 | 37 | ## Loading required package: gtrendsR 38 | 39 | ## Loading required package: tidyverse 40 | 41 | ## ── Attaching packages ─────────────────────────────────────────────────────────────── tidyverse 1.2.1 ── 42 | 43 | ## ✔ ggplot2 3.2.0 ✔ purrr 0.3.2 44 | ## ✔ tibble 2.1.3 ✔ dplyr 0.8.3 45 | ## ✔ tidyr 0.8.3 ✔ stringr 1.4.0 46 | ## ✔ readr 1.3.1 ✔ forcats 0.4.0 47 | 48 | ## ── Conflicts ────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── 49 | ## ✖ dplyr::filter() masks stats::filter() 50 | ## ✖ dplyr::lag() masks stats::lag() 51 | 52 | ## Loading required package: gifski 53 | 54 | ## Loading required package: gganimate 55 | 56 | ## Loading required package: ggimage 57 | 58 | ## Loading required package: lubridate 59 | 60 | ## 61 | ## Attaching package: 'lubridate' 62 | 63 | ## The following object is masked from 'package:base': 64 | ## 65 | ## date 66 | 67 | ``` r 68 | #verify they are loaded 69 | search() 70 | ``` 71 | 72 | ## [1] ".GlobalEnv" "package:lubridate" "package:ggimage" 73 | ## [4] "package:gganimate" "package:gifski" "package:forcats" 74 | ## [7] "package:stringr" "package:dplyr" "package:purrr" 75 | ## [10] "package:readr" "package:tidyr" "package:tibble" 76 | ## [13] "package:ggplot2" "package:tidyverse" "package:gtrendsR" 77 | ## [16] "package:stats" "package:graphics" "package:grDevices" 78 | ## [19] "package:utils" "package:datasets" "package:methods" 79 | ## [22] "Autoloads" "package:base" 80 | 81 | Set the color variables 82 | ======================= 83 | 84 | To ensure consistent and effective color formatting, I am setting the 85 | color variables up front. 86 | 87 | ``` r 88 | pink <- "#FF8DC6" 89 | blue <- "#56C1FF" 90 | yellow <- "#FAE232" 91 | ``` 92 | 93 | Gather the ratings data for: The Bachelor, The Bachelorette and Bachelor in Paradise 94 | ==================================================================================== 95 | 96 | Using the gtrendsR package, load weekly US ratings for “Bachelor in 97 | Paradise”, “The Bachelor” and “The Bachelorette”. The hits are 98 | calculated with a relative max of 100 to show the relative max hits over 99 | the time range and search subjects. 100 | 101 | Plot the trends with the plot() function. 102 | 103 | ``` r 104 | bachTrends <- gtrends(c("Bachelor in Paradise", "The Bachelor", "The Bachelorette"), geo ="US") 105 | plot(bachTrends) 106 | ``` 107 | 108 | ![](readme_files/figure-markdown_github/unnamed-chunk-3-1.png) 109 | 110 | ``` r 111 | bachTrendsInterest <- bachTrends$interest_over_time 112 | ``` 113 | 114 | Transform the data 115 | ================== 116 | 117 | Filter to data that is 2017+ Convert hits to numeric as the default is 118 | character 119 | 120 | ``` r 121 | trends <- bachTrendsInterest %>% 122 | filter(year(date)>2016) %>% 123 | mutate(date = ymd(date), 124 | hits = as.numeric(hits)) 125 | ``` 126 | 127 | ## Warning: NAs introduced by coercion 128 | 129 | Create the same plot with ggplot2 130 | ================================= 131 | 132 | Create the basic plot of relative search popularity by search critera 133 | with the ggplot2 package. Transitioning to ggplot2 in order to use the 134 | ggplot2 features and complimentary packages like ggimage and gganimate. 135 | 136 | ``` r 137 | #Frequency plot by keyword 138 | p <- ggplot() + 139 | geom_line(data=trends, aes(x=date, y=hits, group=keyword, color = keyword)) + 140 | scale_color_manual(values=c( yellow, blue, pink)) + 141 | theme_classic() + 142 | theme(legend.position="bottom") + 143 | labs(title = "The Bachelor Franchise Popularity ", 144 | subtitle = "Using data to find the most dramatic season ever!", 145 | caption = "Source: @littlemissdata", 146 | x = "Date", y = "Hits") 147 | p 148 | ``` 149 | 150 | ![](readme_files/figure-markdown_github/unnamed-chunk-5-1.png) 151 | 152 | Create an animation 153 | =================== 154 | 155 | Take the basic plot and make an animation out of it with the gganimate() 156 | package. 157 | 158 | ``` r 159 | t <- p + 160 | transition_reveal(as.numeric(date)) 161 | gif <- animate(t, end_pause = 25, width = 800, height = 400, fps = 8) 162 | ``` 163 | 164 | ## Warning: Removed 2 rows containing missing values (geom_path). 165 | 166 | ## Warning: Removed 4 rows containing missing values (geom_path). 167 | 168 | ## Warning: Removed 6 rows containing missing values (geom_path). 169 | 170 | ## Warning: Removed 1 rows containing missing values (geom_path). 171 | 172 | ## Warning: Removed 2 rows containing missing values (geom_path). 173 | 174 | ## Warning: Removed 3 rows containing missing values (geom_path). 175 | 176 | ## Warning: Removed 5 rows containing missing values (geom_path). 177 | 178 | ``` r 179 | gif 180 | ``` 181 | 182 | ![](readme_files/figure-markdown_github/unnamed-chunk-6-1.gif) 183 | 184 | ``` r 185 | anim_save("Bachelor trends", gif) 186 | ``` 187 | 188 | Bring in meta data about bachelor franchise shows 189 | ================================================= 190 | 191 | We are going to bring in a data set which has the start dates for every 192 | single season of the Bachelor franchise. We will then do some data 193 | munging to find the closest ratings date to the season start date. With 194 | this info we will join the bachelor season metadata to the ratings 195 | table. 196 | 197 | ``` r 198 | ## Add lollipops 199 | x <-read.csv("https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/bachelorListing.csv", stringsAsFactors = FALSE) 200 | 201 | # Turn the dates into proper dates. 202 | #Ratings are only tracked on sundays so get the closest Sunday for ratings 203 | x <-x %>% 204 | mutate(startDate = ymd(as.Date(startDate, "%m/%d/%y")), 205 | endDate = ymd(as.Date(endDate, "%m/%d/%y")), 206 | ratingStartDate = floor_date(startDate, "weeks"), 207 | ratingEndDate = floor_date(endDate, "weeks")) 208 | x 209 | ``` 210 | 211 | ## season topic startDate endDate 212 | ## 1 Nick Viall The Bachelor 2017-01-02 2017-03-13 213 | ## 2 Arie Luyendyk Jr The Bachelor 2018-01-01 2018-03-06 214 | ## 3 Colton Underwood The Bachelor 2019-01-07 2019-03-12 215 | ## 4 Rachel Lindsay The Bachelorette 2017-05-22 2017-08-07 216 | ## 5 Becca Kufrin The Bachelorette 2018-05-28 2018-08-06 217 | ## 6 Hannah Brown The Bachelorette 2019-05-13 2019-07-30 218 | ## 7 Season 4 Bachelor in Paradise 2017-08-14 2017-09-11 219 | ## 8 Season 5 Bachelor in Paradise 2018-08-07 2018-09-11 220 | ## 9 Season 6 Bachelor in Paradise 2019-08-05 2019-09-17 221 | ## ratingStartDate ratingEndDate 222 | ## 1 2017-01-01 2017-03-12 223 | ## 2 2017-12-31 2018-03-04 224 | ## 3 2019-01-06 2019-03-10 225 | ## 4 2017-05-21 2017-08-06 226 | ## 5 2018-05-27 2018-08-05 227 | ## 6 2019-05-12 2019-07-28 228 | ## 7 2017-08-13 2017-09-10 229 | ## 8 2018-08-05 2018-09-09 230 | ## 9 2019-08-04 2019-09-15 231 | 232 | ``` r 233 | #Ratings are typically highest at the beginning 234 | x<-left_join(x, trends, by = c("topic"= "keyword", "ratingEndDate"="date")) 235 | ``` 236 | 237 | Get the images for each of the seasons 238 | ====================================== 239 | 240 | I have some plans to layer on a lollipop graph with the image of the 241 | bachelor season and display it at the height of the number of hits on 242 | the last day of the season. As such, I need to assign an image to every 243 | single season. 244 | 245 | ``` r 246 | x <-x %>% 247 | mutate(Image = case_when(season == "Nick Viall" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Nick.png", 248 | season == "Arie Luyendyk Jr" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Arie.png", 249 | season == "Colton Underwood" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Colton.png", 250 | season == "Rachel Lindsay" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Rachel.png", 251 | season == "Becca Kufrin" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Becca.png", 252 | season == "Hannah Brown" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/Hannah.png", 253 | topic == "Bachelor in Paradise" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/gtrendsR/images/BIP.png")) 254 | ``` 255 | 256 | Create the mega chart 257 | ===================== 258 | 259 | Create a fun graph to display the relative ratings for each season by 260 | layering on a lollipop chart to represent the seasons and their relative 261 | search popularity for the last week of the season. Use the 262 | geom\_segment() function to set the lollipop stem and the geom\_image() 263 | function to set the lolipop circle with the image representing the 264 | season. 265 | 266 | ``` r 267 | p <- ggplot() + 268 | geom_line(data=trends, aes(x=date, y=hits, group=keyword, color = keyword), size=1) + 269 | scale_color_manual(values=c(yellow, blue, pink)) + 270 | geom_segment(data=x, aes(x=ratingEndDate, 271 | xend=ratingEndDate, 272 | y=0, 273 | yend=hits, 274 | color=topic), size=1) + 275 | geom_image(data=x, aes(x=ratingEndDate, y=hits, image=Image), size=0.105) + 276 | theme_classic() + 277 | labs(title = "The Bachelor Franchise Popularity ", 278 | subtitle = "Using data to find the most dramatic season ever!", 279 | caption = "Source: @littlemissdata", 280 | x = "Date", y = "Hits") + 281 | theme(legend.position="none", 282 | plot.title = element_text(size = 12, face = "bold"), 283 | plot.subtitle = element_text(size=10, face = "italic"), 284 | plot.caption = element_text(size = 8, face = "italic") ) 285 | 286 | p 287 | ``` 288 | 289 | ![](readme_files/figure-markdown_github/unnamed-chunk-9-1.png) 290 | -------------------------------------------------------------------------------- /gtrendsR/readme_files/figure-markdown_github/unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/readme_files/figure-markdown_github/unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /gtrendsR/readme_files/figure-markdown_github/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/readme_files/figure-markdown_github/unnamed-chunk-5-1.png -------------------------------------------------------------------------------- /gtrendsR/readme_files/figure-markdown_github/unnamed-chunk-6-1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/readme_files/figure-markdown_github/unnamed-chunk-6-1.gif -------------------------------------------------------------------------------- /gtrendsR/readme_files/figure-markdown_github/unnamed-chunk-9-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/gtrendsR/readme_files/figure-markdown_github/unnamed-chunk-9-1.png -------------------------------------------------------------------------------- /iconmap/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/.DS_Store -------------------------------------------------------------------------------- /iconmap/BasketballCourts.csv: -------------------------------------------------------------------------------- 1 | GIS_ID,Name,Address,Type,Year Constructed,POINT_X,POINT_Y 2 | 5,West Lions Park Basketball Court #1 - Full,20 Granville St,Full, ,478397.4194,4759371.437 3 | 31,West Lions Park Basketball Court #2 - Half,20 Granville St,Half, ,478407.7135,4759382.983 4 | 8,Carling Park Basketball Court #2 - Full,675 Grosvenor St,Full, ,480797.2277,4761351.135 5 | 9,Carling Park Basketball Court #1 - Full,675 Grosvenor St,Full, ,480791.4695,4761367.421 6 | 7,Doidge Park Basketball Court #1 - Full,300 Cromwell St,Full, ,479422.6306,4760986.988 7 | 22,Southeast Optimist Park Basketball Court #1 - Full,237 Deveron Cres,Half, ,484074.2226,4755960.511 8 | 25,St. Stephen's Park Basketball Court #1 - Half,1030 Mitchell St,Half, ,481777.4668,4753673.485 9 | 24,Westminster Optimist Park Basketball Court #1 - Full,650 Osgoode Dr,Full, ,483116.0329,4754421.736 10 | 13,Huron Heights Park Basketball Court #1 - Full,185 McNay St,Full, ,481821.5678,4762198.038 11 | 16,Culver Park Basketball Court #1 - Full,1735 Culver Dr,Full, ,485117.3092,4761905.234 12 | 18,River East Optimist Park Basketball Court #1 - Full,55 Danielle Cres,Full, ,486305.7509,4758067.921 13 | 14,Flanders Park Basketball Court #1 - Half,145 Rhine Ave,Half, ,482683.2632,4761853.123 14 | 11,Boyle Park Basketball Court #1 - Full,1010 Princess Ave,Full, ,482179.2192,4760546.766 15 | 12,Boyle Park Basketball Court #2 - Half,1010 Princess Ave,Half, ,482199.8298,4760548.041 16 | 4,Chesham Heights Park Basketball Court #1 - Full,328 Castlegrove Blvd,Full, ,476500.7543,4760321.248 17 | 26,Earl Nichols Park and Arena Basketball Court #1 - Half,799 Homeview Rd,Half, ,479385.9413,4754759.272 18 | 30,Belvedere Place Park Basketball Court #1 - Full,457 Belvedere Pl,Full, ,472358.9119,4755476.632 19 | 6,Piccadilly Park Basketball Court #1 - Half,695 Waterloo St,Half, ,479777.9991,4760217.573 20 | 20,Naiomi Almeida Park Basketball Court #1 - Half,828 Deveron Cres,Half, ,484148.682,4757193.361 21 | 15,Huronview Park Basketball Court #1 - Half,1345 Bentley Dr,Half, ,483145.9148,4763640.525 22 | 3,Huntington Park Basketball Court #1 - Full,63 Meadowridge Rd,Full, ,474780.9971,4759055.366 23 | 17,Grampian Woods Basketball Court #1 - Half,265 Bournemouth Dr,Half, ,487983.6767,4759850.956 24 | 29,Cresthaven Park Basketball Court #1 - Half,104 Parks Edge Cres,Half, ,474763.4483,4755038.723 25 | 2,Ambleside Park Basketball Court #1 - Half,257 Ambleside Dr,Half, ,476078.0237,4762830.988 26 | 21,Carroll Park Basketball Court #1 - Full,270 Ellerslie Rd,Full, ,485487.6183,4756283.688 27 | 23,Heritage Park Basketball Court #1 - Half,150 Bexhill Dr,Half, ,483683.8376,4754380.436 28 | 19,St. Julien Park Basketball Court #1 - Full,15 Tommy Hunter Way,Full, ,483831.4329,4758113.06 29 | 10,Carling Heights Optimist Community Centre Basketball Court #1 - Full,656 Elizabeth St,Full, ,481297.0368,4760661.951 30 | 27,Highland Woods Basketball Court #1 - Half,324 Highview Ave E,Half, ,479228.3667,4755259.775 31 | 36,Richard B. Harrison Park Basketball Court #1 - Half,205 South St,Half, ,480233.2825,4757969.218 32 | 37,CNRA Park Basketball Court #1 - Full,325 Egerton St,Full, ,482315.8386,4759131.203 33 | 38,Heron Haven Park Basketball Court #1 - Half,204 Skyline Ave,Half, ,477143.6461,4765334.602 34 | 39,Ed Blake Park Basketball Court #1 - Full,449 Barker St,Full, ,481051.9687,4762705.334 35 | 42,Sugarcreek Park Basketball Court #1 - Full,460 Sugarcreek Trail,Full,2011,477014.9361,4758951.69 36 | 44,Kensington Park Basketball Court #1 - Full,16 Fernley Ave,Full,2013,478296.1202,4760068.357 37 | 45,Foxfield District Park Basketball Court #1 - Full,2384 Buroak Dr,Full,2013,473481.975,4763881.19 38 | 46,Blackacres Park Basketball Court #1,595 Blackacres Park,Full,2914,473490.6924,4761878.989 39 | 1,White Oaks Park Basketball Court #1 - Full,1394 Ernest Ave,Full, ,481008.8618,4753310.377 40 | 47,Capulet Park Basketball Court #1 - Full,40 Capulet Lane,Full, ,475667.5645,4759290.206 41 | 40,Beaverbrook Woods Basketball Court #1 - Half,780 Mapelridge St,Half, ,475215.4086,4759540.998 42 | 41,Riverbend Park Basketball Court #1,1585 Riverbend Rd,Full,2011,470279.2602,4757778.452 43 | 43,Constitution Park Basketball Court #1 - Full,725 Grenfell Dr,Full,2013,479822.3003,4765188.976 44 | 48,White Oaks Park Basketball Court #2 - Full,1394 Ernest Ave,Full, ,480988.8531,4753314.216 45 | 49,Powell Park Basketball Court #1 - Full,800 Superior Dr,Full, ,477976.0207,4766204.97 46 | 50,Meredith Park Basketball Court #1 - Full,450 Nelson St,Full, ,480978.6269,4758224.526 47 | 51,Forest Hill Park Basketball Court #1 - Half,1955 Rollingacres Dr,Half,2017,479383.0083,4766613.11 48 | 52,Glen Cairn Park - East Basketball Court #1 - Full,170 Helena Ave,Full,2017,482382.0163,4757084.854 -------------------------------------------------------------------------------- /iconmap/babyPoolFinal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/babyPoolFinal.png -------------------------------------------------------------------------------- /iconmap/duckie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/duckie.png -------------------------------------------------------------------------------- /iconmap/iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-2-1.png -------------------------------------------------------------------------------- /iconmap/iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /iconmap/iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-5-1.png -------------------------------------------------------------------------------- /iconmap/iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-6-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-6-1.png -------------------------------------------------------------------------------- /iconmap/iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-7-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-7-1.png -------------------------------------------------------------------------------- /iconmap/readme.md: -------------------------------------------------------------------------------- 1 | Introduction 2 | ------------ 3 | 4 | The full icon map tutorial is available [here](https://www.littlemissdata.com/blog/iconmap). 5 | 6 | Previously I have [written a tutorial on how to use ggmap with R.](https://www.littlemissdata.com/blog/maps) It shows the end to end of how to get started with using ggmaps, including signing up for the google service. 7 | 8 | This icon map tutorial is going to extend our ggmap work to create icon maps with ggmap. 9 | 10 | Set up packages 11 | --------------- 12 | 13 | Uncomment any "install.packages()" lines if you also need to install the package. 14 | 15 | Download the data 16 | ----------------- 17 | 18 | I was able to find some neat location information on the [data portal for my home town, London Ontario.](https://www.london.ca/city-hall/open-data/Pages/Open-Data-Data-Catalogue.aspx) 19 | 20 | In their catalogue, they have a variety of spreadsheets with location information of public facilities. I downloaded all public facilities of interest, combined and standardized them. 21 | 22 | ``` r 23 | #Download the data set 24 | df= read_csv('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/iconmap/MasterList.csv', col_names = TRUE) 25 | 26 | head(df) 27 | ``` 28 | 29 | ## # A tibble: 6 x 5 30 | ## Facility Name Address POINT_X POINT_Y 31 | ## 32 | ## 1 Basketball West Lions Park Basketball Cou… 20 Granvill… 478397.4… 4.76e6 33 | ## 2 Basketball West Lions Park Basketball Cou… 20 Granvill… 478407.7… 4.76e6 34 | ## 3 Basketball Carling Park Basketball Court … 675 Grosven… 480797.2… 4.76e6 35 | ## 4 Basketball Carling Park Basketball Court … 675 Grosven… 480791.4… 4.76e6 36 | ## 5 Basketball Doidge Park Basketball Court #… 300 Cromwel… 479422.6… 4.76e6 37 | ## 6 Basketball Southeast Optimist Park Basket… 237 Deveron… 484074.2… 4.76e6 38 | 39 | ``` r 40 | #Set your API Key 41 | #ggmap::register_google(key = "ENTER_YOUR_API_KEY") 42 | ``` 43 | 44 | Convert UTM to lat and long 45 | --------------------------- 46 | 47 | One thing that really confused me was trying to understand what the x and y coordinates were representing in the open data. After looking at their Q&A I realized that they are in Universal Transverse Mercator (UTM) format. 48 | 49 | I found a way to convert UTM to Lat and Long from [stack overflow](https://stackoverflow.com/questions/33456934/lat-long-conversion-to-utm-loop-r-code). I slightly modified the code to run through the whole data set efficiently. 50 | 51 | ``` r 52 | #Convert UTM to lat/lon 53 | 54 | proj4string <- "+proj=utm +zone=17 +north +ellps=WGS84 +datum=NAD83 +units=m +no_defs " 55 | nRow <- nrow(df) 56 | df$Lat <-0 57 | df$Lon <-0 58 | 59 | 60 | for(i in 1:nRow){ 61 | temp <-project(df[i,4:5], proj4string, inverse=TRUE) 62 | df[i,6] <- temp$y 63 | df[i,7] <-temp$x 64 | } 65 | 66 | head(df,60) 67 | ``` 68 | 69 | ## # A tibble: 60 x 7 70 | ## Facility Name Address POINT_X POINT_Y Lat Lon 71 | ## 72 | ## 1 Basketba… West Lions Park Baske… 20 Granvi… 478397.… 4.76e6 43.0 -81.3 73 | ## 2 Basketba… West Lions Park Baske… 20 Granvi… 478407.… 4.76e6 43.0 -81.3 74 | ## 3 Basketba… Carling Park Basketba… 675 Grosv… 480797.… 4.76e6 43.0 -81.2 75 | ## 4 Basketba… Carling Park Basketba… 675 Grosv… 480791.… 4.76e6 43.0 -81.2 76 | ## 5 Basketba… Doidge Park Basketbal… 300 Cromw… 479422.… 4.76e6 43.0 -81.3 77 | ## 6 Basketba… Southeast Optimist Pa… 237 Dever… 484074.… 4.76e6 43.0 -81.2 78 | ## 7 Basketba… St. Stephen's Park Ba… 1030 Mitc… 481777.… 4.75e6 42.9 -81.2 79 | ## 8 Basketba… Westminster Optimist … 650 Osgoo… 483116.… 4.75e6 42.9 -81.2 80 | ## 9 Basketba… Huron Heights Park Ba… 185 McNay… 481821.… 4.76e6 43.0 -81.2 81 | ## 10 Basketba… Culver Park Basketbal… 1735 Culv… 485117.… 4.76e6 43.0 -81.2 82 | ## # … with 50 more rows 83 | 84 | Create the starting map 85 | ----------------------- 86 | 87 | I start out with a basic map plotting all amenities, colored by facility type. For more information on the basics of ggmap, please see this [tutorial](https://www.littlemissdata.com/blog/maps). 88 | 89 | ``` r 90 | p <- ggmap(get_googlemap(center = c(lon =-81.23304, lat = 42.98339), 91 | zoom = 12, scale = 2, 92 | maptype ='roadmap', 93 | color = 'color')) 94 | p + geom_point(aes(x = Lon, y = Lat, colour = Facility), data = df, size = 0.5) + 95 | theme(legend.position="bottom") 96 | ``` 97 | 98 | ![](iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-2-1.png) 99 | 100 | Filter and plot an icon map 101 | --------------------------- 102 | 103 | Filter to only include water facilities (pool, wading pool, spray pad). 104 | 105 | Assign and display the icon for each facility type. 106 | 107 | ``` r 108 | df2 <-df %>% 109 | filter(Facility %in% c("Pool", "Wading Pool", "Spray Pad")) %>% 110 | mutate(Image = case_when(Facility == "Pool" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/iconmap/swimmer.png", 111 | Facility == "Wading Pool" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/iconmap/duckie.png", 112 | Facility == "Spray Pad" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/iconmap/splash.png")) 113 | 114 | p + geom_image(aes(x = Lon, y = Lat, image=Image), data = df2, size = 0.05) + 115 | theme(legend.position="bottom") 116 | ``` 117 | 118 | ![](iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-3-1.png) 119 | 120 | Make the images more aesthetically pleasing. 121 | -------------------------------------------- 122 | 123 | Make all images transparent, save and re-upload to GH 124 | 125 | Use the "Magick" package to make each image transparent with image\_transparent(). Make the resulting image a specific color with image\_colorize(). 126 | 127 | I then saved the images using the image\_write() function. I manually re-uploaded them to GH. 128 | 129 | ``` r 130 | baby <-image_transparent(image_read("https://raw.githubusercontent.com/lgellis/MiscTutorial/master/iconmap/duckie.png"), 'white') 131 | baby <- image_colorize(baby, 100, "#FF9933") 132 | image_write(baby, path = "babyPoolFinal.png", format = "png") 133 | 134 | swimmer <-image_transparent(image_read("https://raw.githubusercontent.com/lgellis/MiscTutorial/master/iconmap/swimmer.png"), 'white') 135 | swimmer <- image_colorize(swimmer, 100, "#000099") 136 | image_write(swimmer, path = "swimmerFinal.png", format = "png") 137 | 138 | splash <-image_transparent(image_read("https://raw.githubusercontent.com/lgellis/MiscTutorial/master/iconmap/splash.png"), 'white') 139 | splash <- image_colorize(splash, 100, "#3399FF") 140 | image_write(splash, path = "splashFinal.png", format = "png") 141 | ``` 142 | 143 | Create a better map using the newly formatted icons 144 | --------------------------------------------------- 145 | 146 | Create a new icon map using the new images as the icons. 147 | 148 | ``` r 149 | df2 <-df %>% 150 | filter(Facility %in% c("Pool", "Wading Pool", "Spray Pad")) %>% 151 | mutate(Image = case_when(Facility == "Pool" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/iconmap/swimmerFinal.png", 152 | Facility == "Wading Pool" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/iconmap/babyPoolFinal.png", 153 | Facility == "Spray Pad" ~ "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/iconmap/splashFinal.png")) 154 | 155 | p + geom_image(aes(x = Lon, y = Lat, image=Image), data = df2, size = 0.06) + 156 | theme(legend.position="bottom") 157 | ``` 158 | 159 | ![](iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-5-1.png) 160 | 161 | Create a different version using 'stamen' and 'terrain-lines' 162 | ------------------------------------------------------------- 163 | 164 | Now that we’ve sorted out our icons and have them plotted on the map, let’s try swapping out the base map. We are going to try the 'stamen' 'terrain-lines' version. I think that this a really cool way of looking at it as it represents the subdivisions more clearly. 165 | 166 | For more information on swapping out base plots, please see my previous [tutorial](https://www.littlemissdata.com/blog/maps). 167 | 168 | ``` r 169 | center = c(lon =-81.23304, lat = 42.98339) 170 | qmap(center, zoom = 12, source = "stamen", maptype = "terrain-lines") + 171 | geom_image(aes(x = Lon, y = Lat, image=Image), data = df2, size = 0.06) + 172 | theme(legend.position="bottom") 173 | ``` 174 | 175 | ![](iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-6-1.png) 176 | 177 | Create a better version using 'stamen' and 'terrain' 178 | ---------------------------------------------------- 179 | 180 | Now swap out 'terrain-lines' mapping parameter for 'terrain'. I think this is my favorite version as it nicely balances showing green space and street lines very clearly at a distance. 181 | 182 | ``` r 183 | center = c(lon =-81.23304, lat = 42.98339) 184 | qmap(center, zoom = 12, source = "stamen", maptype = "terrain") + 185 | geom_image(aes(x = Lon, y = Lat, image=Image), data = df2, size = 0.06) + 186 | theme(legend.position="bottom") 187 | ``` 188 | 189 | ![](iconMap_NoKey_files/figure-markdown_github/unnamed-chunk-7-1.png) 190 | 191 | ``` r 192 | sessionInfo() 193 | ``` 194 | 195 | ## R version 3.5.2 (2018-12-20) 196 | ## Platform: x86_64-apple-darwin15.6.0 (64-bit) 197 | ## Running under: macOS Mojave 10.14.6 198 | ## 199 | ## Matrix products: default 200 | ## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib 201 | ## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib 202 | ## 203 | ## locale: 204 | ## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 205 | ## 206 | ## attached base packages: 207 | ## [1] stats graphics grDevices utils datasets methods base 208 | ## 209 | ## other attached packages: 210 | ## [1] ggimage_0.2.2 ggmap_3.0.0 magick_2.1 proj4_1.0-8 211 | ## [5] forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3 purrr_0.3.2 212 | ## [9] readr_1.3.1 tidyr_0.8.3 tibble_2.1.3 ggplot2_3.2.0 213 | ## [13] tidyverse_1.2.1 214 | ## 215 | ## loaded via a namespace (and not attached): 216 | ## [1] tidyselect_0.2.5 xfun_0.8 haven_2.1.0 217 | ## [4] lattice_0.20-38 vctrs_0.1.0 colorspace_1.4-1 218 | ## [7] generics_0.0.2 htmltools_0.3.6 yaml_2.2.0 219 | ## [10] utf8_1.1.4 gridGraphics_0.4-1 rlang_0.4.0 220 | ## [13] pillar_1.4.1 glue_1.3.1 withr_2.1.2 221 | ## [16] modelr_0.1.4 readxl_1.3.1 rvcheck_0.1.3 222 | ## [19] plyr_1.8.4 jpeg_0.1-8 munsell_0.5.0 223 | ## [22] gtable_0.3.0 cellranger_1.1.0 rvest_0.3.4 224 | ## [25] RgoogleMaps_1.4.3 evaluate_0.14 labeling_0.3 225 | ## [28] knitr_1.23 curl_3.3 fansi_0.4.0 226 | ## [31] broom_0.5.2 Rcpp_1.0.2 scales_1.0.0 227 | ## [34] backports_1.1.4 jsonlite_1.6 rjson_0.2.20 228 | ## [37] hms_0.4.2 png_0.1-7 digest_0.6.20 229 | ## [40] stringi_1.4.3 grid_3.5.2 cli_1.1.0 230 | ## [43] tools_3.5.2 bitops_1.0-6 magrittr_1.5 231 | ## [46] lazyeval_0.2.2 zeallot_0.1.0 crayon_1.3.4 232 | ## [49] pkgconfig_2.0.2 ggplotify_0.0.4 xml2_1.2.0 233 | ## [52] lubridate_1.7.4 assertthat_0.2.1 rmarkdown_1.13 234 | ## [55] httr_1.4.0 rstudioapi_0.10 R6_2.4.0 235 | ## [58] nlme_3.1-140 compiler_3.5.2 236 | -------------------------------------------------------------------------------- /iconmap/splash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/splash.png -------------------------------------------------------------------------------- /iconmap/splashFinal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/splashFinal.png -------------------------------------------------------------------------------- /iconmap/swimmer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/swimmer.png -------------------------------------------------------------------------------- /iconmap/swimmerFinal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/iconmap/swimmerFinal.png -------------------------------------------------------------------------------- /inspectdf/readme.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Easy EDA in R using the inspectdf package" 3 | author: Laura Ellis 4 | date: Sept 11 2021 5 | output: 6 | md_document: 7 | variant: markdown_github 8 | --- 9 | 10 | # Install and load the inspectdf package 11 | 12 | I like this package because it’s got a lot of functionality and it’s incredibly straightforward. In short it allows you to understand and visualize column types, sizes, values, value imbalance & distributions as well as correlations. Further to this, it allows you to very easily perform any of the above features for an individual data frame, or to compare the differences between two data frames. 13 | 14 | ```{r setup, include=FALSE} 15 | 16 | #First install devtools to allow you to install inspectdf from github 17 | #install.packages("devtools") 18 | library(devtools) 19 | 20 | # install and load the package - https://github.com/alastairrushworth/inspectdf 21 | 22 | #devtools::install_github("alastairrushworth/inspectdf") 23 | library(inspectdf) 24 | 25 | #install.packages("tidyverse") 26 | library(tidyverse) 27 | ``` 28 | 29 | # Download the data Set from the previous [EDA blog](https://www.littlemissdata.com/blog/simple-eda) 30 | 31 | ```{r} 32 | #Load the readr library to bring in the dataset 33 | #install.packages("readr") 34 | library(readr) 35 | 36 | #Download the data set 37 | df= read_csv('https://raw.githubusercontent.com/lgellis/STEM/master/DATA-ART-1/Data/FinalData.csv', col_names = TRUE) 38 | 39 | #Quick view of the data frame 40 | head(df, 10) 41 | dim(df) 42 | ``` 43 | 44 | # Create the necessary data frames 45 | 46 | We need three data frames. We need one data frame with the complete data set. We simply rename df to allGrades. We also need two subsetted data sets to leverage the packages easy data frame comparison features. We create the data frames oldGrades (6-8) and youngGrades (3-5). 47 | ```{r} 48 | allGrades <- df 49 | 50 | oldGrades <- allGrades %>% 51 | filter(Grade > 5) 52 | 53 | youngGrades <- allGrades %>% 54 | filter(Grade < 6) 55 | 56 | ggplot(oldGrades, aes(x=Grade)) + geom_histogram() 57 | ggplot(youngGrades, aes(x=Grade)) + geom_histogram() 58 | 59 | ``` 60 | 61 | # Run through the functions of the inspectdf package 62 | 63 | Simply pass in a dataframe, or two (for comparisons) and set show_plot = TRUE. The output will include both a tibble with the raw data and a visualization. 64 | 65 | ## 1 Evaluate the data frame column types with the inspect_types() function 66 | 67 | ### a) Evaluate the full data frame: allGrades 68 | 69 | ```{r} 70 | inspect_types(allGrades) %>% show_plot() 71 | ``` 72 | 73 | ### b) Compare between youngGrades and oldGrades 74 | 75 | ```{r} 76 | inspect_types(youngGrades, oldGrades) %>% show_plot() 77 | ``` 78 | 79 | ## 2 Evaluate the data frame column memory usage with the inspect_mem() function 80 | 81 | ### a) Evaluate the full data frame: allGrades 82 | 83 | ```{r} 84 | inspect_mem(allGrades) %>% show_plot() 85 | ``` 86 | 87 | 88 | ### b) Compare between youngGrades and oldGrades 89 | 90 | ```{r} 91 | inspect_mem(youngGrades, oldGrades) %>% show_plot() 92 | ``` 93 | 94 | ## 3 Evaluate the na prevelance within the data frame with the inspect_na() function 95 | 96 | ### a) Evaluate the full data frame: allGrades 97 | 98 | ```{r} 99 | inspect_na(allGrades) %>% show_plot() 100 | ``` 101 | 102 | ### b) Compare between youngGrades and oldGrades 103 | 104 | ```{r} 105 | inspect_na(youngGrades, oldGrades) %>% show_plot() 106 | ``` 107 | 108 | 109 | ## 4 Evaluate the distribution of the numerical columns with the inspect_num() function 110 | 111 | ### a) Evaluate the full data frame: allGrades 112 | 113 | ```{r} 114 | inspect_num(allGrades) %>% show_plot() 115 | ``` 116 | 117 | ### b) Compare between youngGrades and oldGrades 118 | 119 | ```{r} 120 | inspect_num(youngGrades, oldGrades) %>% show_plot() 121 | ``` 122 | 123 | ## 5 Evaluate for any possible categorical column imbalance with inspect_imb() function to identify factors which might be overly prevalent. 124 | 125 | ### a) Evaluate the full data frame: allGrades 126 | 127 | ```{r} 128 | inspect_imb(allGrades) %>% show_plot() 129 | ``` 130 | 131 | ### b) Compare between youngGrades and oldGrades 132 | 133 | ```{r} 134 | inspect_imb(youngGrades, oldGrades) %>% show_plot() 135 | ``` 136 | 137 | ## 6 Evaluate the categorical column distribution with the inspect_cat() function 138 | 139 | ### a) Evaluate the full data frame: allGrades 140 | 141 | ```{r} 142 | inspect_cat(allGrades) %>% show_plot() 143 | ``` 144 | 145 | ### b) Compare between youngGrades and oldGrades 146 | 147 | ```{r} 148 | inspect_cat(youngGrades, oldGrades) %>% show_plot() 149 | ``` 150 | 151 | ## 7 Evaluate the column correlations with the inspect_cor() function 152 | 153 | ### a) Evaluate the full data frame: allGrades 154 | 155 | ```{r} 156 | inspect_cor(allGrades) %>% show_plot() 157 | ``` 158 | 159 | ### b) Compare between youngGrades and oldGrades 160 | 161 | ```{r} 162 | inspect_cor(youngGrades, oldGrades) %>% show_plot() 163 | ``` 164 | 165 | 166 | -------------------------------------------------------------------------------- /inspectdf/readme.md: -------------------------------------------------------------------------------- 1 | # Install and load the inspectdf package 2 | 3 | I like this package because it’s got a lot of functionality and it’s 4 | incredibly straightforward. In short it allows you to understand and 5 | visualize column types, sizes, values, value imbalance & distributions 6 | as well as correlations. Further to this, it allows you to very easily 7 | perform any of the above features for an individual data frame, or to 8 | compare the differences between two data frames. 9 | 10 | # Download the data Set from the previous [EDA blog](https://www.littlemissdata.com/blog/simple-eda) 11 | 12 | ``` r 13 | #Load the readr library to bring in the dataset 14 | #install.packages("readr") 15 | library(readr) 16 | 17 | #Download the data set 18 | df= read_csv('https://raw.githubusercontent.com/lgellis/STEM/master/DATA-ART-1/Data/FinalData.csv', col_names = TRUE) 19 | ``` 20 | 21 | ## Rows: 185 Columns: 17 22 | 23 | ## ── Column specification ──────────────────────────────────────────────────────── 24 | ## Delimiter: "," 25 | ## chr (11): Gender, Horoscope, Subject, IntExt, OptPest, SpendTime1, SpendTime... 26 | ## dbl (6): ID, Grade, ScreenTime, Sleep, PhysActive, HrsHomework 27 | 28 | ## 29 | ## ℹ Use `spec()` to retrieve the full column specification for this data. 30 | ## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message. 31 | 32 | ``` r 33 | #Quick view of the data frame 34 | head(df, 10) 35 | ``` 36 | 37 | ## # A tibble: 10 × 17 38 | ## ID Gender Grade Horoscope Subject IntExt OptPest ScreenTime Sleep 39 | ## 40 | ## 1 1 male 4 Scorpio Math Extravert Optimist 1 7 41 | ## 2 2 female 4 Capricorn Gym Extravert Optimist 1 8 42 | ## 3 3 male 4 Taurus Math Introvert Optimist 4 9 43 | ## 4 4 male 4 Aquarius Math Don't Know Don't Know 3 9 44 | ## 5 5 male 4 Scorpio Gym Don't Know Don't Know 1 9 45 | ## 6 6 male 4 Pisces Gym Extravert Optimist 2 9 46 | ## 7 7 male 3 Scorpio Art Introvert Optimist 1 11 47 | ## 8 8 male 6 Taurus Math Extravert Optimist 4 9 48 | ## 9 9 male 6 Aries Gym Introvert Pessimist 6 8 49 | ## 10 10 male 6 Pisces Math Introvert Don't Know 3 9 50 | ## # … with 8 more variables: PhysActive , HrsHomework , 51 | ## # SpendTime1 , SpendTime2 , Self1 , Self2 , Career , 52 | ## # Superpower 53 | 54 | ``` r 55 | dim(df) 56 | ``` 57 | 58 | ## [1] 185 17 59 | 60 | # Create the necessary data frames 61 | 62 | We need three data frames. We need one data frame with the complete data 63 | set. We simply rename df to allGrades. We also need two subsetted data 64 | sets to leverage the packages easy data frame comparison features. We 65 | create the data frames oldGrades (6-8) and youngGrades (3-5). 66 | 67 | ``` r 68 | allGrades <- df 69 | 70 | oldGrades <- allGrades %>% 71 | filter(Grade > 5) 72 | 73 | youngGrades <- allGrades %>% 74 | filter(Grade < 6) 75 | 76 | ggplot(oldGrades, aes(x=Grade)) + geom_histogram() 77 | ``` 78 | 79 | ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. 80 | 81 | ![](readme_files/figure-markdown_github/unnamed-chunk-2-1.png) 82 | 83 | ``` r 84 | ggplot(youngGrades, aes(x=Grade)) + geom_histogram() 85 | ``` 86 | 87 | ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. 88 | 89 | ![](readme_files/figure-markdown_github/unnamed-chunk-2-2.png) 90 | 91 | # Run through the functions of the inspectdf package 92 | 93 | Simply pass in a dataframe, or two (for comparisons) and set show_plot = 94 | TRUE. The output will include both a tibble with the raw data and a 95 | visualization. 96 | 97 | ## 1 Evaluate the data frame column types with the inspect_types() function 98 | 99 | ### a) Evaluate the full data frame: allGrades 100 | 101 | ``` r 102 | inspect_types(allGrades) %>% show_plot() 103 | ``` 104 | 105 | ![](readme_files/figure-markdown_github/unnamed-chunk-3-1.png) 106 | 107 | ### b) Compare between youngGrades and oldGrades 108 | 109 | ``` r 110 | inspect_types(youngGrades, oldGrades) %>% show_plot() 111 | ``` 112 | 113 | ![](readme_files/figure-markdown_github/unnamed-chunk-4-1.png) 114 | 115 | ## 2 Evaluate the data frame column memory usage with the inspect_mem() function 116 | 117 | ### a) Evaluate the full data frame: allGrades 118 | 119 | ``` r 120 | inspect_mem(allGrades) %>% show_plot() 121 | ``` 122 | 123 | ## Warning: `guides( = FALSE)` is deprecated. Please use `guides( = 124 | ## "none")` instead. 125 | 126 | ![](readme_files/figure-markdown_github/unnamed-chunk-5-1.png) 127 | 128 | ### b) Compare between youngGrades and oldGrades 129 | 130 | ``` r 131 | inspect_mem(youngGrades, oldGrades) %>% show_plot() 132 | ``` 133 | 134 | ![](readme_files/figure-markdown_github/unnamed-chunk-6-1.png) 135 | 136 | ## 3 Evaluate the na prevelance within the data frame with the inspect_na() function 137 | 138 | ### a) Evaluate the full data frame: allGrades 139 | 140 | ``` r 141 | inspect_na(allGrades) %>% show_plot() 142 | ``` 143 | 144 | ## Warning: `guides( = FALSE)` is deprecated. Please use `guides( = 145 | ## "none")` instead. 146 | 147 | ![](readme_files/figure-markdown_github/unnamed-chunk-7-1.png) 148 | 149 | ### b) Compare between youngGrades and oldGrades 150 | 151 | ``` r 152 | inspect_na(youngGrades, oldGrades) %>% show_plot() 153 | ``` 154 | 155 | ![](readme_files/figure-markdown_github/unnamed-chunk-8-1.png) 156 | 157 | ## 4 Evaluate the distribution of the numerical columns with the inspect_num() function 158 | 159 | ### a) Evaluate the full data frame: allGrades 160 | 161 | ``` r 162 | inspect_num(allGrades) %>% show_plot() 163 | ``` 164 | 165 | ![](readme_files/figure-markdown_github/unnamed-chunk-9-1.png) 166 | 167 | ### b) Compare between youngGrades and oldGrades 168 | 169 | ``` r 170 | inspect_num(youngGrades, oldGrades) %>% show_plot() 171 | ``` 172 | 173 | ![](readme_files/figure-markdown_github/unnamed-chunk-10-1.png) 174 | 175 | ## 5 Evaluate for any possible categorical column imbalance with inspect_imb() function to identify factors which might be overly prevalent. 176 | 177 | ### a) Evaluate the full data frame: allGrades 178 | 179 | ``` r 180 | inspect_imb(allGrades) %>% show_plot() 181 | ``` 182 | 183 | ## Warning: `guides( = FALSE)` is deprecated. Please use `guides( = 184 | ## "none")` instead. 185 | 186 | ![](readme_files/figure-markdown_github/unnamed-chunk-11-1.png) 187 | 188 | ### b) Compare between youngGrades and oldGrades 189 | 190 | ``` r 191 | inspect_imb(youngGrades, oldGrades) %>% show_plot() 192 | ``` 193 | 194 | ![](readme_files/figure-markdown_github/unnamed-chunk-12-1.png) 195 | 196 | ## 6 Evaluate the categorical column distribution with the inspect_cat() function 197 | 198 | ### a) Evaluate the full data frame: allGrades 199 | 200 | ``` r 201 | inspect_cat(allGrades) %>% show_plot() 202 | ``` 203 | 204 | ![](readme_files/figure-markdown_github/unnamed-chunk-13-1.png) 205 | 206 | ### b) Compare between youngGrades and oldGrades 207 | 208 | ``` r 209 | inspect_cat(youngGrades, oldGrades) %>% show_plot() 210 | ``` 211 | 212 | ![](readme_files/figure-markdown_github/unnamed-chunk-14-1.png) 213 | 214 | ## 7 Evaluate the column correlations with the inspect_cor() function 215 | 216 | ### a) Evaluate the full data frame: allGrades 217 | 218 | ``` r 219 | inspect_cor(allGrades) %>% show_plot() 220 | ``` 221 | 222 | ![](readme_files/figure-markdown_github/unnamed-chunk-15-1.png) 223 | 224 | ### b) Compare between youngGrades and oldGrades 225 | 226 | ``` r 227 | inspect_cor(youngGrades, oldGrades) %>% show_plot() 228 | ``` 229 | 230 | ![](readme_files/figure-markdown_github/unnamed-chunk-16-1.png) 231 | -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-10-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-10-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-11-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-11-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-12-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-12-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-13-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-13-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-14-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-14-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-15-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-15-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-16-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-16-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-2-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-2-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-2-2.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-4-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-4-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-5-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-6-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-6-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-7-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-7-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-8-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-8-1.png -------------------------------------------------------------------------------- /inspectdf/readme_files/figure-markdown_github/unnamed-chunk-9-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/inspectdf/readme_files/figure-markdown_github/unnamed-chunk-9-1.png -------------------------------------------------------------------------------- /lacroix/lacroix_animation.R: -------------------------------------------------------------------------------- 1 | ######## Extra material and guides ######## 2 | 3 | # https://cran.r-project.org/web/packages/magick/vignettes/intro.html 4 | # https://www.danielphadley.com/ggplot-logo/ 5 | 6 | ######## Set Up Work ######## 7 | 8 | #Install necessary packages 9 | install.packages("ggplot2") 10 | install.packages("magrittr") 11 | install.packages("magick") 12 | install.packages("devtools") 13 | install.packages("curl") 14 | install.packages("data.table") 15 | install.packages("lubridate") 16 | install.packages("devtools") 17 | library(devtools) 18 | install_github("johannesbjork/LaCroixColoR") 19 | 20 | #Load necessary packages 21 | library(LaCroixColoR) 22 | library(ggplot2) 23 | library(magick) 24 | library(magrittr) 25 | library(curl) 26 | library(data.table) 27 | library(lubridate) 28 | 29 | #Bring in the nasdaq data from Nasdaq.com for the LaCroix parent company: National Beverage Corp 30 | #https://www.nasdaq.com/symbol/fizz/historical 31 | 32 | df= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/lacroix/FIZZ_Stock.csv') 33 | attach(df) 34 | df$mdy <-mdy(df$date) 35 | df$month <- month(df$mdy, label = TRUE) 36 | df$year <- as.factor(year(df$mdy)) 37 | df$wday <- wday(df$mdy, label = TRUE) 38 | head(df) 39 | attach(df) 40 | 41 | 42 | ######## Image One - The LaCroix Curve ######## 43 | 44 | # Create base plot 45 | fizz <-ggplot(data=df, aes(x=mdy, y=close, color=year)) + 46 | geom_point() + 47 | ggtitle("FIZZ Stock (LaCroix Parent Company)") + 48 | scale_color_manual(values=lacroix_palette("PeachPear",type = "continuous", n=11)) + 49 | labs(x = "Date", y="Closing Stock Price") 50 | 51 | ggsave(fizz, file="fizz.png", width = 5, height = 4, dpi = 100) 52 | plot <- image_read("fizz.png") 53 | plot 54 | 55 | #Bring in the gif - scale and rotate 56 | laCroixAnimation <- image_read("https://media.giphy.com/media/h7ZuxGCxXTRMQ/giphy.gif") %>% 57 | image_scale("150") %>% 58 | image_rotate(-30) 59 | laCroixAnimation 60 | 61 | # Combine the plot and animation 62 | # Set the Background image 63 | background <- image_background(image_scale(plot, "500"), "white", flatten = TRUE) 64 | # Combine and flatten frames 65 | frames <- image_composite(background, laCroixAnimation, offset = "+150+120") 66 | # Turn frames into animation 67 | animation <- image_animate(frames, fps = 5) 68 | print(animation) 69 | 70 | #Save gif 71 | image_write(animation, "laCroixImage1.gif") 72 | 73 | 74 | ######## Image Two - Climbing the Mountain! ######## 75 | 76 | #Use base plot previously created 77 | #Bring in the gif - scale and rotate 78 | laCroixAnimation <- image_read("https://media.giphy.com/media/l378xFKDBZO9Y5VUk/giphy.gif") %>% 79 | image_scale("300") %>% 80 | image_rotate(10) 81 | laCroixAnimation 82 | # Combine the plot and animation 83 | # Background image 84 | background <- image_background(image_scale(plot, "500"), "white", flatten = TRUE) 85 | # Combine and flatten frames 86 | frames <- image_composite(background, laCroixAnimation, offset = "+100+50") 87 | # Turn frames into animation 88 | animation <- image_animate(frames, fps = 5) 89 | print(animation) 90 | 91 | #Save gif 92 | image_write(animation, "laCroixImage2.gif") 93 | 94 | ######## Image Three - Sad Decline ######## 95 | 96 | #Use base plot previously created 97 | #Bring in the gif - scale 98 | laCroixAnimation <- image_read("https://media.giphy.com/media/xUA7bfJ4OF11xXe4Fy/giphy.gif") %>% 99 | image_scale("80") 100 | laCroixAnimation 101 | 102 | # Background image 103 | background <- image_background(image_scale(plot, "500"), "white", flatten = TRUE) 104 | # Combine and flatten frames 105 | frames <- image_composite(background, laCroixAnimation, offset = "+360+150") 106 | # Turn frames into animation 107 | animation <- image_animate(frames, fps = 10) 108 | print(animation) 109 | 110 | #Save gif 111 | image_write(animation, "laCroixImage3.gif") 112 | 113 | ######## Other GIFs to Consider ######## 114 | 115 | # -girl - https://media.giphy.com/media/3o6vXX2ppxY2MLFuTe/giphy.gif 116 | # can - https://media.giphy.com/media/26vIfDdMIMR46bvQA/giphy.gif 117 | #fun guy - https://media.giphy.com/media/h7ZuxGCxXTRMQ/giphy.gif 118 | 119 | ######## Other Fun Packages to Consider ######## 120 | 121 | # Create Memes - https://cran.r-project.org/web/packages/meme/vignettes/meme.html 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /lacroix/readme.md: -------------------------------------------------------------------------------- 1 | A Lacroix themed tutorial using LaCroixColoR, Magick Animations + More! 2 | 3 | Tutorial available hereFor more information on this tutorial, please see [the blog post on littlemissdata.com](https://www.littlemissdata.com/blog/lacroix) 4 | -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-1-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-1-1.png -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-1-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-1-2.png -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-10-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-10-1.png -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-12-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-12-1.png -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-2-1.png -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-5-1.png -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-6-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-6-1.png -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-7-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-7-1.png -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-8-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-8-1.png -------------------------------------------------------------------------------- /sets/readme_files/figure-markdown_github/unnamed-chunk-9-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/sets/readme_files/figure-markdown_github/unnamed-chunk-9-1.png -------------------------------------------------------------------------------- /simple_eda/dataExplorerGifLg.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/simple_eda/dataExplorerGifLg.gif -------------------------------------------------------------------------------- /simple_eda/exploreData.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "EDA Notebook" 3 | output: 4 | word_document: default 5 | html_notebook: default 6 | --- 7 | 8 | This is a notebook used to explore data. 9 | 10 | #Use Data Explorer to Get a Greater Summary View 11 | 12 | ```{r} 13 | #Bring in the data set 14 | 15 | library(readr) 16 | 17 | df= read_csv('https://raw.githubusercontent.com/lgellis/STEM/master/DATA-ART-1/Data/FinalData.csv', col_names = TRUE) 18 | 19 | ``` 20 | 21 | 22 | #Use the head command to get a preview of the data set 23 | 24 | The default is 6 rows, but let's select 10 just to get a bigger picture 25 | 26 | ```{r} 27 | head(df, 10) 28 | ``` 29 | 30 | ```{r} 31 | dim(df) 32 | 33 | #Displays the type and a preview of all columns as a row so that it's very easy to take in. 34 | 35 | library(dplyr) 36 | glimpse(df) 37 | 38 | 39 | ``` 40 | 41 | 42 | #Summary Statistics 43 | 44 | This is a good method because it shows the summary statistics for numerical values 45 | 46 | ```{r} 47 | summary(df) 48 | ``` 49 | 50 | 51 | #Use Skimr to Display Summary Statistics for the variables 52 | 53 | This has same as above + missing and a histogram. Also, it has some additional statistics of non-numerical values. 54 | ```{r} 55 | library(skimr) 56 | skim(df) 57 | ``` 58 | 59 | 60 | # Get a Full Data Summary Report 61 | 62 | Basic statistics, missing data, distribution, correlation, PCA. 63 | 64 | ```{r} 65 | 66 | library(DataExplorer) 67 | DataExplorer::create_report(df) 68 | 69 | ``` 70 | 71 | 72 | 73 | ```{r} 74 | install.packages("devtools") 75 | devtools::install_github("ropensci/visdat") 76 | library(visdat) 77 | ``` 78 | 79 | ```{r} 80 | vis_miss(df) 81 | vis_dat(df) 82 | 83 | ``` 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /simple_eda/readme.md: -------------------------------------------------------------------------------- 1 | 2 | Please view the tutorial at: https://www.littlemissdata.com/blog/simple-eda 3 | 4 | ![Preview](https://github.com/lgellis/MiscTutorial/blob/master/simple_eda/dataExplorerGifLg.gif) 5 | -------------------------------------------------------------------------------- /twitter_wordcloud/R_word_clouds.R: -------------------------------------------------------------------------------- 1 | #twitter - https://cran.r-project.org/web/packages/rtweet/vignettes/intro.html 2 | 3 | 4 | ### Twitter Tips 5 | # Follow steps here to set up twitter account - https://github.com/mkearney/rtweet 6 | # twitter app -#token - https://apps.twitter.com/app/new 7 | # - make sure to uncheck "Enable Callback Locking (It is recommended to enable callback locking to ensure apps cannot overwrite the callback url)" 8 | 9 | ### Other Documentation 10 | 11 | #Tidy text - https://www.tidytextmining.com/tidytext.html 12 | #Wordcloud2 - https://cran.r-project.org/web/packages/wordcloud2/vignettes/wordcloud.html 13 | #How to download a file from a URL - http://rfunction.com/archives/2222 14 | 15 | install.packages("rtweet") 16 | install.packages("tidytext") 17 | install.packages("dplyr") 18 | install.packages("stringr") 19 | require(devtools) 20 | install_github("lchiffon/wordcloud2") 21 | 22 | library(tidytext) 23 | library(dplyr) 24 | library(stringr) 25 | library(rtweet) 26 | library(wordcloud2) 27 | 28 | #Create twitter token 29 | #replace with your values 30 | create_token( 31 | app = "PLACE YOUR APP NAME HERE", 32 | consumer_key = "PLACE YOUR CONSUMER KEY HERE", 33 | consumer_secret = "PLACE YOUR CONSUMER SECRET HERE") 34 | 35 | ############# Handmaiden's Tale Tweets ############# 36 | 37 | #Grab tweets - note: reduce to 1000 if it's slow 38 | ?search_tweets 39 | hmt <- search_tweets( 40 | "#HandmaidsTale", n = 2000, include_rts = FALSE 41 | ) 42 | 43 | #Look at tweets 44 | head(hmt) 45 | dim(hmt) 46 | hmt$text 47 | 48 | #Unnest the words - code via Tidy Text 49 | 50 | hmtTable <- hmt %>% 51 | unnest_tokens(word, text) 52 | 53 | #remove stop words - aka typically very common words such as "the", "of" etc 54 | 55 | data(stop_words) 56 | 57 | hmtTable <- hmtTable %>% 58 | anti_join(stop_words) 59 | 60 | #do a word count 61 | 62 | hmtTable <- hmtTable %>% 63 | count(word, sort = TRUE) 64 | hmtTable 65 | 66 | #Remove other nonsense words 67 | hmtTable <-hmtTable %>% 68 | filter(!word %in% c('t.co', 'https', 'handmaidstale', "handmaid's", 'season', 'episode', 'de', 'handmaidsonhulu', 'tvtime', 69 | 'watched', 'watching', 'watch', 'la', "it's", 'el', 'en', 'tv', 70 | 'je', 'ep', 'week', 'amp')) 71 | 72 | 73 | #wordcloud2 74 | wordcloud2(hmtTable, size=0.7) 75 | 76 | #Visualize 77 | #Create Palette 78 | redPalette <- c("#5c1010", "#6f0000", "#560d0d", "#c30101", "#940000") 79 | redPalette 80 | 81 | 82 | #Download images for plotting 83 | url = "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/twitter_wordcloud/handmaiden.jpeg" 84 | handmaiden <- "handmaiden.jpg" 85 | download.file(url, handmaiden) # download file 86 | url = "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/twitter_wordcloud/resistance.jpeg" 87 | resistance <- "resistance.jpeg" 88 | download.file(url, resistance) # download file 89 | 90 | 91 | #plots 92 | wordcloud2(hmtTable, size=1.6, figPath = handmaiden, color=rep_len( redPalette, nrow(hmtTable) ) ) 93 | wordcloud2(hmtTable, size=1.6, figPath = resistance, color=rep_len( redPalette, nrow(hmtTable) ) ) 94 | wordcloud2(hmtTable, size=1.6, figPath = resistance, color="#B20000") 95 | 96 | #extras 97 | wordcloud2(hmtTable, size=0.7, color=rep_len( redPalette, nrow(hmtTable) )) 98 | #use a letter or word 99 | letterCloud(hmtTable, word = "HMT", size = 3) 100 | 101 | 102 | ############# West World Tweets ############# 103 | 104 | rt <- search_tweets( 105 | "#westworld", n = 2000, include_rts = FALSE 106 | ) 107 | 108 | 109 | #unnest the words 110 | 111 | westWorld <- rt %>% 112 | unnest_tokens(word, text) 113 | 114 | dim(westWorld) 115 | 116 | westWorld$word 117 | 118 | #remove stop words - aka typically very common words such as "the", "of" etc 119 | 120 | data(stop_words) 121 | 122 | westWorld <- westWorld %>% 123 | anti_join(stop_words) 124 | 125 | dim(westWorld) 126 | 127 | #do a word count 128 | 129 | westWorld <- westWorld %>% 130 | count(word, sort = TRUE) 131 | westWorld 132 | 133 | #remove other nonsense 134 | 135 | westWorld <-westWorld %>% 136 | filter(!word %in% c('t.co', 'https', 'westworld', 'episode', 'season', 'westworldhbo', 137 | 'de', 'la', '2', 'tvtime', 'watched', 'da', 'con', 'eu', 'se', 'es', 'da')) 138 | westWorld 139 | 140 | 141 | #Visualize 142 | 143 | greyPalette <- c("#2B2B2B", "#373D3F", #333333", "#303030", "#404040", "#484848", "#505050", "#606060", 144 | #444444", "#555555", "#666666", "#777777", "#888888", "#999999") 145 | greyPalette 146 | 147 | wordcloud2(westWorld, size=0.6, shape = "star", color=rep_len( greyPalette, nrow(westWorld) )) 148 | ?wordcloud2 149 | 150 | #download maze 151 | url <- "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/twitter_wordcloud/wwmaze2.jpg" 152 | wwMaze <- "wwmaze2.jpg" 153 | download.file(url, wwMaze) # download file 154 | 155 | #download the WW logo 156 | url = "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/twitter_wordcloud/wwlogowithname.jpg" 157 | wwLogo <- "wwlogowithname.jpg" 158 | download.file(url, wwLogo) # download file 159 | 160 | #wordclouds 161 | wordcloud2(westWorld, size=2.6, figPath = wwMaze, color="black") 162 | wordcloud2(westWorld, size=2.6, figPath = wwLogo, color=rep_len( greyPalette, nrow(westWorld) ) ) 163 | wordcloud2(westWorld, size=1.6, figPath = wwLogo, color="black" ) 164 | 165 | 166 | ############# Summer of Data Science ############# 167 | 168 | rt <- search_tweets( 169 | "#SoDS18", n = 5000, include_rts = FALSE 170 | ) 171 | 172 | 173 | #unnest the words 174 | 175 | sods <- rt %>% 176 | unnest_tokens(word, text) 177 | 178 | dim(sods) 179 | 180 | sods$word 181 | 182 | #remove stop words - aka typically very common words such as "the", "of" etc 183 | 184 | data(stop_words) 185 | 186 | sods <- sods %>% 187 | anti_join(stop_words) 188 | 189 | dim(sods) 190 | 191 | #do a word count 192 | 193 | sods <- sods %>% 194 | count(word, sort = TRUE) 195 | sods 196 | 197 | #remove other nonsense 198 | 199 | sods <-sods %>% 200 | filter(!word %in% c('t.co', 'https')) 201 | sods 202 | 203 | 204 | #Visualize 205 | wordcloud2(sods) 206 | letterCloud(sods, word = "#SoDS18", size = 2) 207 | 208 | #download the sods18 logo 209 | url = "https://raw.githubusercontent.com/lgellis/MiscTutorial/master/twitter_wordcloud/sunglasses.jpg" 210 | sun <- "sunglasses.jpg" 211 | download.file(url, sun) 212 | 213 | #wordclouds 214 | wordcloud2(sods) 215 | #not working 216 | wordcloud2(sods, size=2.6, figPath = sun) 217 | 218 | 219 | -------------------------------------------------------------------------------- /twitter_wordcloud/handmaiden.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/handmaiden.jpeg -------------------------------------------------------------------------------- /twitter_wordcloud/handmaidens.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/handmaidens.jpg -------------------------------------------------------------------------------- /twitter_wordcloud/handmaidens2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/handmaidens2.jpeg -------------------------------------------------------------------------------- /twitter_wordcloud/readme.md: -------------------------------------------------------------------------------- 1 | A tutorial showing how to create awesome word clouds with custom shapes using wordcloud2. Also shows how to pull down twitter data with rtweet. 2 | 3 | Tutorial available here for more information on this tutorial, please see [the blog post on littlemissdata.com](https://www.littlemissdata.com/blog/wordclouds) 4 | -------------------------------------------------------------------------------- /twitter_wordcloud/resistance.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/resistance.jpeg -------------------------------------------------------------------------------- /twitter_wordcloud/sods18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/sods18.jpg -------------------------------------------------------------------------------- /twitter_wordcloud/sods18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/sods18.png -------------------------------------------------------------------------------- /twitter_wordcloud/spiceGirls.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/spiceGirls.jpeg -------------------------------------------------------------------------------- /twitter_wordcloud/sun2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/sun2.jpg -------------------------------------------------------------------------------- /twitter_wordcloud/sunglasses.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/sunglasses.jpg -------------------------------------------------------------------------------- /twitter_wordcloud/wwlogo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/wwlogo.jpg -------------------------------------------------------------------------------- /twitter_wordcloud/wwlogowithname.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/wwlogowithname.jpg -------------------------------------------------------------------------------- /twitter_wordcloud/wwmaze2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/twitter_wordcloud/wwmaze2.jpg -------------------------------------------------------------------------------- /usmap/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/usmap/.DS_Store -------------------------------------------------------------------------------- /usmap/.Rhistory: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/usmap/.Rhistory -------------------------------------------------------------------------------- /usmap/readme.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | title: "usmap" 4 | author: "Laura Ellis" 5 | date: "11/27/2019" 6 | output: 7 | md_document: 8 | variant: markdown_github 9 | --- 10 | 11 | ## Tutorial 12 | 13 | The full tutorial and description is available at [littlemissdata.com](https://www.littlemissdata.com/blog) 14 | 15 | ## Install and Load Packages 16 | 17 | Check for packages installed, install if needed and load package. I found this code off of 18 | [Vikram Baliga's blog](https://www.vikram-baliga.com/blog/2015/7/19/a-hassle-free-way-to-verify-that-r-packages-are-installed-and-loaded) 19 | 20 | ```{r} 21 | #specify the packages of interest 22 | packages = c("gtrendsR","tidyverse","usmap") 23 | 24 | #use this function to check if each package is on the local machine 25 | #if a package is installed, it will be loaded 26 | #if any are not, the missing package(s) will be installed and loaded 27 | package.check <- lapply(packages, FUN = function(x) { 28 | if (!require(x, character.only = TRUE)) { 29 | install.packages(x, dependencies = TRUE) 30 | library(x, character.only = TRUE) 31 | } 32 | }) 33 | 34 | #verify they are loaded 35 | search() 36 | 37 | #Set some variables 38 | 39 | orange <- "#C9592E" 40 | 41 | ``` 42 | 43 | 44 | ## Get thanksgiving trends 45 | 46 | Use the [gTrendsR Package](https://cran.r-project.org/web/packages/gtrendsR/gtrendsR.pdf) to get the query trends for thanksgiving in the US for the past 24 hours 47 | 48 | ```{r} 49 | thanksgiving <- gtrends("thanksgiving",geo = "US", time = "now 1-d") # last day 50 | ``` 51 | 52 | ## Gather Interest by State 53 | 54 | Select the data frame for interest by state and then convert the names of the states to FIPS codes (2 characters for state, 5 characters for county). 55 | 56 | ```{r } 57 | 58 | thanksgivingStates <- thanksgiving$interest_by_region 59 | 60 | #The usmap requires the data in FIPS format. 61 | # To gather the FIPS data, use the fips() function to convert state name to code 62 | #https://www.rdocumentation.org/packages/cdlTools/versions/0.14/topics/fips 63 | 64 | thanksgivingStates$fips <-fips(thanksgivingStates$location) 65 | 66 | ``` 67 | 68 | ## Plot interest with the US Map by state 69 | 70 | Create a US heatmap with google search popularity for the keyword "thanksgiving". 71 | 72 | ```{r } 73 | 74 | plot_usmap(data = thanksgivingStates, values = "hits", color = orange, labels=TRUE) + 75 | scale_fill_continuous( low = "white", high = orange, 76 | name = "Popularity", label = scales::comma 77 | ) + 78 | theme(legend.position = "right") + 79 | theme(panel.background = element_rect(colour = "black")) + 80 | labs(title = "Popularity of Thanksgiving Google Search by State", caption = "Source: @littlemissdata") 81 | 82 | ``` 83 | 84 | 85 | 86 | ## Plot interest with the US Map by state 87 | 88 | Drill in on the seemingly most popular regions using the "include" parameter in the plot_usmap() function. Regional divisions can be found in the docs [here](https://cran.r-project.org/web/packages/usmap/usmap.pdf) 89 | 90 | ```{r } 91 | 92 | plot_usmap(data = thanksgivingStates, values = "hits", include = c(.south_atlantic, .east_north_central), color = orange, labels=TRUE) + 93 | scale_fill_continuous( low = "white", high = orange, 94 | name = "Popularity", label = scales::comma 95 | ) + 96 | theme(legend.position = "right") + 97 | theme(panel.background = element_rect(colour = "black")) + 98 | labs(title = "US East Coast Popularity of Thanksgiving Google Search", caption = "Source: @littlemissdata") 99 | 100 | ``` 101 | 102 | 103 | -------------------------------------------------------------------------------- /usmap/readme.md: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------ 2 | 3 | title: “usmap” author: “Laura Ellis” date: “11/27/2019” output: 4 | md\_document: variant: markdown\_github — 5 | 6 | Tutorial 7 | -------- 8 | 9 | The full tutorial and description is available at 10 | [littlemissdata.com](https://www.littlemissdata.com/blog) 11 | 12 | Install and Load Packages 13 | ------------------------- 14 | 15 | Check for packages installed, install if needed and load package. I 16 | found this code off of [Vikram Baliga’s 17 | blog](https://www.vikram-baliga.com/blog/2015/7/19/a-hassle-free-way-to-verify-that-r-packages-are-installed-and-loaded) 18 | 19 | ``` r 20 | #specify the packages of interest 21 | packages = c("gtrendsR","tidyverse","usmap") 22 | 23 | #use this function to check if each package is on the local machine 24 | #if a package is installed, it will be loaded 25 | #if any are not, the missing package(s) will be installed and loaded 26 | package.check <- lapply(packages, FUN = function(x) { 27 | if (!require(x, character.only = TRUE)) { 28 | install.packages(x, dependencies = TRUE) 29 | library(x, character.only = TRUE) 30 | } 31 | }) 32 | ``` 33 | 34 | ## Loading required package: gtrendsR 35 | 36 | ## Loading required package: tidyverse 37 | 38 | ## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ── 39 | 40 | ## ✔ ggplot2 3.2.0 ✔ purrr 0.3.2 41 | ## ✔ tibble 2.1.3 ✔ dplyr 0.8.3 42 | ## ✔ tidyr 0.8.3 ✔ stringr 1.4.0 43 | ## ✔ readr 1.3.1 ✔ forcats 0.4.0 44 | 45 | ## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── 46 | ## ✖ dplyr::filter() masks stats::filter() 47 | ## ✖ dplyr::lag() masks stats::lag() 48 | 49 | ## Loading required package: usmap 50 | 51 | ``` r 52 | #verify they are loaded 53 | search() 54 | ``` 55 | 56 | ## [1] ".GlobalEnv" "package:usmap" "package:forcats" 57 | ## [4] "package:stringr" "package:dplyr" "package:purrr" 58 | ## [7] "package:readr" "package:tidyr" "package:tibble" 59 | ## [10] "package:ggplot2" "package:tidyverse" "package:gtrendsR" 60 | ## [13] "package:stats" "package:graphics" "package:grDevices" 61 | ## [16] "package:utils" "package:datasets" "package:methods" 62 | ## [19] "Autoloads" "package:base" 63 | 64 | ``` r 65 | #Set some variables 66 | 67 | orange <- "#C9592E" 68 | ``` 69 | 70 | Get thanksgiving trends 71 | ----------------------- 72 | 73 | Use the [gTrendsR 74 | Package](https://cran.r-project.org/web/packages/gtrendsR/gtrendsR.pdf) 75 | to get the query trends for thanksgiving in the US for the past 24 hours 76 | 77 | ``` r 78 | thanksgiving <- gtrends("thanksgiving",geo = "US", time = "now 1-d") # last day 79 | ``` 80 | 81 | Gather Interest by State 82 | ------------------------ 83 | 84 | Select the data frame for interest by state and then convert the names 85 | of the states to FIPS codes (2 characters for state, 5 characters for 86 | county). 87 | 88 | ``` r 89 | thanksgivingStates <- thanksgiving$interest_by_region 90 | 91 | #The usmap requires the data in FIPS format. 92 | # To gather the FIPS data, use the fips() function to convert state name to code 93 | #https://www.rdocumentation.org/packages/cdlTools/versions/0.14/topics/fips 94 | 95 | thanksgivingStates$fips <-fips(thanksgivingStates$location) 96 | ``` 97 | 98 | Plot interest with the US Map by state 99 | -------------------------------------- 100 | 101 | Create a US heatmap with google search popularity for the keyword 102 | “thanksgiving”. 103 | 104 | ``` r 105 | plot_usmap(data = thanksgivingStates, values = "hits", color = orange, labels=TRUE) + 106 | scale_fill_continuous( low = "white", high = orange, 107 | name = "Popularity", label = scales::comma 108 | ) + 109 | theme(legend.position = "right") + 110 | theme(panel.background = element_rect(colour = "black")) + 111 | labs(title = "Popularity of Thanksgiving Google Search by State", caption = "Source: @littlemissdata") 112 | ``` 113 | 114 | ![](readme_files/figure-markdown_github/unnamed-chunk-4-1.png) 115 | 116 | Plot interest with the US Map by state 117 | -------------------------------------- 118 | 119 | Drill in on the seemingly most popular regions using the “include” 120 | parameter in the plot\_usmap() function. Regional divisions can be found 121 | in the docs 122 | [here](https://cran.r-project.org/web/packages/usmap/usmap.pdf) 123 | 124 | ``` r 125 | plot_usmap(data = thanksgivingStates, values = "hits", include = c(.south_atlantic, .east_north_central), color = orange, labels=TRUE) + 126 | scale_fill_continuous( low = "white", high = orange, 127 | name = "Popularity", label = scales::comma 128 | ) + 129 | theme(legend.position = "right") + 130 | theme(panel.background = element_rect(colour = "black")) + 131 | labs(title = "US East Coast Popularity of Thanksgiving Google Search", caption = "Source: @littlemissdata") 132 | ``` 133 | 134 | ![](readme_files/figure-markdown_github/unnamed-chunk-5-1.png) 135 | -------------------------------------------------------------------------------- /usmap/readme_files/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/usmap/readme_files/.DS_Store -------------------------------------------------------------------------------- /usmap/readme_files/figure-markdown_github/unnamed-chunk-4-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/usmap/readme_files/figure-markdown_github/unnamed-chunk-4-1.png -------------------------------------------------------------------------------- /usmap/readme_files/figure-markdown_github/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lgellis/MiscTutorial/f4019477dc03a6994e05a9ceaa77184290642d56/usmap/readme_files/figure-markdown_github/unnamed-chunk-5-1.png --------------------------------------------------------------------------------