Section 35 Additional Features

35.1 Grouping points

str(iris)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
my.pch <- 16:18
my.col <- c('orange','blue','green3')

plot(Sepal.Length ~ Petal.Length, data = iris, 
     type = 'p', xlab = 'Petal.Length', ylab = 'Sepal.Length',
     pch = my.pch[iris$Species], 
     col = my.col[iris$Species])

35.2 Adding a legend

# Simple text

mtext <- 'Plot of Sepal & Petal length for all Species'

text(x = 1, y = 7.5, pos = 4,
     labels = mtext,
     col = 'red', cex = 1)

# Legend

my.text <- levels(iris$Species)

legend(x = 'bottomright', legend = my.text)


# Legend: Redo

plot(Sepal.Length ~ Petal.Length, data = iris, 
     type = 'p', xlab = 'Petal.Length', ylab = 'Sepal.Length',
     pch = my.pch[iris$Species], 
     col = my.col[iris$Species])

legend(x = 'bottomright', legend = my.text, pch = my.pch, col = my.col)

35.3 Using locator to position

plot(Sepal.Length ~ Petal.Length, data = iris, 
     type = 'p', xlab = 'Petal.Length', ylab = 'Sepal.Length',
     pch = my.pch[iris$Species], 
     col = my.col[iris$Species])

legend(x = locator(1), legend = my.text, pch = my.pch, col = my.col)

legend(x = locator(1), legend = my.text, pch = my.pch, col = my.col, bty='n')

35.4 Using identify to locate

identify(iris$Sepal.Length ~ iris$Petal.Length)

identify(x = iris$Petal.Length, y = iris$Sepal.Length)

35.5 Add text with locator

text(x = locator(1), labels = expression(R^2 == 0.85))

35.6 Other font family

  • Fonts:
    • Common font families are generally available within the R environment
    • The package extrafont to load additional fonts: loadfonts(device=“postscript”)
par(family='sans')
text(x = locator(1), labels = 'This is the Arial font')

par(family='serif')
text(x = locator(1), labels = 'This is the Times New Roman font')

par(family='mono')
text(x = locator(1), labels = 'This is the Courier font')

par(family='sans')