-
R Plots Not Showing카테고리 없음 2021. 6. 25. 10:26
The simple scatterplot is created using the plot function. The basic syntax for creating scatterplot in R is − plot(x, y, main, xlab, ylab, xlim, ylim, axes) Following is the description of the parameters used − x is the data set whose values are the horizontal coordinates. Y is the data set whose values are the vertical coordinates. To see more of the R is Not So Hard! Tutorial series, visit our R Resource page. About the Author: David Lillis has taught R to many researchers and statisticians. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics. Combining Plots. R makes it easy to combine multiple plots into one overall graph, using either the par( ) or layout( ) function. With the par( ) function, you can include the option mfrow=c(nrows, ncols) to create a matrix of nrows x ncols plots that are filled in by row. To see more of the R is Not So Hard! Tutorial series, visit our R Resource page. About the Author: David Lillis has taught R to many researchers and statisticians. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics.
R has excellent graphics and plotting capabilities, which can mostly be found in 3 main sources: base graphics, the lattice package, the ggplot2 package. The latter two are built on the highly flexible grid graphics package, while the base graphics routines adopt a
pen and paper
model for plotting, mostly written in Fortran, which date back to the early days of S, the precursor to R (for more on this, see the book Software for Data Analysis - Programming with R by John Chambers, which has lots of very useful information).For a basic introduction, see the 'getting started' page here. Base graphics are very flexible and allow a great deal of customisation, with many individual functions available. However, they lack a coherent underlying framework and, for visualizing highly structured data, are outclassed by lattice and ggplot2.
- Quick reference info:
- Useful plotting functions:
- Create some data for plotting:
- Different point styles:
Plot symbols and colours can be specified as vectors, to allow individual specification for each point. R usesrecycling
of vectors in this situation to determine the attributes for each point, i.e. if the length of the vector is less than the number of points, the vector is repeated and concatenated to match the number required.- Single plot symbol (see '?points' for more) and colour (type 'colours()' or 'colors()' for the full list of predefined colours):
- Create vector of contiguous colours in a
rainbow
palette:- Label axes:
Axis limits are controlled byxlim
andylim
, which are vectors of the minimum and maximum values, respectively.- Specify axis limits:
The basic idea behind the R function
layout
is to divide the plotting device into a series of rows and columns specified by a matrix. The matrix itself is composed of values referring to the plot number, generally just 1,2,3...etc., but can feature repetition.- Show simple 2x1 matrix:
- To view the graphical layout, the following will show the borders of the sub-panels and the number identying each one:
- Now fill the layout with 4 plots:
Theheights
andwidths
arguments tolayout
are vectors of relative heights and widths of the matrix rows and columns, respectively.- Specifying panels of different sizes:
The function
curve
allows you to plot equations or complex functions, either on their own, or added to an existing plot (withadd=T
).- Plot some analytic expressions:
- Plot a function, with specified arguments:
curve
provides the function to be plotted with a vector of x-axis values calledx
with which to calculate the corresponding y-axis data. If the argument of your function is not calledx
(e.g.r
) , then you need to use the following syntax:
. The following example illustrates this with a plot of several blackbody curves.curve(myfun(r=x))
- First, define a function for the Planck blackbody law to calculate the radiation intensity as a function of wavelength (lambda, in microns) and temperature (Temp, in Kelvin):
- Now plot the curve for the default temperature of 1000K, with some axis labels:
- Finally, add 2 more curves for 900K and 800K:
- Print a copy to a PDF file (the resulting plot can be viewed here):
- To find out the coordinates at a particular position on a graph, type:
locator()
then left click with the mouse any number of times within the axes and right click to end; the R prompt will then return and a list will be printed with the X and Y coordinates of the positions clicked. You can retain this information by repeating the above, but withA <- locator()
the coordinates will then be stored in
andA$x
A$y
- To identify a particular point in a plot, use 'identify', e.g:
Now left click near one or more points and the element number of that point will be printed at the bottom, left, top or right of the point, depending on which side of it you clicked. Right click inside the axes to finish, and the element numbers of the points identified will be printed, as forlocator
This is more useful if you have named points, in which caseidentify
can print the name instead of the element number, for example:Lattice is an excellent package for visualizing multivariate data, which is essentially a port of the S software trellis display to R. While it lacks the flexibility and extensibility of ggplot2, it nevertheless represents a great set of routines for quickly displaying complex data with ease. This makes it ideal for use in exploratory data analysis; you can find out more by reading the excellent book
Lattice - Multivariate Data Visualization with R
by Deepayan Sarkar.- Some examples of using lattice, first assemble some data (from this book) on the masses (in kg) and semi-major axis lengths (in metres) of the Planets and a dotplot of the former: A histogram and a kernel-smoothed density plot of the semi-major axes: Now to demonstrate the multivariate capabilities, assemble the data in a data frame and create a categorical variable
giant
, which identifies the 4 most massive planets: Lattice can now separately handle the different categories, either by usinggroup
, to use different plotting symbols etc. within the same panel, e.g.: ...or byconditioning
on a categorical variable, to plot separate panels for each dataset: - You can also easily plot linear regression models (from
lm
) for each group category, using thetype
argument: - Lattice offers a very quick route to visualize a set of properties conditioned on one or more factors. For example, to show boxplots of 4 different quantities in separate panels, with each panel comparing values in different categories:
- Another excellent feature of lattice is the ability to span plots over multiple pages, using the
layout
argument (which is a vector specifying the required number of columns, rows & pages for the plot panels). This is great if you are plotting a large number of panels and want to dump them onto separate pages of a PDF document, say. Following on from the previous example (saved as the lattice objectp
): - You can see examples of a timeseries and dotplot created with lattice, together with the R code that produced them in the R gallery page.
The
gg
inggplot2
refers to the book The Grammar of Graphics (which I can highly recommend), by Leland Wilkinson, which has been implemented in an R package by Hadley Wickham. This is another excellent package for multivariate data analysis in R, which is based on agrammatical
approach to graphics that provides great flexibility in design. Still under active delevelopment, the only noticeable (and slight) drawback with ggplot2 is the small delay in rendering the final plot. This reflects the fact that there's a lot going on behind the scenes in order to produce such highly polished graphics. The package has an offical website and wiki and you can find out more by reading the excellent bookggplot2 - Elegant Graphics for Data Analysis
by Hadley Wickham.- Continuing with the data frame of planet properties created above:
- You can see examples of plotting error envelopes and a multi-panel boxplot created with ggplot2, together with the R code that produced them in the R gallery page.
- For more examples (with code snippets) of using ggplot2 to create a variety of plots, see the PDF slides from my recent useR! 2011 conference presentation.
R Shiny Plot Not Showing
For further information, you can find out more about how to access, manipulate, summarise, plot and analyse data using R.
R Plots Not Showing Fractions
Also, why not check out some of the graphs and plots shown in the R gallery, with the accompanying R source code used to create them.