Nathalie Villa-Vialaneix - http://www.nathalievialaneix.eu
September 14-16th, 2015
Master TIDE, Université Paris 1
par
)plot
,
boxplot
, pie
…) that initiates the graphic;legend
, points
, lines
…) to add elements to the
graphic.Eventually, the graphic can be exported in a file (png, postscript, pdf, …) after.
data(airquality)
par(bg="lightyellow", mfcol=c(1,2), pch="+")
plot(airquality$Ozone)
plot(factor(airquality$Month))
Important graphics parameters include:
pch
: the plotting symbol (default is an open circle)lty
: the line type (default is a solide line)lwd
: the line width, specified as an integer multiplecol
: the plotting color (default is black)las
: the orientation of the axis label on the plot (default is 0, which
corresponds to labels parallel to the axes, 1 is for horizontal labels, 2 for
labels perpendicular to the axes and 3 to vertical labels)bg
: the background color (default: white)Important graphics parameters include:
mar
: the margin size (default is c(5,4,4,2)
, bottom, left, top,
right)oma
: the outer margin size (default is 0)mfrow
: number of plots per row, column (plots are filled row-wise)mfcol
: number of plots per row, column (plots are filled col-wise)By default graphics are shown on screen but this can be changed using a specific
command like png
, svg
, pdf
… Available graphics devices can
be found using ?Device
.
png("myFistGraphic.png", width=600,
height=600)
par(bg="transparent", lty=2, lwd=2,
col="darkred")
plot(airquality$Wind, airquality$Ozone)
dev.off() # close the graphics device
A graphics made on the screen can be exported in a graphic device (but this is not an exact operation)
par(bg="transparent", lty=2, lwd=2,
mar=rep(2,4), col="darkred")
plot(airquality$Wind, airquality$Ozone)
dev.print(png, file="mySecondGraphic.png",
width=600, height=600)
png
2
This function displays the value of the variable versus its index, as a
scatterplot (default) or a line (type="l"
):
plot(airquality$Ozone, type="l")
… meaningful only for e.g., time series.
The standard graphics paramaters can be passed to the function (see also
argument type
in help):
plot(airquality$Ozone, main="Ozone",
type="b", pch=19, col="darkred")
This function displays the barplot of the distribution of the variable:
plot(factor(airquality$Month), main="Month",
col="pink", lwd=2, las=3,
names=c("May","June","July","Aug.","Sept"))
This function displays the value of the first variable versus the value of the second variable.
par(mfrow=c(1,2))
plot(airquality$Wind, airquality$Ozone)
plot(airquality$Ozone~airquality$Wind)
This function has different behaviors depending on its syntax:
par(mfrow=c(1,2))
plot(airquality[,3], factor(airquality[,5]))
plot(factor(airquality[,5]), airquality[,3])
par(mfrow=c(1,2))
plot(airquality[,3]~factor(airquality[,5]))
plot(factor(airquality[,5])~airquality[,3],
col=rainbow(nlevels(factor(airquality[,5]))))
This function displays a barplot of the cross distribution (alternative syntax with ~ also usable)
plot(factor(airquality[,5]),
factor(airquality[,1]>30), col=c(3,2),
xlab="Month", ylab="Bad quality")
boxplot(airquality$Wind, ylab="Wind speed",
main="Wind speed distribution")
boxplot(airquality$Wind~
factor(airquality$Month),
ylab="Wind speed",
names=c("May","June","July","Aug.","Sept."))
Breakpoints are given as a number or as a vector; areas are proportional to the counts.
par(mfrow=c(1,2))
hist(airquality$Ozone, breaks=20)
hist(airquality$Ozone, col="pink",
breaks=c(0,10,20,40,60,80,100,170))
barplot
is used on the results of the function table
:
barplot(table(airquality$Month),
ylab="Frenquency", col="lightblue")
barplot(table(airquality$Ozone>50,
airquality$Month),
ylab="Frenquency",
col=c("lightgreen", "pink"))
CT <- table(airquality$Ozone>50,
airquality$Month)
barplot(sweep(CT, 2, colSums(CT), "/"),
ylab="Frenquency",
col=c("lightgreen", "pink"))
pie
is used on the results of the function table
:
pie(table(airquality$Month), col=rainbow(5),
main="Months",
labels=paste(round(table(airquality$Month)/
nrow(airquality),2),"%"))
plot(airquality$Ozone, type="h")
abline(h=50, col="darkred", lty=2, lwd=2)
abline(v=50, col="darkblue", lwd=2)
plot(airquality$Ozone~airquality$Wind)
abline(0, 1, col="darkgreen")
plot(airquality$Wind[1:100], col="darkred",
type="l")
lines(25:78, airquality$Wind[100:153],
col="darkgreen")
plot(airquality$Ozone~airquality$Wind)
segments(airquality[1:5,3],
airquality[1:5,1],
airquality[141:145,3],
airquality[141:145,1], col="darkred")
plot(airquality$Ozone~airquality$Wind)
arrows(airquality[1:5,3],
airquality[1:5,1],
airquality[141:145,3],
airquality[141:145,1],
col="darkred")
plot(airquality$Ozone, type="l")
points(airquality$Wind, pch=19)
plot(airquality$Temp~airquality$Wind,
pch=19)
points(airquality$Wind, airquality$Ozone,
col="darkred", pch=19)
plot(airquality$Temp~airquality$Wind,
pch=19,
ylim=range(airquality$Ozone,
na.rm=TRUE))
points(airquality$Wind, airquality$Ozone,
col="darkred", pch=19)
plot(airquality$Temp, type="l")
text(90, 70, "It's much\n too hot",
cex=3, col="darkred")
text(0, 90, "Too cold...", col="blue", cex=3,
pos=4) # pos=4 <=> "to the right of"
barplot(table(factor(airquality$Ozone>50),
factor(airquality$Month)),
col=c(3,2))
legend(1.5, 20, cex=2, pch=15,
legend=c("good","bad"), col=c(3,2))
par(mfrow=c(1,2))
barplot(table(factor(airquality$Ozone>50),
factor(airquality$Month)),
col=c(3,2))
plot.new()
legend("center", cex=4, pch=15,
legend=c("good","bad"), col=c(3,2))
data(CO2)
How to make the following graphics?