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

Post on 17-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

R (3)

Introduction to Graphics

The main guide

R in ActionData Analysis and Graphics with RRobert I. Kabacoff

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

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")

Parameters for symbols and lines

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)

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)

Parameters for color

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")

Parameters for text s& dimensions/margins

Parameters for text & dimensions/margins

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)

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")

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)

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"))

Legend (2)

top related