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.

Usage

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:

type

what 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.

main

an overall title for the plot: see title.

sub

a sub title for the plot: see title.

xlab

a title for the x axis: see title.

ylab

a title for the y axis: see title.

asp

the 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.

See Also

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]:
worms <- read.table("../data/worms.txt", header=TRUE)  # loads dataframe 'mtcars'
head(worms)
Out[3]:
Field.NameAreaSlopeVegetationSoil.pHDampWorm.density
1Nashs.Field3.611Grassland4.1FALSE4
2Silwood.Bottom5.12Arable5.2FALSE7
3Nursery.Field2.83Grassland4.3FALSE2
4Rush.Meadow2.45Meadow4.9TRUE5
5Gunness.Thicket3.80Scrub4.2FALSE6
6Oak.Mead3.12Grassland3.9FALSE2
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]:
data(mtcars)  # loads dataframe 'mtcars'
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]:
ozdata = read.table("../data/ozone.csv", header=TRUE, sep=",")
with(ozdata,
     coplot(Ozone ~ Wind|Temp, panel=panel.smooth)
)
 Missing rows: 5, 10, 25, 26, 27, 32, 33, 34, 35, 36, 37, 39, 42, 43, 45, 46, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 65, 72, 75, 83, 84, 102, 103, 107, 115, 119, 150 

lattice -- Trellis Graphics

In [14]:
library(lattice)
    xyplot(mpg ~ disp | factor(gear), data=mtcars, layout=c(3,1))
In [15]:
bwplot(Worm.density ~ Soil.pH + Area | Vegetation, data=worms)