![]() ![]() Although hist() accepts xlim and ylim parameters, they are ignored when hist() is used with add = TRUE, so they must be specified in the plot() call in this example. For example, plot() must be called before hist(), as add = TRUE isn’t accepted by the plot() function. For example, given two numeric vectors of equal length, it produces a dotplot. Like hist(), plot() is a generic function that determines what the plot should look like on the basis of class attributes of the data given to it. The most basic plotting function (other than hist(), which we’ve already seen) is plot(). To finish writing the PDF file, a call to dev.off() is required (it takes no parameters). The name of this file can be changed by calling the pdf() function, giving a file name to write to. Alternatively, or if we are producing plots via a remote command line login, each plot will be saved to a PDF file called Rplots.pdf. When working with a graphical interface like RStudio, plots are by default shown in a pop-up window or in a special plotting panel for review. Further, plotting is often the end result of a complex analysis, so it makes sense to think of graphical output much like any other program output that needs to be reproducible. Although writing a noninteractive program for producing plots might seem counterintuitive, it is beneficial as a written record of how the plot was produced for future reference. First, how plots are generated depends on whether we are running R through a graphical user interface (like RStudio) or on the command line via the interactive R console or executable script. ![]() A Brief Introduction to Base-R GraphicsĪlthough this chapter focuses on the ggplot2 package, it is worth having at least passing familiarity with some of the basic plotting tools included with R. As a bonus, the results are usually professional looking with little tweaking, and the integration into R makes data visualization a natural extension of data analysis. What ggplot2 provides is a remarkable balance of power and ease of use. Neither is ggplot2 the easiest-simpler programs like Microsoft Excel are much easier to use. The ggplot2 package is not the most powerful or flexible-the graphics provided by default with R may take that title. One of these is ggp lot2, which differs from many other data visualization packages in that it is designed around a well-conceived “grammar” of graphics. A basic installation of R provides an entire set of tools for plotting, and there are many libraries available for installation that extend or supplement this core set. R provides some of the most powerful and sophisticated data visualization tools of any program or programming language (though gnuplot mentioned in chapter 12, “ Miscellanea,” is also quite sophisticated, and Python is catching up with increasingly powerful libraries like matplotlib). ![]()
0 Comments
Leave a Reply. |