Visualization of Graphics in R
To understand graphics in R, initially dataset is selected to understand it.
The dataset 'measles' is downloaded from the following link: https://vincentarelbundock.github.io/Rdatasets/datasets.html
The dataset 'measles' is imported in R by clicking on import dataset option below the environment tab in Rstudio followed by appearanace of dialog box to choose data set from downloads.
To produce graphics in R, there are three types:
1. Base graphics
2. Lattice
3. ggplot2
1. Base graphics:
Base graphics already exists in the R installation. It allows to create a wide range of two dimensional plots, including scatterplots, line graphs, bar graphs, and more.
> plot(measles,pch=21,col=rainbow(12),main= " Deaths in London from measles", cex=1.0)
> plot(measles,pch=21,bg="green",col=cm.colors(12),main= " Deaths in London from measles", cex=1.0)'plot' function is implemented which creates scatterplot of a dataset. 'pch=21' is used to set the type of plotting to a filled circle with a solid fill. There are different color palletes which can be used, but for this code 'rainbow()' and 'cm.colors()' is utilized. The title of the plot can be added to the graph using 'main' function. 'bg' defines fill color of the circle. 'cex' controls the size of the points in the plot (character expansion). Finally, this code produces a scatter plot of the number of deaths in london over time with each point representing a particular time period.
Disadvantages:
Awkward outflow-User must deal with laying out the individual plots
No built in support for encoding additional information
2. Lattice:
It provides functions to create high level graphics like trellis plots. It provides an additional layer of abstraction when compared to base graphics. In addition, they are very customisable.
> install.packages("lattice")> library(lattice)
> xyplot(value~time, data=measles,main= " Deaths in London from measles", pch=".",cex=2.5)
Initially, lattice package is installed to create these graphics in R followed by loading the lattice package using library(). 'xyplot' function is used to plot the relationship between the time and the values i.e number of death due to measles in London. 'pch' function sets the type of plot. 'cex' controls the size of the points in the plot (character expansion).
> wireframe(volcano, shade = TRUE, aspect = c(61/87, 0.4),light.source = c(10,0,10))
3. ggplot2:
Each component is added to the plot as a layer that never been done in statistical programming language. It creates complex and highly customizable visualizations.It takes two primary arguments, data and aes(). Aesthetic mapping to pass to be plotted.
> install.packages("ggplot2")> library(ggplot2)
> ggplot(measles, aes(x=time,y=value))+geom_line(linetype=3)
> ggplot(measles, aes(x=time,y=value))+geom_point()
To create ggplot2 graphics in R, 'ggplot2' package is installed and loaded using 'library()' function. 'ggplot2' function is implemented to the 'measles' dataset.Plots convey information through various aspects of their aesthetics. 'x' is defined by 'time','y' is defined by 'value'. The sign '+' added to give a new layer with 'geom_line()' which give line to plot. The code 'linetype=3' sets the line type to dashed line.
'geom_point()' function adds points to the plot.
Through these graphics, the year in which more and less number of deaths due to measles in London can be determined and in addition, more clustering of the deaths in a particular year can also made out through these graphics. In this way, by importing datatsets and implementing codes for these graphics gives us information and broad perspective of how deaths are progressing and indirectly impact of disease on the population.These graphs are very helpful in pandemic too given the dataset.
URL to git repo:https://github.com/VedaVangala/vedas-r-repo/tree/main/R9
References
Wickham, H. (2015). R Packages.
Matloff. N. (2011). The Artof R programming.
Comments
Post a Comment