R Examples


In [1]:
options(repr.plot.width = 5)
options(repr.plot.height = 5)

Simple DataFrame and ggplot2

Here is a simple data set that is put into an R Data Frame and then plotted using R's ggplot2 library:


In [2]:
year <- c(2000 ,   2001  ,  2002  ,  2003 ,   2004)
rate <- c(9.34 ,   8.50  ,  7.62  ,  6.93  ,  6.60)
df = data.frame(year, rate)

In [3]:
head(df)


Out[3]:
yearrate
120009.34
220018.5
320027.62
420036.93
520046.6

In [4]:
library(ggplot2)

In [5]:
ggplot(df, aes(x=year, y=rate)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm)   # Add linear regression line, with 95% confidence region


Iris data set


In [6]:
head(iris)


Out[6]:
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
15.13.51.40.2setosa
24.931.40.2setosa
34.73.21.30.2setosa
44.63.11.50.2setosa
553.61.40.2setosa
65.43.91.70.4setosa

In [7]:
pairs(iris[1:4], main = "Iris Data", pch = 21,
      bg = c("red", "green3", "blue")[unclass(iris$Species)])