By default, there are 26 symbols that can be used for the data points of a scatter plot. These are set using the pch
keyword argument and are numbered from 0 to 25, for example:
plot(x, y, pch = 0)
will plot with squares while
plot(x, y, pch = 1)
will plot with circles, and so on.
num_symbols <- 26
nrows <- 13
ncols <- 2
# Set the margins of the plotting area
par(mar = c(0, 0, 0, 0))
# Plot
x <- c()
y <- c()
for (i in 0:25) {
x <- c(x, ceiling((i + 1) / nrows))
y <- c(y, nrows - (i %% nrows))
}
plot(x, y, pch = 0:25, axes = FALSE, xlim = c(0.75, ncols + 0.75))
name <- c(
"square", "circle", "triangle point up", "plus", "cross", "diamond",
"triangle point down", "square cross", "star", "diamond plus",
"circle plus", "triangles up and down", "square plus", "circle cross",
"square and triangle down", "filled square", "filled circle",
"filled triangle point-up", "filled diamond", "solid circle",
"bullet (smaller circle)", "filled circle blue", "filled square blue",
"filled diamond blue", "filled triangle point-up",
"filled triangle point dwn"
)
text(x + 0.1, y, name, adj = 0)
text(x - 0.1, y, 0:25)
The style of line that is plotted can be controlled with the lty
keyword argument (line type), for example:
plot(x, y, lty = "dashed", type = "l")
will plot a dashed line. Note that the type
keyword argument needs to be set to l
for a line to be plotted (only the points will be plotted by default - ie a scatter plot instead of a line plot). The line types that can be used are as follows:
library(ggpubr)
show_line_types()
There are 657 colours that can be referred to by name when choosing a colour for a plot. These are set using the col
keyword argument, for example:
plot(x, y, col = "red")
will plot in red. The complete list is below:
num_colours <- 657
nrows <- 110
ncols <- 6
# Set the margins of the plotting area
par(mar = c(0, 0, 0, 0))
# Plot
plot(
seq(1.5, 3.5, by = 2), seq(1.5, 3.5, by = 2),
xlim = c(0, ncols * 2), ylim = c(0, nrows),
xlab = NULL, ylab = NULL, xaxt = "n", yaxt = "n", axes = FALSE, ann = FALSE
)
xleft <- rep(seq(1, ncols * 2 - 1, by = 2), nrows)[1:num_colours]
ybottom <- c()
for (i in nrows:1 - 1) {
ybottom <- c(ybottom, rep(i, ncols))
}
ybottom <- ybottom[1:num_colours]
xright <- rep(seq(1.5, ncols * 2 - 0.5, by = 2), nrows)[1:num_colours]
ytop <- c()
for (i in nrows:1) {
ytop <- c(ytop, rep(i, ncols))
}
ytop <- ytop[1:num_colours]
rect(xleft, ybottom, xright, ytop, col = colors()[1:num_colours])
xtext <- rep(seq(0.25, ncols * 2 - 1.75, by = 2), nrows)[1:num_colours]
ytext <- c()
for (i in nrows:1 - 0.5) {
ytext <- c(ytext, rep(i, ncols))
}
ytext <- ytext[1:num_colours]
text(xtext, ytext, colors()[1:num_colours], cex = 1.2, font = 2)
The easiest way to edit the transparency of an element is to use the rgb()
function when setting its colour. Instead of col = "colour_name"
as is being demonstrated above, this would involve using col = rgb()
and specifying the amounts of red, green and blue (on scales of 0 to 1) in the colour you want. The advantage of this method is that rgb()
also has an alpha
keyword argument which, when given a number between 0 and 1, sets the transparency of the colour:
height <- c(1, 1, 1, 1, 1, 1)
alpha <- c(0.0, 0.2, 0.4, 0.6, 0.8, 1.0)
barplot(height, col = rgb(red = 1, green = 0, blue = 0, alpha = alpha), yaxt = "n")
This is a parameter of the plot and so it needs to be set with the par()
function. Specifically, use the cex
keyword argument as you are changing the character expansion relative to the default (which is 1):
par(cex = 1)
x <- c(1, 2, 3, 4, 5)
y <- c(1, 1, 1, 1, 1)
plot(x, y, ylim = c(1, 3))
par(cex = 2)
x <- c(1, 2, 3, 4, 5)
y <- c(2, 2, 2, 2, 2)
points(x, y)
par(cex = 3)
x <- c(1, 2, 3, 4, 5)
y <- c(3, 3, 3, 3, 3)
points(x, y)