User Tools

Site Tools


recol:plotting_rscript
plotting_figures.r
# Code used for creating the REcol page "plotting figures"
 
class (cars)  # data.frame - the function class checks the type of the object (vector, matrix, data.frame, list)
names (cars)  # "speed" "dist" - names of the variables (columns) in the data frame cars
dim (cars)    # 40 2 - it has 40 rows (samples) and 2 columns (variables)
cars$speed    # returns vector with values of the first column (variable speed)
cars$dist     # returns vector with values of the second column (variable dist)
 
setwd ('c:\\Users\\Zeleny\\Dropbox\\uceni\\Recol\\materials\\')
 
png (file = 'plotting_1.png', height = 550, width = 550, units = 'px', pointsize = 16)
plot (x = cars$speed, y = cars$dist)
dev.off ()
 
png (file = 'plotting_2.png', height = 550, width = 550, units = 'px', pointsize = 16)
plot (dist ~ speed, data = cars)
dev.off ()
 
plot (dist ~ speed)
 
png (file = 'plotting_3.png', height = 550, width = 550, units = 'px', pointsize = 16)
plot (dist ~ speed, data = cars,
      xlab = 'Speed [mph]', ylab = 'Distance [ft]',  # 1
      pch = 21,                                      # 2
      col = 'tomato', bg = 'yellow',                 # 3
      xlim = c(0, 25),                               # 4
      main = 'Scatterplot')                          # 5
dev.off ()
 
 
plot (dist ~ speed, data = cars,                     # a) data to be used,
      xlim = c(0, 25),                               #    define range of the x-axis,
      axes = FALSE, ann = FALSE, type = 'n')         #    make sure nothing will be actually plotted 
points (dist ~ speed, data = cars,                   # b) add data points,
        pch = 21, col = 'tomato', bg = 'yellow')     #    with given symbols and colours
title (xlab = 'Speed [mph]', ylab = 'Distance [ft]', # c) add x- and y-labels,
       main = 'Scatterplot')                         #    add main title to the plot
box ()                                               # d) add box around the plot
axis (1)                                             # e) add axis 1 (bottom horizontal)
axis (2)                                             # f) add axis 2 (left vertical)
 
# plot sequentially
png (file = 'plotting_4.png', height = 633, width = 950, units = 'px', pointsize = 16)
par (mfrow = c(2,3))
# a)
plot (dist ~ speed, data = cars,                     # data to be used
      xlim = c(0, 25),                               # define range of the x-axis
      axes = FALSE, ann = FALSE, type = 'n')  
par (xpd = T); legend ('topleft', legend = 'a)', bty = 'n', inset = -.3, cex = 2); par (xpd = F)
# b) 
plot (dist ~ speed, data = cars,                     # data to be used
      xlim = c(0, 25),                               # define range of the x-axis
      axes = FALSE, ann = FALSE, type = 'n')  
points (dist ~ speed, data = cars,
        pch = 21, col = 'tomato', bg = 'yellow')     # add data points with given symbols and colours
par (xpd = T); legend ('topleft', legend = 'b)', bty = 'n', inset = -.3, cex = 2); par (xpd = F)
 
# c) 
plot (dist ~ speed, data = cars,                     # data to be used
      xlim = c(0, 25),                               # define range of the x-axis
      axes = FALSE, ann = FALSE, type = 'n')  
points (dist ~ speed, data = cars,
        pch = 21, col = 'tomato', bg = 'yellow')     # add data points with given symbols and colours
title (xlab = 'Speed [mph]', ylab = 'Distance [ft]', # add x- and y-labels
       main = 'Scatterplot')                         # add main title to the plot
par (xpd = T); legend ('topleft', legend = 'c)', bty = 'n', inset = -.3, cex = 2); par (xpd = F)
 
# d)
plot (dist ~ speed, data = cars,                     # data to be used
      xlim = c(0, 25),                               # define range of the x-axis
      axes = FALSE, ann = FALSE, type = 'n')  
points (dist ~ speed, data = cars,
        pch = 21, col = 'tomato', bg = 'yellow')     # add data points with given symbols and colours
title (xlab = 'Speed [mph]', ylab = 'Distance [ft]', # add x- and y-labels
       main = 'Scatterplot')                         # add main title to the plot
box ()   
par (xpd = T); legend ('topleft', legend = 'd)', bty = 'n', inset = -.3, cex = 2); par (xpd = F)
 
# e)
plot (dist ~ speed, data = cars,                     # data to be used
      xlim = c(0, 25),                               # define range of the x-axis
      axes = FALSE, ann = FALSE, type = 'n')  
points (dist ~ speed, data = cars,
        pch = 21, col = 'tomato', bg = 'yellow')     # add data points with given symbols and colours
title (xlab = 'Speed [mph]', ylab = 'Distance [ft]', # add x- and y-labels
       main = 'Scatterplot')                         # add main title to the plot
box ()   
axis (1)                                             # add axis 1 (bottom horizontal)
par (xpd = T); legend ('topleft', legend = 'e)', bty = 'n', inset = -.3, cex = 2); par (xpd = F)
 
# f)
plot (dist ~ speed, data = cars,                     # data to be used
      xlim = c(0, 25),                               # define range of the x-axis
      axes = FALSE, ann = FALSE, type = 'n')  
points (dist ~ speed, data = cars,
        pch = 21, col = 'tomato', bg = 'yellow')     # add data points with given symbols and colours
title (xlab = 'Speed [mph]', ylab = 'Distance [ft]', # add x- and y-labels
       main = 'Scatterplot')                         # add main title to the plot
box ()   
axis (1)                                             # add axis 1 (bottom horizontal)
axis (2)    
par (xpd = T); legend ('topleft', legend = 'f)', bty = 'n', inset = -.3, cex = 2); par (xpd = F)
dev.off ()
 
png (file = 'plotting_5.png', height = 550, width = 550, units = 'px', pointsize = 16)
plot (dist ~ speed, data = cars,                     # a) data to be used,
      xlim = c(0, 25),                               #    define range of the x-axis,
      axes = FALSE, ann = FALSE, type = 'n')         #    make sure nothing will be actually plotted 
points (dist ~ speed, data = cars,                   # b) add data points,
        pch = 21, col = 'tomato', bg = 'yellow', 
        lwd = 2)     
title (xlab = 'Speed [mph]', ylab = 'Distance [ft]', # c) add x- and y-labels,
       main = 'Scatterplot', line = 2.5)                         #    add main title to the plot
box (bty = 'l')                                               # d) add box around the plot
axis (1, col = 'red', at = seq (0, 25))              # e) add axis 1 (bottom horizontal)
axis (2, las = 2)                                             # f) add axis 2 (left vertical)
dev.off ()
recol/plotting_rscript.txt · Last modified: 2019/10/10 18:26 by david