• Home
  • University of Queensland
  • Analysis Of Scientific Data
  • Data Import and Visualization in R

Data Import and Visualization in R

Getting Data into R 1. Enter quiz2 = read. csv ("Quiz2. csv") in the Console to import the data. Checking the Data Use str (quiz2) to check you have copied 300 observations of 20 variables. Alternatively, check quiz2 in the Environment pane in RStudio. · str (quiz2) gives the structure of the data .nrow (quiz2) gives the number of rows in the data ·names (quiz2) gives just the variable names . head (quiz2) gives the first 6 rows of the data Looking at the Data You will need to use library (lattice) in R before trying the following examples. (If you have never used this library before, you may need to first install it using install. packages ("lattice") .) · library (lattice) · histogram (quiz2$Height) · histogram (~ Height, data=quiz2) ·histogram (~ Height, data=quiz2, nint=20) ·bwplot (~ Height, data=quiz2) · bwplot (Height ~ Superpower, data=quiz2) . xyplot (Forearm ~ Height, data=quiz2) · xyplot (Forearm ~ Height, data=quiz2, groups=Handed) . xyplot (Forearm ~ Height, data=quiz2, groups=Handed, auto. key=TRUE) . xyplot (Forearm ~ Height, data=quiz2, groups=Handed, auto. key=TRUE, type=c ("p", "smooth") , lwd=3) The c () notation defines a list of values. Here we use it to give a list of plotting options for the plot: · g shows a grid ·p shows data points · 1 joins the data points together with lines ·b shows both data points and lines · r shows a least-squares (regression) line · smooth shows a loess smoothing line • Summary Statistics ·mean (quiz2$Height) ·sd (quiz2$Height) ·median (quiz2$Height) · IQR (quiz2$Height) · table (quiz2$Eyes) ·prop. table (table (quiz2$Eyes) ) You can also obtain a summary of the whole data frame using · summary (quiz2) Lattice Graphics Using lattice graphics allows easy exploration of differences between groups, such as · histogram (~ Sleep | Superpower, data=quiz2) · histogram (~ Minutes | Residence, data=quiz2) .xyplot (Forearm ~ Height | Superpower, data=quiz2, type=c ("p", "smooth") ) Categorical Data Bar charts can be used to show tables of counts and proportions, and to show conditional distributions, such as the distribution of relationship status conditional on residence. · barchart (table (quiz2$Residence) ) . barchart (prop. table (table (quiz2$Residence) ) ) .barchart (prop. table (table (quiz2$Residence, quiz2$Holidays) ) , auto. key=TRUE) .barchart (prop. table (table (quiz2$Residence, quiz2$Holidays) , 1) , auto. key=TRUE) . barchart (prop. table (table (quiz2$Residence, quiz2$Holidays) , 1) , auto.key=list (columns=2) ) Density Plot · densityplot (~ Height, data=quiz2) · densityplot (~ Height, data=quiz2, bw=1.0) · densityplot (~ Height, data=quiz2, bw=5.0) · densityplot (~ Height, data=quiz2, group=Handed, auto. key=TRUE) Violin Plots A violin plot shows more detail of a distribution by adding a density plot around a regular box plot. To create a violin plot with lattice graphics, you need to use the panel function which allows you to overlay components in the same plot: ·bwplot (~ Minutes, quiz2, panel = function ( ... , box. ratio) { panel. violin ( ... , box.ratio = box. ratio) ; panel.bwplot ( ... , fill = NULL, box.ratio = . 1)