Commit 6324e481 authored by O'Reilly Media, Inc's avatar O'Reilly Media, Inc
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
  
# R Graphics Cookbook, by Winston Chang
  
[![R Graphics Cookbook, by Winston Chang](http://akamaicovers.oreilly.com/images/9781449316945/cat.gif)](https://www.safaribooksonline.com/library/view/title/9781449363086//)
  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.

example_code/appa.txt

0 → 100644
+106 −0
Changes for example_code/appa.txt: 106 added lines, 0 removed lines.
Original line number Diff line number Diff line
appendix: Introduction to ggplot2
==================
library(gcookbook) # For the data set
simpledat

   A1 A2 A3
B1 10  7 12
B2  9 11  6

    
    
====================================
barplot(simpledat, beside=TRUE)
    
    
====================================
t(simpledat)

   B1 B2
A1 10  9
A2  7 11
A3 12  6

    
    
====================================
barplot(t(simpledat), beside=TRUE)
    
    
====================================
plot(simpledat[1,], type="l")
lines(simpledat[2,], type="l", col="blue")
    
    
====================================
simpledat_long

 Aval Bval value
   A1   B1    10
   A1   B2     9
   A2   B1     7
   A2   B2    11
   A3   B1    12
   A3   B2     6

    
    
====================================
library(ggplot2)
ggplot(simpledat_long, aes(x=Aval, y=value, fill=Bval)) +
    geom_bar(stat="identity", position="dodge")
    
    
====================================
ggplot(simpledat_long, aes(x=Bval, y=value, fill=Aval)) +
    geom_bar(stat="identity", position="dodge")
    
    
====================================
ggplot(simpledat_long, aes(x=Aval, y=value, colour=Bval, group=Bval)) +
    geom_line()
    
    
====================================
dat <- data.frame(xval=1:4, yval=c(3,5,6,9), group=c("A","B","A","B"))
dat

 xval yval group
    1    3     A
    2    5     B
    3    6     A
    4    9     B

    
    
====================================
ggplot(dat, aes(x=xval, y=yval))
    
    
====================================
ggplot(dat, aes(x=xval, y=yval)) + geom_point()
    
    
====================================
p <- ggplot(dat, aes(x=xval, y=yval))
p + geom_point()
    
    
====================================
p + geom_point(aes(colour=group))
    
    
====================================
p + geom_point(colour="blue")
    
    
====================================
p + geom_point() + scale_x_continuous(limits=c(0,8))
    
    
====================================
p + geom_point() +
    scale_colour_manual(values=c("orange","forestgreen"))
    
    
==================
 No newline at end of file

example_code/ch00.txt

0 → 100644
+11 −0
Changes for example_code/ch00.txt: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
preface: Preface
==================
install.packages("ggplot2")
install.packages("gcookbook")
    
    
====================================
library(ggplot2)
    
    
==================
 No newline at end of file

example_code/ch01.txt

0 → 100644
+87 −0
Changes for example_code/ch01.txt: 87 added lines, 0 removed lines.
Original line number Diff line number Diff line
chapter: R Basics
==================
install.packages(c("ggplot2", "gcookbook"))
    
    
====================================
library(ggplot2)
library(gcookbook)
    
    
====================================
install.packages("ggplot2")
    
    
====================================
library(ggplot2)
    
    
====================================
data <- read.csv("datafile.csv")
    
    
====================================
data <- read.csv("datafile.csv", header=FALSE)
    
    
====================================
# Manually assign the header names
names(data) <- c("Column1","Column2","Column3")
    
    
====================================
data <- read.csv("datafile.csv", sep="\t")
    
    
====================================
data <- read.csv("datafile.csv", stringsAsFactors=FALSE)

# Convert to factor
data$Sex <- factor(data$Sex)

str(data)

'data.frame':   3 obs. of  4 variables:
 $ First : chr  "Currer" "Dr." ""
 $ Last  : chr  "Bell" "Seuss" "Student"
 $ Sex   : Factor w/ 2 levels "F","M": 1 2 NA
 $ Number: int  2 49 21

    
    
====================================
# Only need to install once
install.packages("xlsx")

library(xslx)
data <- read.xlsx("datafile.xlsx", 1)
    
    
====================================
# Only need to install once
install.packages("gdata")

library(gdata)
# Read first sheet
data <- read.xls("datafile.xls")
    
    
====================================
data <- read.xlsx("datafile.xls", sheetIndex=2)

data <- read.xlsx("datafile.xls", sheetName="Revenues")
    
    
====================================
data <- read.xls("datafile.xls", sheet=2)
    
    
====================================
# Only need to install the first time
install.packages("foreign")

library(foreign)
data <- read.spss("datafile.sav")
    
    
==================
 No newline at end of file

example_code/ch02.txt

0 → 100644
+168 −0
Changes for example_code/ch02.txt: 168 added lines, 0 removed lines.
Original line number Diff line number Diff line
chapter: Quickly Exploring Data
==================
plot(mtcars$wt, mtcars$mpg)
    
    
====================================
library(ggplot2)
qplot(mtcars$wt, mtcars$mpg)
    
    
====================================
qplot(wt, mpg, data=mtcars)
# This is equivalent to:
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
    
    
====================================
plot(pressure$temperature, pressure$pressure, type="l")
    
    
====================================
plot(pressure$temperature, pressure$pressure, type="l")
points(pressure$temperature, pressure$pressure)

lines(pressure$temperature, pressure$pressure/2, col="red")
points(pressure$temperature, pressure$pressure/2, col="red")
    
    
====================================
library(ggplot2)
qplot(pressure$temperature, pressure$pressure, geom="line")
    
    
====================================
qplot(temperature, pressure, data=pressure, geom="line")
# This is equivalent to:
ggplot(pressure, aes(x=temperature, y=pressure)) + geom_line()


# Lines and points together
qplot(temperature, pressure, data=pressure, geom=c("line", "point"))
# Equivalent to:
ggplot(pressure, aes(x=temperature, y=pressure)) + geom_line() + geom_point()
    
    
====================================
barplot(BOD$demand, names.arg=BOD$Time)
    
    
====================================
table(mtcars$cyl)

 4  6  8
11  7 14
# There are 11 cases of the value 4, 7 cases of 6, and 14 cases of 8

    
    
====================================
# Generate a table of counts
barplot(table(mtcars$cyl))
    
    
====================================
library(ggplot2)
qplot(BOD$Time, BOD$demand, geom="bar", stat="identity")

# Convert the x variable to a factor, so that it is treated as discrete
qplot(factor(BOD$Time), BOD$demand, geom="bar", stat="identity")
    
    
====================================
# cyl is continuous here
qplot(mtcars$cyl)

# Treat cyl as discrete
qplot(factor(mtcars$cyl))
    
    
====================================
# Bar graph of values. This uses the BOD data frame, with the 
#"Time" column for x values and the "demand" column for y values.
qplot(Time, demand, data=BOD, geom="bar", stat="identity")
# This is equivalent to:
ggplot(BOD, aes(x=Time, y=demand)) + geom_bar(stat="identity")


# Bar graph of counts
qplot(factor(cyl), data=mtcars)
# This is equivalent to:
ggplot(mtcars, aes(x=factor(cyl))) + geom_bar()
    
    
====================================
hist(mtcars$mpg)

# Specify approximate number of bins with breaks
hist(mtcars$mpg, breaks=10)
    
    
====================================
qplot(mtcars$mpg)
    
    
====================================
library(ggplot2)
qplot(mpg, data=mtcars, binwidth=4)
# This is equivalent to:
ggplot(mtcars, aes(x=mpg)) + geom_histogram(binwidth=4)
    
    
====================================
plot(ToothGrowth$supp, ToothGrowth$len)
    
    
====================================
# Formula syntax
boxplot(len ~ supp, data = ToothGrowth)

# Put interaction of two variables on x-axis
boxplot(len ~ supp + dose, data = ToothGrowth)
    
    
====================================
library(ggplot2)
qplot(ToothGrowth$supp, ToothGrowth$len, geom="boxplot")
    
    
====================================
qplot(supp, len, data=ToothGrowth, geom="boxplot")
# This is equivalent to:
ggplot(ToothGrowth, aes(x=supp, y=len)) + geom_boxplot()
    
    
====================================
# Using three separate vectors
qplot(interaction(ToothGrowth$supp, ToothGrowth$dose), ToothGrowth$len,
      geom="boxplot")

# Alternatively, get the columns from the data frame
qplot(interaction(supp, dose), len, data=ToothGrowth, geom="boxplot")
# This is equivalent to:
ggplot(ToothGrowth, aes(x=interaction(supp, dose), y=len)) + geom_boxplot()
    
    
====================================
curve(x^3 - 5*x, from=-4, to=4)
    
    
====================================
# Plot a user-defined function
myfun <- function(xvar) {
    1/(1 + exp(-xvar + 10))
}
curve(myfun(x), from=0, to=20)
# Add a line:
curve(1-myfun(x), add = TRUE, col = "red")
    
    
====================================
library(ggplot2)
# This sets the x range from 0 to 20
qplot(c(0,20), fun=myfun, stat="function", geom="line")
# This is equivalent to:
ggplot(data.frame(x=c(0, 20)), aes(x=x)) + stat_function(fun=myfun, geom="line")
    
    
==================
 No newline at end of file
Loading