├── datasets ├── ex2.RData ├── ex3.RData ├── ex4.RData ├── final_ex.RData ├── chapter1.R └── interlude.html ├── .gitignore ├── img ├── author_image.png └── shield_image.png ├── README.md ├── requirements.r ├── courses-googlevis.Rproj ├── course.yml └── chapter1.Rmd /datasets/ex2.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datacamp/misc-courses-googlevis/master/datasets/ex2.RData -------------------------------------------------------------------------------- /datasets/ex3.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datacamp/misc-courses-googlevis/master/datasets/ex3.RData -------------------------------------------------------------------------------- /datasets/ex4.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datacamp/misc-courses-googlevis/master/datasets/ex4.RData -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user/* 2 | .Rproj.user/ 3 | .Rhistory 4 | .RData 5 | .DS_Store 6 | .cache/ 7 | .Rproj.user 8 | -------------------------------------------------------------------------------- /img/author_image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datacamp/misc-courses-googlevis/master/img/author_image.png -------------------------------------------------------------------------------- /img/shield_image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datacamp/misc-courses-googlevis/master/img/shield_image.png -------------------------------------------------------------------------------- /datasets/final_ex.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datacamp/misc-courses-googlevis/master/datasets/final_ex.RData -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Having fun with googleVis 2 | 3 | Source files for the googleVis course. 4 | 5 | This repo is linked to DataCamp Teach. 6 | -------------------------------------------------------------------------------- /requirements.r: -------------------------------------------------------------------------------- 1 | devtools::install_version("rdatamarket", "0.6.5") 2 | devtools::install_version("plyr", "1.8.4") 3 | devtools::install_version("googleVis", "0.6.1") 4 | -------------------------------------------------------------------------------- /courses-googlevis.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | -------------------------------------------------------------------------------- /course.yml: -------------------------------------------------------------------------------- 1 | title: Having Fun with googleVis 2 | author_field: Weston Stearns 3 | description: >- 4 | In this demonstration, you will learn how to use the googleVis package in R to 5 | create a few beautiful interactive charts. The charts created in these 6 | exercises are inspired by the work of data guru Hans 8 | Rosling. GoogleVis provides an interface between R and the Google Charts 9 | Tools, allowing you to create interactive web charts from R without uploading 10 | your data to Google. You can check out the latest googleVis news at co-author 11 | Markus Gesmann's blog. 13 | author_bio: >- 14 | Weston is a member of the DataCamp team and all-around stats guy. He is 15 | passionate about making education efficient and accessible with the help of 16 | technology. He holds a degree in statistics from Harvard where he also served 17 | as a teaching fellow. 18 | university: DataCamp 19 | from: 'r-file-server-prod:v1.1.4' 20 | programming_language: r 21 | -------------------------------------------------------------------------------- /datasets/chapter1.R: -------------------------------------------------------------------------------- 1 | library("rdatamarket") 2 | library("googleVis") 3 | library("plyr") 4 | 5 | dminit(NULL) 6 | life_expectancy <- dmlist("15r2!hrp") 7 | population <- dmlist("1cfl!r3d") 8 | gdp <- dmlist("15c9!hd1") 9 | 10 | save(life_expectancy, population, gdp, file = "datasets/ex2.RData") 11 | 12 | names(gdp)[3] <- "GDP" 13 | names(life_expectancy)[3] <- "LifeExpectancy" 14 | names(population)[3] <- "Population" 15 | development <- join(gdp,life_expectancy) 16 | development <- join(development,population) 17 | selection <- c("Afghanistan","Australia","Austria","Belgium","Bolivia","Brazil","Cambodia", 18 | "Azerbaijan", "Chile","China","Denmark","Estonia","Ethiopia","Finland","France", 19 | "Georgia","Germany","Ghana","Greece","India","Indonesia","Iraq","Italy", 20 | "Japan","Lithuania","Luxembourg","Mexico","New Zealand", "Niger", "Norway", 21 | "Poland", "Portugal","Rwanda", "Somalia", "South Africa", "Spain", "Sweden", 22 | "Switzerland", "Turkey", "Uganda", "Ukraine", "United Kingdom", 23 | "United States", "Vietnam") 24 | 25 | save(development, selection, file = "datasets/ex3.RData") 26 | 27 | development_complete <- subset(development, Year <= 2008) 28 | development_motion <- subset(development_complete, Country %in% selection) 29 | 30 | save(development_motion, file = "datasets/ex4.RData") 31 | 32 | # first gvismotionchart 33 | 34 | prelude <- gvisMotionChart(development_motion, 35 | idvar = "Country", 36 | timevar = "Year") 37 | capture.output(plot(prelude, tag = "html"), file = "datasets/prelude.html") 38 | 39 | interlude <- gvisMotionChart(development_motion, 40 | idvar = "Country", 41 | timevar = "Year", 42 | xvar = "GDP", 43 | yvar = "LifeExpectancy", 44 | sizevar = "Population") 45 | capture.output(plot(prelude, tag = "html"), file = "datasets/interlude.html") 46 | 47 | development_motion$logGDP <- log(development_motion$GDP) 48 | final_output <- gvisMotionChart(development_motion, 49 | idvar = "Country", 50 | timevar = "Year", 51 | xvar = "logGDP", 52 | yvar = "LifeExpectancy", 53 | sizevar = "Population") 54 | capture.output(plot(prelude, tag = "html"), file = "datasets/final_output.html") 55 | 56 | motion_graph <- final_output 57 | save(development_motion, motion_graph, file = "datasets/final_ex.RData") 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /chapter1.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: 'Discovering googleVis' 3 | description: 'In this chapter you will be introduced to gooleVis for R.' 4 | --- 5 | 6 | ## Hans Rosling TED 2006 7 | 8 | ```yaml 9 | type: VideoExercise 10 | key: 0a6585decf 11 | lang: r 12 | xp: 50 13 | skills: 1 14 | video_link: //player.vimeo.com/video/97249290 15 | video_hls: //videos.datacamp.com/transcoded/1007_having-fun-with-googlevis/v1/hls-googlevis+video.master.m3u8 16 | ``` 17 | 18 | 19 | --- 20 | 21 | ## Loading in your data 22 | 23 | ```yaml 24 | type: NormalExercise 25 | key: 692450cb77 26 | lang: r 27 | xp: 100 28 | skills: 1,4 29 | ``` 30 | 31 | What you just saw was one of the most famous TED talks on data and statistics. With the drama and urgency of a sportscaster, data guru [Hans Rosling](http://www.ted.com/speakers/hans_rosling) debunks myths about the so-called "developing world". But what sets Rosling apart isn't just his apt observations of broad social and economic trends, but the stunning way he presents them. Have you ever seen data presented like this? The data sings, trends come to life, and the big picture snaps into sharp focus. 32 | 33 | In this demonstation, you will learn how to DIY with the help of R. Step-by-step you will transform development statistics into moving bubbles and flowing curves that make global trends clear, intuitive and even playful. 34 | 35 | First, you will need to load data on a country's evolution of life expectancy, GDP and population over the past years into R. We can get this data by using [DataMarket](https://datamarket.com/), a company that lets you freely search public data and load it into R with the [`rdatamarket`](http://www.rdocumentation.org/packages/rdatamarket/functions/rdatamarket-package) package. 36 | 37 | Some code that initializes a DataMarket client with an empty API key (which works fine for public datasets) and then fetches life expectancy and population data has already been provided. Can you finish the script? 38 | 39 | `@instructions` 40 | - Use `head()` or `tail()` to inspect both the `life_expectancy` and `population` data frame. 41 | - Use `dmlist()` to pull in data on yearly GDP for different countries. This time, use the ID `"15c9!hd1"`. Store the resulting data frame as `gdp`. 42 | - Time to inspect `gdp` again: use `tail()` this time, to inspect the last observations in `gdp`. 43 | 44 | `@hint` 45 | Have a look at the code for `life_expectancy` on how to load `gdp` and `population`. Do not forget, no API key is required. 46 | 47 | `@pre_exercise_code` 48 | ```{r,eval=FALSE} 49 | library("rdatamarket") 50 | ``` 51 | 52 | `@sample_code` 53 | ```{r} 54 | # Load rdatamarket and initialize client 55 | library("rdatamarket") 56 | dminit(NULL) 57 | 58 | # Pull in life expectancy and population data 59 | life_expectancy <- dmlist("15r2!hrp") 60 | population <- dmlist("1cfl!r3d") 61 | 62 | # Inspect life_expectancy and population with head() or tail() 63 | 64 | 65 | 66 | # Load in the yearly GDP data frame for each country as gdp 67 | 68 | 69 | # Inspect gdp with tail() 70 | 71 | ``` 72 | 73 | `@solution` 74 | ```{r} 75 | # Load rdatamarket and initialize client 76 | library("rdatamarket") 77 | dminit(NULL) 78 | 79 | # Pull in life expectancy and population data 80 | life_expectancy <- dmlist("15r2!hrp") 81 | population <- dmlist("1cfl!r3d") 82 | 83 | # Inspect life_expectancy and population with head() or tail() 84 | head(life_expectancy) 85 | tail(life_expectancy) 86 | head(population) 87 | tail(population) 88 | 89 | # Load in the yearly GDP data frame for each country as gdp 90 | gdp <- dmlist("15c9!hd1") 91 | 92 | # Inspect gdp with head() or tail() 93 | head(gdp) 94 | tail(gdp) 95 | ``` 96 | 97 | `@sct` 98 | ```{r} 99 | msg <- "Don't change the code that pulls in the life expectancy and population data. You can use the arrow next to Submit Answer to reset your code." 100 | test_object("life_expectancy", undefined_msg = msg, incorrect_msg = msg) 101 | test_object("population", undefined_msg = msg, incorrect_msg = msg) 102 | 103 | test_or(test_output_contains("head(life_expectancy)"), 104 | test_output_contains("tail(life_expectancy)"), 105 | incorrect_msg = "Did you print the start and/or the end of the life expectancies data frame to the console?") 106 | 107 | test_correct({ 108 | test_object("gdp", incorrect_msg = "There's still something wrong with `gdp`. Make sure to assign the result of `dmlist(\"15c9!hd1\"` to `gdp`.") 109 | }, { 110 | test_function("dmlist", args = "ds", index = 3, 111 | incorrect_msg = "Have you correclty used `dmlist()` to pull in the GDP data? Look at the other `dmlist()` calls as an example. Remember to use the key `\"15c9!hd1\"`.") 112 | }) 113 | 114 | test_or(test_output_contains("head(gdp)"), 115 | test_output_contains("tail(gdp)"), 116 | incorrect_msg = "Did you print the first and/or the last observations in the `gdp` data frame? Use `head()` and/or `tail()`?") 117 | 118 | success_msg("Good job! Now that you've imported the data, continue to the next exercise to start the real work.") 119 | ``` 120 | 121 | --- 122 | 123 | ## Preparing the data 124 | 125 | ```yaml 126 | type: NormalExercise 127 | key: 8ee4cdeac6 128 | lang: r 129 | xp: 100 130 | skills: 1,4 131 | ``` 132 | 133 | Now you have the following three different datasets at your disposal: `life_expectancy`, `gdp`, and `population`. As of now, these datasets will always be preloaded in all the exercises' workspaces so you can access and use them at any time. 134 | 135 | If you've applied the [`head()`](http://www.rdocumentation.org/packages/multivator/functions/head) and/or [`tail()`](http://www.rdocumentation.org/packages/multivator/functions/head) function to these variables you've probably noticed that: 136 | 137 | - Not all column names are named properly: the string "Value" is used to name the GDP value, the life expectancy value, and the population value. It would be better if you could make these more descriptive and unique for each. (Tip: use `names()` to see the column names of a dataset.) 138 | - Our data is only complete until 2008. 139 | 140 | These issues should be fixed before you start creating your graph. In addition, if you want to map all three development statistics into one interactive graph (and you should because it is extremely cool), you will have to merge your three data frames into one called `development`. 141 | 142 | In this exercise you will be required to do this with the [`join()`](http://www.rdocumentation.org/packages/adehabitatMA/html/join.html) function from the [`plyr()`](http://www.rdocumentation.org/packages/plyr/functions/plyr) package. You can read more on how to use it by typing `?join` in your console. 143 | 144 | `@instructions` 145 | - The code to rename `Value` variable to `GDP` for the `gdp` dataset is provided. Now rename the column name `Value` in the other two datasets to respectively `Population` and `LifeExpectancy`. 146 | - Finish the `join()` calls so that: 147 | + `gdp` and `life_expectancy` are joined into `gdp_life_exp`, 148 | + `gdp_life_exp` and `population` are joined into `development`. 149 | There's no need to specify additional arguments, because you want to join on all common variables (`Country` and `Year` in this case). 150 | 151 | `@hint` 152 | Joining our three data frames into one is very easy in this case. If you have 3 datasets to join, `data_one`, `data_two` and `data_three`, on all common variables, you go in steps: `data_one_two <- join(data_one, data_two)` and then `data_total <- join(data_one_two, data_three)`. So every `join()` only needs two arguments here. 153 | 154 | `@pre_exercise_code` 155 | ```{r,eval=FALSE} 156 | library("rdatamarket") 157 | load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_1007/datasets/ex2.RData")) 158 | ``` 159 | 160 | `@sample_code` 161 | ```{r} 162 | # Load in the plyr package 163 | library("plyr") 164 | 165 | # Rename the Value for each dataset 166 | names(gdp)[3] <- "GDP" 167 | 168 | 169 | 170 | # Use plyr to join your three data frames into one: development 171 | gdp_life_exp <- join(gdp, ___) 172 | development <- join(gdp_life_exp, ___) 173 | ``` 174 | 175 | `@solution` 176 | ```{r} 177 | # Load in the plyr package 178 | library("plyr") 179 | 180 | # Rename the Value for each dataset 181 | names(gdp)[3] <- "GDP" 182 | names(life_expectancy)[3] <- "LifeExpectancy" 183 | names(population)[3] <- "Population" 184 | 185 | # Use plyr to join your three data frames into one: `development` 186 | gdp_life_exp <- join(gdp, life_expectancy) 187 | development <- join(gdp_life_exp, population) 188 | ``` 189 | 190 | `@sct` 191 | ```{r} 192 | test_error() 193 | 194 | msg <- "Don't change the code that renames the `Value` column of `gdp`." 195 | test_data_frame("gdp", "GDP", undefined_cols_msg = msg, incorrect_msg = msg) 196 | msg <- "Have you correctly renamed the `Value` column of `%s`? Take the code for `gdp` as an example." 197 | test_data_frame("life_expectancy", "LifeExpectancy", undefined_cols_msg = sprintf(msg, "life_expectancy"), incorrect_msg = sprintf(msg, "life_expectancy")) 198 | test_data_frame("population", "Population", undefined_cols_msg = sprintf(msg, "life_expectancy"), incorrect_msg = sprintf(msg, "life_expectancy")) 199 | 200 | test_object("gdp_life_exp", incorrect_msg = "Have you correctly joined `gdp` and `life_expectancy` to create `gdp_life_exp`?") 201 | test_object("development", incorrect_msg = "Have you correclty joined `gdp_life_exp` and `population` to create `development`?") 202 | 203 | success_msg("Your data is almost ready! Only one more set of data preparations and you can start to make some sweet visualizations!") 204 | ``` 205 | 206 | --- 207 | 208 | ## Last data preps 209 | 210 | ```yaml 211 | type: NormalExercise 212 | key: eba2e8eb5b 213 | lang: r 214 | xp: 100 215 | skills: 1,4 216 | ``` 217 | 218 | Now that you have merged your data, it would make sense to trim the data set. You can do this in 2 ways: 219 | 220 | - Take out data for years you know that have incomplete observations. In this case, the data is only complete up until 2008. 221 | - Trim down the data set to include fewer countries. Your dataframe `development` is currently loaded and contains observations about 226 countries per year. That could be a bit messy to plot on one graph. 222 | 223 | One way to do this would be to make use of the `subset()` function. You can use this function to pull values from your data frame based on sets of conditions. For example, if you want to see only observations from 2005: 224 | 225 | ``` 226 | dev_2005 <- subset(development, Year == 2005) 227 | ``` 228 | 229 | Then if you want to see only countries that had a gdp of over 30,000 in 2005: 230 | 231 | ``` 232 | dev_2005_big <- subset(dev_2005, GDP >= 30000) 233 | ``` 234 | 235 | Feel free to type `?subset` in the console to read more on how to use the function. 236 | 237 | `@instructions` 238 | - Take a subset from the `development` dataset that contains observations whose `Year` variable is smaller than or equal to 2008. Store the result as `development_complete`. 239 | - Print out the final few rows of `development_complete` with `tail()`. 240 | - To make sure the graph is not too busy, you will work with a subset of only a few countries. These country names are stored in the variable `selection`. Take a subset from `development_complete` for which the `Country` variable is in `selection`. Name this dataset `development_motion`. The operator `%in%` will be helpful here. 241 | 242 | `@hint` 243 | - To build `develompent_complete`, use `subset()` on `development`, with the condition `Year <- 2008`. 244 | - You can use the condition `Country %in% selection` for the second `subset()` operation. 245 | 246 | `@pre_exercise_code` 247 | ```{r,eval=FALSE} 248 | library("rdatamarket") 249 | library("plyr") 250 | load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_1007/datasets/ex3.RData")) 251 | ``` 252 | 253 | `@sample_code` 254 | ```{r} 255 | # development and selection are available in the workspace 256 | 257 | # Subset development with Year on or before 2008 258 | development_complete <- subset(___, ___) 259 | 260 | # Print out tail of development_complete 261 | 262 | 263 | # Subset development_complete: keep only countries in selection 264 | development_motion <- subset(___, ___) 265 | ``` 266 | 267 | `@solution` 268 | ```{r} 269 | # development and selection are available in the workspace 270 | 271 | # Only include data on or before 2008 and inspect the tail of the data frame 272 | development_complete <- subset(development, Year <= 2008) 273 | 274 | # Print out tail of development_complete 275 | tail(development_complete) 276 | 277 | # Subset development_complete: keep only countries in selection 278 | development_motion <- subset(development_complete, Country %in% selection) 279 | ``` 280 | 281 | `@sct` 282 | ```{r} 283 | test_object("development_complete", incorrect_msg = "`development_complete` is not correct. Have you correclty subsetted `development()`, to keep only observations with `Year <= 2008`?") 284 | test_output_contains("tail(development_complete)", 285 | incorrect_msg = "Don't forget to inspect the last few rows of `development_complete`.") 286 | test_object("development_motion", incorrect_msg = "`development_motion` is not correct. Have you correclty subsetted `development_complete` to keep only observations with `Country %in% selection`?") 287 | test_error() 288 | success_msg("Looks like your data is ready to rumble! Time to make Hans Rosling proud.") 289 | ``` 290 | 291 | --- 292 | 293 | ## googleVis - the prelude 294 | 295 | ```yaml 296 | type: NormalExercise 297 | key: 8909f97907 298 | lang: r 299 | xp: 100 300 | skills: 1,4 301 | ``` 302 | 303 | Time to start the magic! In the next exercises, you will be introduced to the [`googleVis`](http://www.rdocumentation.org/packages/googleVis) package. This package provides an interface between R and the [Google Chart Tools](https://developers.google.com/chart/). As with every package, `googleVis` give you access to various functions. Here they will allow you to visualize data with the Google Chart Tools without uploading your data to Google. 304 | 305 | For this exercise, you will need to create your first motion chart. A motion chart is a dynamic chart to explore several indicators over time. To create a motion chart with the [`googleVis`](http://www.rdocumentation.org/packages/googleVis) package, you will need to use the [`gvisMotionChart()`](http://www.rdocumentation.org/packages/googleVis) function (what's in a name?). The beauty of this function is in its simplicity, and the huge range of tweaks you can do to prettify your graph. 306 | 307 | The [`gvisMotionChart()`](http://www.rdocumentation.org/packages/googleVis) function in its simplest form takes 3 arguments. The first argument you need to provide is your data frame `development_motion` (which is pre-loaded). Next, you assign the subject to be analyzed to the `idvar` argument, and to the `timevar` argument the column name that contains the time dimension data. Et voila, your first motion chart with `googleVis` is ready! 308 | 309 | `@instructions` 310 | - Use the `gvisMotionChart()` function to create an interactive motion chart with R. Assign the output to `motion_graph`. What column names do you need to provide to the `idvar` and `timevar` if you know that the Hans Rosling moving bubbles represent countries, and their movement is based on the date? Pass the column names as strings. 311 | - Use the [`plot()`](http://www.rdocumentation.org/packages/Rssa/functions/plot) function on `motion_graph` to visualize your first motion graph. 312 | 313 | `@hint` 314 | The `idvar` argument should be equal to `"Country"` and the `timevar` argument to `"Year"`. 315 | 316 | `@pre_exercise_code` 317 | ```{r,eval=FALSE} 318 | library("googleVis") 319 | options(gvis.plot.tag = 'chart') 320 | load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_1007/datasets/ex4.RData")) 321 | ``` 322 | 323 | `@sample_code` 324 | ```{r} 325 | # development_motion is pre-loaded in your workspace 326 | 327 | # Create the interactive motion chart 328 | motion_graph <- gvisMotionChart(___, 329 | idvar = ___, 330 | timevar = ___) 331 | 332 | # Plot motion_graph 333 | 334 | ``` 335 | 336 | `@solution` 337 | ```{r} 338 | # development_motion is pre-loaded in your workspace 339 | 340 | # Create the interactive motion chart 341 | motion_graph <- gvisMotionChart(development_motion, 342 | idvar = "Country", 343 | timevar = "Year") 344 | 345 | # Plot motion_graph 346 | plot(motion_graph) 347 | ``` 348 | 349 | `@sct` 350 | ```{r} 351 | # Instruction 1 352 | test_function("gvisMotionChart", c("data"), eval = F, incorrect_msg = "The first argument you pass to `gvisMotionChart()` should be `development_motion`.") 353 | test_function("gvisMotionChart", c("idvar", "timevar"), incorrect_msg = "Make sure the `idvar` argument is set to `\"Country\"` and the `timevar` argument is set to `\"Year\"`.") 354 | 355 | # Instruction 2 356 | test_function("plot", "x", eval = F, 357 | not_called_msg = "Do not forget to plot your new motion graph with `plot()`!", 358 | incorrect_msg = "Make sure to pass `motion_graph` to the `plot()` function.") 359 | 360 | test_error() 361 | success_msg("Bellissimo! Take some time to enjoy the fruits of your labor and play around with the motion chart! You can check the chart in a separate window [here](http://s3.amazonaws.com/assets.datacamp.com/production/course_1007/datasets/prelude.html). In the next exercise you will start to place the icing on the cake.") 362 | ``` 363 | 364 | --- 365 | 366 | ## googleVis - the interlude 367 | 368 | ```yaml 369 | type: NormalExercise 370 | key: ad3c2b1839 371 | lang: r 372 | xp: 100 373 | skills: 1,4 374 | ``` 375 | 376 | When working with a simple dataset to visualize, a single color and size for each observation is sufficient. But what if you like to know more? For example, how can you see at a glance which dot represents which country in your motion chart? Or how are these different dots proportionate to each other? 377 | 378 | To make the motion chart even more understandable you can play with the size and color of each bubble. This way you can present more information into one motion chart. Doing this with googleVis is not that hard, again, you only have to play a little bit with the arguments. 379 | 380 | Let's say you want to make a motion chart that displays the GDP of a country on the x-axis and the life expectancy on the y-axis. Furthermore, you think it would be nice if each country has a unique color, and the size of each bubble represents the size of the population of that country. Doing this via the `gvisMotionChart()` function will require adding 3 additional arguments to our existing function: 381 | 382 | - `xvar`: Here you place the column name of the variable to be plotted on the x-axis 383 | - `yvar`: Here you place the column name of the variable to be plotted on the y-axis 384 | - `sizevar`: Here you provide the column name that will make the bubbles change size. 385 | 386 | Time to set this into action... 387 | 388 | `@instructions` 389 | - The code you wrote for `motion_graph` in the previous exercise is provided. Add the required additional arguments: 390 | + Set `xvar` to `"GDP"`. 391 | + Set `yvar` to `"LifeExpectancy"`. 392 | + Set `sizevar` so that the bubble size should increases if a country has a larger number of citizens. 393 | - As before, use `plot()` to plot your updated chart. 394 | 395 | `@hint` 396 | You need to set three additional arguments in `gvisMotionChart()`: `xvar`, `yvar`, and `sizevar`. Check out the arguments you have already set. 397 | 398 | `@pre_exercise_code` 399 | ```{r} 400 | library("googleVis") 401 | options(gvis.plot.tag = 'chart') 402 | load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_1007/datasets/ex4.RData")) 403 | ``` 404 | 405 | `@sample_code` 406 | ```{r} 407 | # Update the interactive motion chart 408 | motion_graph <- gvisMotionChart(development_motion, 409 | idvar = "Country", 410 | timevar = "Year", 411 | xvar = ___, 412 | ___, 413 | ___) 414 | 415 | # Plot motion_graph 416 | plot(motion_graph) 417 | ``` 418 | 419 | `@solution` 420 | ```{r} 421 | # Update the interactive motion chart 422 | motion_graph <- gvisMotionChart(development_motion, 423 | idvar = "Country", 424 | timevar = "Year", 425 | xvar = "GDP", 426 | yvar = "LifeExpectancy", 427 | sizevar = "Population") 428 | 429 | # Plot motion_graph 430 | plot(motion_graph) 431 | ``` 432 | 433 | `@sct` 434 | ```{r} 435 | test_function("gvisMotionChart", c("data", "idvar", "timevar"), eval = c(F, T, T), 436 | incorrect_msg = "Don't change anything about the arguments you specified in the previous exercise.") 437 | test_function("gvisMotionChart", c("xvar", "yvar"), 438 | incorrect_msg = "Make sure the `xvar` argument is set to `\"GDP\"` and the `yvar` argument is set to `\"LifeExpectancy\"`.") 439 | test_function("gvisMotionChart", c("sizevar"), incorrect_msg = "Have you correctly specified the `sizevar` argument? The bubble size should scale according to `\"Population\"`...") 440 | 441 | test_function("plot", "x", eval = FALSE, 442 | not_called_msg = "Do not forget to plot your updated motion graph!", 443 | incorrect_msg = "Pass `motion_graph` to `plot()` to plot your updated motion graph.") 444 | 445 | test_error() 446 | success_msg("Fantastic! For a full view of the plot, check [this page](http://s3.amazonaws.com/assets.datacamp.com/production/course_1007/datasets/interlude.html). The plot looks great, but you could make one more adjustment to make the plot a little easier to see.") 447 | ``` 448 | 449 | --- 450 | 451 | ## googleVis - final output 452 | 453 | ```yaml 454 | type: NormalExercise 455 | key: 76a5556f03 456 | lang: r 457 | xp: 100 458 | skills: 1,4 459 | ``` 460 | 461 | That last plot looked awesome! There is clearly a relationship between a country's GDP per capita and life expectancy at birth. However, it looks like the relationship is non-linear. You should make a transformation to the data to see if you can make the plot easier to read. 462 | 463 | `@instructions` 464 | - Create a new column in `development_motion` that corresponds to the log of the GDP column. Call this collumn `logGDP`. 465 | - The code you wrote for `motion_graph` in the previous exercise is provided. Change the x-axis argument to our newly created `logGDP` column. 466 | - As usual, plot `motion_graph` with `plot()`. 467 | 468 | `@hint` 469 | The argument you need to set the argument `xvar` to the log of GDP in the `gvisMotionChart()` function 470 | 471 | `@pre_exercise_code` 472 | ```{r} 473 | library("googleVis") 474 | options(gvis.plot.tag = 'chart') 475 | load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_1007/datasets/ex4.RData")) 476 | ``` 477 | 478 | `@sample_code` 479 | ```{r} 480 | # Create a new column that corresponds to the log of the GDP column 481 | development_motion$logGDP <- ___ 482 | 483 | # Create the interactive motion chart with R and `gvisMotionChart())` 484 | motion_graph <- gvisMotionChart(development_motion, 485 | idvar = "Country", 486 | timevar = "Year", 487 | xvar = "___", 488 | yvar = "LifeExpectancy", 489 | sizevar = "Population") 490 | 491 | # Plot your new motion graph with the help of `plot()` 492 | ``` 493 | 494 | `@solution` 495 | ```{r} 496 | # Create a new column that corresponds to the log of the GDP column 497 | development_motion$logGDP <- log(development_motion$GDP) 498 | 499 | # Create the interactive motion chart with R and `gvisMotionChart())` 500 | motion_graph <- gvisMotionChart(development_motion, 501 | idvar = "Country", 502 | timevar = "Year", 503 | xvar = "logGDP", 504 | yvar = "LifeExpectancy", 505 | sizevar = "Population") 506 | 507 | # Plot your new motion graph with the help of `plot()` 508 | plot(motion_graph) 509 | ``` 510 | 511 | `@sct` 512 | ```{r} 513 | test_data_frame("development_motion", "logGDP", 514 | incorrect_msg = "Have you correctly calculated the new column, `logGDP`? Use `log()` on `development_motion$GDP`.") 515 | 516 | test_function("gvisMotionChart", c("data", "idvar", "timevar", "yvar", "sizevar"), eval = c(F, T, T, T, T), 517 | incorrect_msg = "Apart from the `xvar` argument, nothing should be changed about the `gvisMotionChart` call!") 518 | test_function("gvisMotionChart", "xvar", incorrect_msg = "Have you correctly changed the `xvar` argument? You want to plot the `logGDP` column onto the x-axis now.") 519 | 520 | test_function("plot", "x", eval = FALSE, 521 | not_called_msg = "Do not forget to plot your updated motion graph!", 522 | incorrect_msg = "Pass `motion_graph` to `plot()` to plot your updated motion graph.") 523 | 524 | test_error() 525 | success_msg("Isn't that beautiful! [Click here](http://s3.amazonaws.com/assets.datacamp.com/production/course_1007/datasets/final_output.html) for a full view. Again, play around with the graph and get a good understanding of what it represents. Then head over to the final question...") 526 | ``` 527 | 528 | --- 529 | 530 | ## googleVis - the recessional 531 | 532 | ```yaml 533 | type: MultipleChoiceExercise 534 | key: 1fd167fb38 535 | lang: r 536 | xp: 50 537 | skills: 1,4 538 | ``` 539 | 540 | As mentioned in the beginning, the goal of these charts is (not only) to impress your audience, but also to visualize trends and to provide a more clear view on the data and the corresponding insights. 541 | 542 | To test your understanding of the data, you end this demo chapter by solving the following multiple choice question: "What was the GDP of Germany in 1980, and what was the population size at that time?" 543 | 544 | The `development_motion` dataset is available in the workshop, if you want to play around with it. 545 | 546 | `@possible_answers` 547 | - 81,653,702 Germans producing a GDP of 11,746 548 | - 78,297,904 Germans producing a GDP of 12,092 549 | - 78,297,904 Germans producing a GDP of 72.7 550 | - 81,653,702 Germans producing a GDP of 30.89 551 | 552 | `@hint` 553 | You can still have a look at your interactive motion chart by typing `plot(motion_graph)` into the console. 554 | 555 | `@pre_exercise_code` 556 | ```{r} 557 | library("googleVis") 558 | options(gvis.plot.tag = 'chart') 559 | load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_1007/datasets/final_ex.RData")) 560 | plot(motion_graph) 561 | ``` 562 | 563 | `@sct` 564 | ```{r,eval=FALSE} 565 | msg <- "Have another look at your graph. Use the hint if you need extra help." 566 | okmsg <- "Wunderbar! We hope you enjoyed doing this demo on DataCamp. In the future, we plan to do more courses on using interactive visualizations to analyze and present your data via R and googleVis. In the meantime, keep practicing, and maybe you can, sometime, share the stage with Hans Rosling." 567 | test_mc(2, c(msg, okmsg, msg, msg)) 568 | success_msg("Congrats on making some seriously awesome graphs! You can learn tons of other ways to visualize data using R [here on our courses page](https://www.datacamp.com/courses?learn=data_visualization) !") 569 | ``` 570 | -------------------------------------------------------------------------------- /datasets/interlude.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | MotionChartID1a6b47bdea38 6 | 7 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 12651 | 12652 | 12653 | 12654 | 12655 | 12656 | 12657 |
12659 |
12660 |
Data: development_motion • Chart ID: MotionChartID1a6b47bdea38googleVis-0.5.10
12661 | 12662 | 12663 | R version 3.2.2 (2015-08-14) 12664 | • Google Terms of UseDocumentation and Data Policy 12665 |
12666 | 12667 | 12668 | --------------------------------------------------------------------------------