r (3) introduction to graphics. the main guide r in action data analysis and graphics with r robert...

15
R (3) Introduction to Graphics

Upload: erin-hart

Post on 17-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

R (3)

Introduction to Graphics

Page 2: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

The main guide

R in ActionData Analysis and Graphics with RRobert I. Kabacoff

http://www.manning.com/affiliate/idevaffiliate.php?id=1102_173

Page 3: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Simple plot• Evolution of daily sales in August 2011 (data imported from Pg)> plot(daily_sales_2011_8$day,+ daily_sales_2011_8$sales, type="b")

Page 4: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Parameters for symbols and lines

Page 5: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Plot with symbol and line• Evolution of daily sales in August 2011 (data imported from Pg)> plot(daily_sales_2011_8$day,+ daily_sales_2011_8$sales, + type="b", + lty=2, + pch=17)

Page 6: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Plot with symbol,line and different symbol size• Evolution of daily sales in August 2011 (data imported from Pg)> plot(daily_sales_2011_8$day,+ daily_sales_2011_8$sales, + type="b", + lty=2, + pch=17,+ cex = .5)

Page 7: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Parameters for color

Page 8: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Plot with colour> plot(daily_sales_2011_8$day,+ daily_sales_2011_8$sales, + type="b", + lty=2, + pch=17,+ cex = .5,+ col="brown", + col.axis=+ "green", + col.lab=+ "red", + fg="blue", + bg="yellow")

Page 9: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Parameters for text s& dimensions/margins

Page 10: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Parameters for text & dimensions/margins

Page 11: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Change axes limits

> plot(daily_sales_2011_8$day,daily_sales_2011_8$sales/+ 1000, xlim=c(min(daily_sales_2011_8$day), + max(daily_sales_2011_8$day)), ylim=c(0, + max(daily_sales_2011_8$sales/1000)+1000),+ type="b", + cex = .5,+ col="red", + lty=2, + pch=2, + lwd=2)

Page 12: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Titles (1)

• Types of titles– Main title of the graphic (main)– Sub-title of the graphic (sub)– Title of the axis x and y (xlab and ylab)

> title(main="main title", + sub="sub-title",+ xlab="x-axis label", + ylab="y-axis label")

Page 13: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Titles (2)> plot(dday, sales/1000, main= "Daily Sales in + August 2011", sub="Data imported from + PostgreSQL", xlab="Day (of the month)",+ ylab="Sales (thousands)",+ type="b", + cex = .5,+ col="red",+ lty=2, + pch=2, + lwd=2)

Page 14: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Legend (1)> attach(daily_sales_2011_8)> plot(day, sales/1000, main="Daily Sales - Aug. vs. Sept. 2011", sub="Data imported from PostgreSQL", xlab="Day (of the month)", ylab="Sales(thousands)", type="b", cex = 1.25, col="red", lty=1, pch=2, lwd=1.5)> detach(daily_sales_2011_8)> attach(daily_sales_2011_9)> # to display on the same graphic, use lines, not plot > lines(day, sales/1000, col="black", lty=2, + pch=17, lwd=2)# now the legendlegend("bottomright", inset=.05, title="Month", + c("Aug","Sept"),+ lty=c(1, 2), pch=c(2, 17), col=c("red", "black"))

Page 15: R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff

Legend (2)