Some of the code from Chapter 3, Section 1.

In this chapter ggplot2 is introduced. We will discuss ggplot2 more in Stat. 651.

The main idea of making graphs with ggplot2 is the idea of mappings that are applied as asthetics in a plot.

The ggplot2 package is part of the tidyverse. We will install and load the tidyverse.

library(mdsr)
library(tidyverse)
CIACountries

Aesthetics

g <- ggplot(data = CIACountries, aes(y = gdp, x = educ))
g + geom_point(size = 3)

g + geom_point(aes(color = net_users), size = 3)

g <- ggplot(data = CIACountries, aes(y = gdp, x = educ, label = country, color = net_users))
g + geom_text(size = 3)

Scale

g <- ggplot(data = CIACountries, aes(y = gdp, x = educ))
g + geom_point(size = 3)

g + geom_point(aes(color = net_users, size = roadways)) 

g + geom_point(aes(color = net_users, size = roadways)) +
   coord_trans(y = "log10")

Facets

g + geom_point(alpha = 0.9, aes(size = roadways)) +
   coord_trans(y = "log10") +
  facet_wrap( ~ net_users, nrow = 1) +
  theme(legend.position = "top")

LS0tCnRpdGxlOiAiRGF0YSBWaXN1YWxpemF0aW9uIFIiCmF1dGhvcjogIlByb2YuIEVyaWMgQS4gU3Vlc3MiCm91dHB1dDoKICB3b3JkX2RvY3VtZW50OiBkZWZhdWx0CiAgaHRtbF9ub3RlYm9vazogZGVmYXVsdAogIHBkZl9kb2N1bWVudDogZGVmYXVsdAotLS0KClNvbWUgb2YgdGhlIGNvZGUgZnJvbSBDaGFwdGVyIDMsIFNlY3Rpb24gMS4KCkluIHRoaXMgY2hhcHRlciBnZ3Bsb3QyIGlzIGludHJvZHVjZWQuICBXZSB3aWxsIGRpc2N1c3MgZ2dwbG90MiBtb3JlIGluIFN0YXQuIDY1MS4KClRoZSBtYWluIGlkZWEgb2YgbWFraW5nIGdyYXBocyB3aXRoIGdncGxvdDIgaXMgdGhlIGlkZWEgb2YgbWFwcGluZ3MgdGhhdCBhcmUgYXBwbGllZCBhcyBhc3RoZXRpY3MgaW4gYSBwbG90LgoKVGhlIGdncGxvdDIgcGFja2FnZSBpcyBwYXJ0IG9mIHRoZSB0aWR5dmVy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