Basic R for Graphics"
Scott Karr
HW4 School Expenditures by State
11.29.2015
Packages: ggplot2, data.tables
This data frame contains the following columns:
education Per-capita education expenditures, dollars.
income Per-capita income, dollars.
young Proportion under 18, per 1000.
urban Proportion urban, per 1000.
Setup and Load data
- Load Anscombe education expenditure data by state.
## Load Data Frame from website
require(data.table)
## Loading required package: data.table
require(ggplot2)
## Loading required package: ggplot2
AnscombeUrl <- "http://vincentarelbundock.github.io/Rdatasets/csv/car/Anscombe.csv"
df_Anscombe <- read.table(file = AnscombeUrl, header = TRUE, sep = ",")
dt_Anscombe <- data.table(df_Anscombe)
Generate Descriptive Statistics
- Generate summary level descriptive statistics:
head(df_Anscombe)
## X education income young urban
## 1 ME 189 2824 350.7 508
## 2 NH 169 3259 345.9 564
## 3 VT 230 3072 348.5 322
## 4 MA 168 3835 335.3 846
## 5 RI 180 3549 327.1 871
## 6 CT 193 4256 341.0 774
str(df_Anscombe)
## 'data.frame': 51 obs. of 5 variables:
## $ X : Factor w/ 51 levels "AK","AL","AR",..: 22 31 47 20 40 7 35 32 39 36 ...
## $ education: int 189 169 230 168 180 193 261 214 201 172 ...
## $ income : int 2824 3259 3072 3835 3549 4256 4151 3954 3419 3509 ...
## $ young : num 351 346 348 335 327 ...
## $ urban : int 508 564 322 846 871 774 856 889 715 753 ...
Prepare Dataset for Graphics
- Rename columns as needed
## Rename "state" column
##setnames(df_Anscombe, old=c(""), new=c("state"))
colnames(df_Anscombe)[c(1)] <- c("state")
- Histogram showing count of states by increasing per-capita-spending by state
ggplot(data = df_Anscombe) + geom_histogram(aes(x = education))
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

- Plot per-capita-spending to per-capita income grouped by state
g <- ggplot(df_Anscombe, aes(x = education, y = income)) + geom_point()
g <- g + geom_point(aes(color = state)) + facet_wrap(~state)
g
