Life expectancy.
The concept of a graphical device is crucial for understanding R graphics.
The concept of a graphical device is crucial for understanding R graphics.
A device can be a screen (default) or a file.
The concept of a graphical device is crucial for understanding R graphics.
A device can be a screen (default) or a file.
Creating a plot entails:
The concept of a graphical device is crucial for understanding R graphics.
A device can be a screen (default) or a file.
Creating a plot entails:
The concept of a graphical device is crucial for understanding R graphics.
A device can be a screen (default) or a file.
Creating a plot entails:
The concept of a graphical device is crucial for understanding R graphics.
A device can be a screen (default) or a file.
Creating a plot entails:
Device.Type | Functions | Notes |
---|---|---|
Screen | default device | Displays plot on screen |
Bitmap/Raster | png(), bmp(), tiff(), jpeg() | Use for images with pixel data |
Vector | svg(), pdf() | High-quality, scalable graphics |
Cairo | cairo_pdf(), cairo_ps() | Enhanced quality, especially on Windows |
For more information visit this link.
png(filename = 'myplot.png', width = 320, height = 320, antialias = T)# anti-aliasing, a technique that smooths the edges of shapes and text in the plot, making them appear less pixelated or jagged.plot(x=c(1,2,7), y=c(2,3,5))dev.off()
png(filename = 'myplot.png', width = 320, height = 320, antialias = T)# anti-aliasing, a technique that smooths the edges of shapes and text in the plot, making them appear less pixelated or jagged.plot(x=c(1,2,7), y=c(2,3,5))dev.off()
What we did was, in sequence:
png()
with some parameters,plot()
anddev.off()
.png(filename = 'myplot.png', width = 320, height = 320, antialias = T)# anti-aliasing, a technique that smooths the edges of shapes and text in the plot, making them appear less pixelated or jagged.plot(x=c(1,2,7), y=c(2,3,5))dev.off()
What we did was, in sequence:
png()
with some parameters,plot()
anddev.off()
.It is of paramount importance to remember to close graphical devices. Otherwise, our plots may end up in some random weird files or on screens.
source: rgraphics.limnology.wisc.edu
source: rgraphics.limnology.wisc.edu
c(bottom, left, top, righ)
base::plot()
basicsbase::plot()
is a basic command that lets you visualize your data and results. It is very powerful yet takes some effort to fully master it. Let's begin with plotting three points:
plot(x=c(1,2,7), y=c(2,3,5))
For convenience, we will create a data frame with our points:
## x y## A 1 2## B 2 3## C 7 5
plot(df)
There is many parameters one can set in plot()
. Let's have a closer look at some of them:
Let's make our plot a bit fancier...
# recycling rule in action!plot(df, pch = c(15, 17, 19), col = c("tomato", "slateblue"), main = "Three points", sub = "Stage 1")
Graphical parameters can be set in two different ways:
plot(dat, cex = 0.5)
par()
to set parameters globally# read current graphical parameterspar()# first, save the current parameters so that you# can set them back if neededold_par <- par() # should work in theory, practise varies :-(# now, modify what you wantpar(cex.axis = 0.8, bg='grey')# do your plottingplot(................)# restore the old parameters if you wantpar(old_par)
pch
parameterpch
cheatsheet# create a grid of coordinatescoords <- expand.grid(1:6,1:6)# make a vector of numerical pch symbolspch.num <- c(0:25)# and a vector of character pch symbolspch.symb <- c('*', '.', 'o', 'O', '0', '-', '+', '|', '%', '#')# plot numerical pchplot(coords[1:26,1], coords[1:26,2], pch=pch.num,bty='n', xaxt='n', yaxt='n', bg='red',xlab='', ylab='')# and character pch'spoints(coords[27:36,1], coords[27:36,2], pch=pch.symb)# label themtext(coords[,1], coords[,2], c(1:26, pch.symb), pos = 1,col='slateblue', cex=.8)
Now, make your own cheatsheet for the lty parameter!
Elements are added to a plot in the same order you plot them. It is like layers in a graphical program. Think about it! For instance the auxiliary grid lines should be plotted before the actual data points.
# make an empty plotplot(1:5, type = "n", las = 1, bty = "n")grid(col = "grey", lty = 3)points(1:5, pch = 19, cex = 3)abline(h = 3, col = "red")
points()
There is a few points you should have in mind when working with plots:
There is a few points you should have in mind when working with plots:
There is a few points you should have in mind when working with plots:
There is a few points you should have in mind when working with plots:
There is a few points you should have in mind when working with plots:
There is a few points you should have in mind when working with plots:
par(mfrow=c(2,3))plot(1:5)plot(1:5, pch=19, col='red')plot(1:5, pch=15, col='blue')hist(rnorm(100, mean = 0, sd=1))hist(rnorm(100, mean = 7, sd=3))hist(rnorm(100, mean = 1, sd=0.5))par(mfrow=c(1,1))
Alternative: use par(mfcol=c(3,2))
.
par(mfrow=c(2,3))plot(1:5)plot(1:5, pch=19, col='red')plot(1:5, pch=15, col='blue')hist(rnorm(100, mean = 0, sd=1))hist(rnorm(100, mean = 7, sd=3))hist(rnorm(100, mean = 1, sd=0.5))par(mfrow=c(1,1))
Alternative: use par(mfcol=c(3,2))
.
graphics::layout()
M <- matrix(c(1,2,2,2,3,2,2,2), nrow = 2, ncol = 4, byrow = T)Mlayout(mat = M)layout.show(n = 3) #It shows the layout setup without plots
## [,1] [,2] [,3] [,4]## [1,] 1 2 2 2## [2,] 3 2 2 2
graphics::layout()
M <- matrix(c(1,2,2,2,3,2,2,2), nrow = 2, ncol = 4, byrow = T)Mlayout(mat = M)layout.show(n = 3) #It shows the layout setup without plots
## [,1] [,2] [,3] [,4]## [1,] 1 2 2 2## [2,] 3 2 2 2
M <- matrix(c(1,2,2,2,3,2,2,2), nrow = 2, ncol = 4, byrow = T)layout(mat = M)plot(1:5, pch=15, col='blue')hist(rnorm(100, mean = 0, sd=1))plot(1:5, pch=15, col='red')
mycol <- c(rgb(0, 0, 1), "olivedrab", "#FF0000")plot(1:3, c(1, 1, 1), col = mycol, pch = 19, cex = 3)points(2, 1, col = rgb(0, 0, 1, 0.5), pch = 15, cex = 5)
mycol <- c(rgb(0, 0, 1), "olivedrab", "#FF0000")plot(1:3, c(1, 1, 1), col = mycol, pch = 19, cex = 3)points(2, 1, col = rgb(0, 0, 1, 0.5), pch = 15, cex = 5)
There some built-in palettes: default, hsv, gray, rainbow, terrain.colors, topo.colors, cm.colors, heat.colors
mypal <- heat.colors(10)mypalpie(x = rep(1, times = 10), col = mypal)
## [1] "#FF0000" "#FF2400" "#FF4900" "#FF6D00" "#FF9200" "#FFB600" "#FFDB00"## [8] "#FFFF00" "#FFFF40" "#FFFFBF"
You can easily create custom palettes:
mypal <- colorRampPalette(c("red", "green", "blue"))pie(x = rep(1, times = 12), col = mypal(12))class(mypal)
## [1] "function"
You can easily create custom palettes:
mypal <- colorRampPalette(c("red", "green", "blue"))pie(x = rep(1, times = 12), col = mypal(12))class(mypal)
## [1] "function"
Note:
grDevices::colorRampPalette()
returns a function for generating colors based on the defined custom palette!There is an excellent package
RColorBrewer
that offers a number of pre-made palettes, e.g. color-blind safe palette.Package
wesanderson
offers palettes based on Wes Anderson's movies :-)
There are also more specialized R functions used for creating specific types of plots. One commonly used is graphics::boxplot()
Rule of thumb. If median of one boxplot is outside the box of another, the median difference is likely to be significant α=5%.
boxplot(decrease ~ treatment,data = OrchardSprays,log = "y", col = "bisque",varwidth=TRUE)
boxplot(decrease ~ treatment,data = OrchardSprays,log = "y", col = "bisque",varwidth=TRUE)
The add=TRUE
parameter in plots allows you to compose plots using previously plotted stuff!
attach(InsectSprays)boxplot(count~spray, data=InsectSprays[spray %in% c("C","F"),], col="lightgray")boxplot(count~spray, data=InsectSprays[spray %in% c("A","B"),], col="tomato", add=TRUE)boxplot(count~spray, data=InsectSprays[spray %in% c("D","E"),], col="slateblue", add=TRUE)detach(InsectSprays)
data(Titanic) # Load the datavcd::mosaic(Titanic, labeling = vcd::labeling_border(rot_labels = c(0,0,0,0)))
Package
vcd
provides a lot of ways of visualizing categorical data.
heatmap(matrix(rnorm(100, mean = 0, sd = 1), nrow = 10),col=terrain.colors(10))
Life expectancy.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |