## R Graphics¶

### Base Graphics¶

In [1]:
x <- seq(0, 2*pi, length.out = 20)
y <- sin(x)
plot(x, y, type="l", col="darkblue",
main="Sine Curve", sub="Copyright (c) 2016 HwB")
points(x, y, col="blue", pch=20)
lines(c(0,2*pi), c(-1,1), lty=2)
grid()
abline(h=0); abline(v=0)
In [2]:
?plot
Out[2]:
 plot {graphics} R Documentation

## Generic X-Y Plotting

### Description

Generic function for plotting of R objects. For more details about the graphical parameter arguments, see par.

For simple scatter plots, plot.default will be used. However, there are plot methods for many R objects, including functions, data.frames, density objects, etc. Use methods(plot) and the documentation for these.

plot(x, y, ...)

### Arguments

 x the coordinates of points in the plot. Alternatively, a single plotting structure, function or any R object with a plot method can be provided. y the y coordinates of points in the plot, optional if x is an appropriate structure. ... Arguments to be passed to methods, such as graphical parameters (see par). Many methods will accept the following arguments: typewhat type of plot should be drawn. Possible types are "p" for points, "l" for lines, "b" for both, "c" for the lines part alone of "b", "o" for both ‘overplotted’, "h" for ‘histogram’ like (or ‘high-density’) vertical lines, "s" for stair steps, "S" for other steps, see ‘Details’ below, "n" for no plotting. All other types give a warning or an error; using, e.g., type = "punkte" being equivalent to type = "p" for S compatibility. Note that some methods, e.g. plot.factor, do not accept this. mainan overall title for the plot: see title. suba sub title for the plot: see title. xlaba title for the x axis: see title. ylaba title for the y axis: see title. aspthe y/x aspect ratio, see plot.window.

### Details

The two step types differ in their x-y preference: Going from (x1,y1) to (x2,y2) with x1 < x2, type = "s" moves first horizontal, then vertical, whereas type = "S" moves the other way around.

plot.default, plot.formula and other methods; points, lines, par. For thousands of points, consider using smoothScatter() instead of plot().

For X-Y-Z plotting see contour, persp and image.

### Examples

require(stats) # for lowess, rpois, rnorm
plot(cars)
lines(lowess(cars))

plot(sin, -pi, 2*pi) # see ?plot.function

## Discrete Distribution Plot:
plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,
main = "rpois(100, lambda = 5)")

## Simple quantiles/ECDF, see ecdf() {library(stats)} for a better one:
plot(x <- sort(rnorm(47)), type = "s", main = "plot(x, type = \"s\")")
points(x, cex = .5, col = "dark red")

[Package graphics version 3.2.3 ]

### Statistical Graphs¶

In [3]:
Out[3]:
Field.NameAreaSlopeVegetationSoil.pHDampWorm.density
1Nashs.Field3.611Grassland4.1FALSE4
2Silwood.Bottom5.12Arable5.2FALSE7
3Nursery.Field2.83Grassland4.3FALSE2
5Gunness.Thicket3.80Scrub4.2FALSE6
In [4]:
plot(worms)  # scatterplot matrix
In [5]:
# attach(worms)
with(worms, plot(Area, Slope))
grid()
In [6]:
hist(worms$Soil.pH, breaks=10) lines(density(worms$Soil.pH), col="red")
In [7]:
plot(worms$Vegetation, worms$Worm.density)

### ggplot2 -- Grammar of Graphics¶

In [8]:
library(ggplot2)
In [9]:
qplot(worms$Area, worms$Slope)
In [10]:
str(mtcars)
'data.frame':	32 obs. of  11 variables:
$mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...$ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
$disp: num 160 160 108 258 360 ...$ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
$drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...$ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
$qsec: num 16.5 17 18.6 19.4 17 ...$ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
$am : num 1 1 1 0 0 0 0 0 0 0 ...$ gear: num  4 4 4 3 3 3 3 4 4 4 ...
\$ carb: num  4 4 1 1 2 1 4 2 2 4 ...
In [11]:
plt <- ggplot(mtcars)
In [12]:
plt + geom_point(aes(x=disp, y=mpg, shape=factor(gear), color=cyl)) +
theme_bw()
In [13]: