Commit 88e12cf4 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+116 KiB

File added.

No diff preview for this file type.

+7 −0
Changes for Data Visualization in R With ggplot2 - Working Files/Chapter 2/No Work Files.rtf: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural

\f0\fs24 \cf0 No work files for this lesson...}
 No newline at end of file
+72 −0
Changes for Data Visualization in R With ggplot2 - Working Files/Chapter 3/basic_plots.R: 72 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Basic plots

library("ggplot2")
library("gapminder")
library("dplyr")

## Subset data for 2007
gap_07 <- filter(gapminder, year == 2007)
head(gap_07)

## Create scatter plot of GDP vs. Life Expectancy
ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

## Create bar chart of counts by continent
ggplot(gap_07, aes(x = continent)) +
  geom_bar()

## Calculate median population by continent
gap_07_med <- gap_07 %>%
  group_by(continent) %>%
  summarize(pop = median(pop))

head(gap_07_med)

## Create bar chart of median population values
ggplot(gap_07_med, aes(x = continent, y = pop)) +
  geom_bar(stat = "identity")

## Subset data for Iceland
gap_iceland <- filter(gapminder, country == "Iceland")

## Plot lines and points for GDP over time
p <- ggplot(gap_iceland, aes(x = year, y = gdpPercap))

p + geom_point()
p + geom_line()
p + geom_point() + geom_line()

## Save plots
p +
  geom_point() +
  geom_line() +
  ggsave("iceland_gdp.png", height = 6, width = 8)

p2 <- ggplot(gap_iceland, aes(x = year, y = gdpPercap)) +
  geom_point() +
  geom_line()

ggsave("iceland_gdp.png", plot = p2, height = 6, width = 8)





















+17 −0
Changes for Data Visualization in R With ggplot2 - Working Files/Chapter 3/basic_plots_practice.R: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Practice exercises: basic plots

library("ggplot2")
library("gapminder")
library("dplyr")

## 1) Create a scatter plot of life expectancy over time for Asia




## 2) Create a line chart of GDP over time for Hungary




## 3) Create a bar chart showing population of countries in Oceania in 1967
+26 −0
Changes for Data Visualization in R With ggplot2 - Working Files/Chapter 3/basic_plots_practice_answers.R: 26 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Practice exercises: basic plots

library("ggplot2")
library("gapminder")
library("dplyr")

## 1) Create a scatter plot of life expectancy over time for Asia

gap_asia <- filter(gapminder, continent == "Asia")

ggplot(gap_asia, aes(x = year, y = lifeExp)) +
  geom_point()

## 2) Create a line chart of GDP over time for Hungary

gap_hungary <- filter(gapminder, country == "Hungary")

ggplot(gap_hungary, aes(x = year, y = gdpPercap)) +
  geom_line()

## 3) Create a bar chart showing population of countries in Oceania in 1967

gap_oceania <- filter(gapminder, continent == "Oceania" & year == 1967)

ggplot(gap_oceania, aes(x = country, y = pop)) +
  geom_bar(stat = "identity")
Loading