Search

# Coordinates and annotations in R. 20th May 2016 This demonstration covers knowing how to place extra things on graphs at particular locations.

You can watch the recording (5 minutes 4 seconds):

Coordinates in R: cord.mp4 (.mp4 format, 65.2 MB)

This demonstration uses base plotting, which is the model of making the initial graph then adding to it with other commands.

The script I used

```#numbers from CBS poll 14/4/16
results_csv <- "
Clinton, Trump
11,88
88,8
51,33
"

row.names(results) <- c("Rep","Dem","Ind")
results <- t(as.matrix(results))

### 1st Graph
barplot(results, beside=TRUE)

### Graph return value
print(barplot(results, beside=TRUE))

### Graph test lines
barplot(results, beside=TRUE)
abline(v=1, col="red", lwd=2)
abline(v=6, col="blue", lwd=2)
abline(v=20, col="purple", lwd=2)

### Fancy up the graph
barplot(results, main="Candidate support by party affiliation",
xlab="Candidate", col=c("blue","red"),
sub="source: CBS poll 14th April 2016",
legend.text = rownames(results), beside=TRUE,
args.legend=list(x = "right", bty="n", y.intersp=1.3))

### Extend the vertical access to give a working area for annotations
barplot(results, main="Candidate support by party affiliation",
xlab="Candidate", col=c("blue","red"),
sub="source: CBS poll 14th April 2016",
legend.text = rownames(results), beside=TRUE,
args.legend=list(x = "right", bty="n", y.intersp=1.3),
ylim=c(0,140))

### Make a better looking custom y axis
barplot(results, main="Candidate support by party affiliation",
xlab="Candidate", col=c("blue","red"),
sub="source: CBS poll 14th April 2016",
legend.text = rownames(results), beside=TRUE,
args.legend=list(x = "right", bty="n", y.intersp=1.3),
ylim=c(0,140), axes=FALSE)
axis(side=2, at=c(0,20,40,60,80,100))

### Add a line with lines(), listing all the points the line starts, ends or turns
barplot(results, main="Candidate support by party affiliation",
xlab="Candidate", col=c("blue","red"),
sub="source: CBS poll 14th April 2016",
legend.text = rownames(results), beside=TRUE,
args.legend=list(x = "right", bty="n", y.intersp=1.3),
ylim=c(0,140), axes=FALSE)
axis(side=2, at=c(0,20,40,60,80,100))
lines(x=c(2,2,5,5), y=c(115,120,120,115), lty=3, col="purple")

### Add a text with text()
barplot(results, main="Candidate support by party affiliation",
xlab="Candidate", col=c("blue","red"),
sub="source: CBS poll 14th April 2016",
legend.text = rownames(results), beside=TRUE,
args.legend=list(x = "right", bty="n", y.intersp=1.3),
ylim=c(0,140), axes=FALSE)
axis(side=2, at=c(0,20,40,60,80,100))
lines(x=c(2,2,5,5), y=c(115,120,120,115), lty=3, col="purple")
text(x=3.5, y=125, labels="***", col="purple")

### Add more lines and text
barplot(results, main="Candidate support by party affiliation",
xlab="Candidate", col=c("blue","red"),
sub="source: CBS poll 14th April 2016",
legend.text = rownames(results), beside=TRUE,
args.legend=list(x = "right", bty="n", y.intersp=1.3),
ylim=c(0,140), axes=FALSE)
axis(side=2, at=c(0,20,40,60,80,100))
lines(x=c(2,2,5,5), y=c(115,120,120,115), lty=3, col="purple")
text(x=3.5, y=125, labels="***", col="purple")
lines(x=c(1.5,1.5,2.5,2.5), y=c(95,100,100,95), lty=4, col="black")
text(x=2, y=105, labels="***", col="black")
lines(x=c(4.5,4.5,5.5,5.5), y=c(95,100,100,95), lty=4, col="black")
text(x=5, y=105, labels="***", col="black")
```