Generic X-Y Plotting
- R Plot Lines Not Showing
- R Plot Title Not Showing
- R Shiny Plot Not Showing
- R Markdown Plots Not Showing
- R Plot Legend Not Showing
- R Plots Not Visible
R par function. We can put multiple graphs in a single plot by setting some graphical parameters with the help of par function. R programming has a lot of graphical parameters which control the way our graphs are displayed. The par function helps us in setting or inquiring about these parameters. For example, you can look at all the. R plots not showing up #800. Closed iagomez opened this issue Oct 6, 2016 14 comments Closed R plots not showing up #800. Iagomez opened this issue Oct 6, 2016.
Generic function for plotting of R objects. For more details about the graphical parameter arguments, see
par
.For simple scatter plots,
plot.default
will be used. However, there are plot
methods for many R objects, including function
s, data.frame
s, density
objects, etc. Use methods(plot)
and the documentation for these.- Keywords
- hplot
Usage
Arguments
the coordinates of points in the plot. Alternatively, a single plotting structure, function or any R object with a
plot
method can be provided.the y coordinates of points in the plot, optional if
x
is an appropriate structure.Arguments to be passed to methods, such as graphical parameters (see
par
). Many methods will accept the following arguments:type
what type of plot should be drawn. Possible types are
'p'
for points,'l'
for lines,'b'
for both,'c'
for the lines part alone of'b'
,'o'
for both ‘overplotted’,'h'
for ‘histogram’ like (or ‘high-density’) vertical lines,'s'
for stair steps,'S'
for other steps, see ‘Details’ below,'n'
for no plotting.
type
s give a warning or an error; using, e.g., type = 'punkte'
being equivalent to type = 'p'
for S compatibility. Note that some methods, e.g.plot.factor
, do not accept this.main
an overall title for the plot: see
title
.sub
a sub title for the plot: see
title
.xlab
a title for the x axis: see
title
.ylab
a title for the y axis: see
title
.asp
the (y/x) aspect ratio, see
plot.window
.Details
The two step types differ in their x-y preference: Going from ((x1,y1)) to ((x2,y2)) with (x1 < x2),
type = 's'
moves first horizontal, then vertical, whereas type = 'S'
moves the other way around.See Also
plot.default
, plot.formula
and other methods; points
, lines
, par
. For thousands of points, consider using smoothScatter()
instead of plot()
.For X-Y-Z plotting see
contour
, persp
and image
.Aliases
- plot
Examples
library(graphics)
# NOT RUN {require(stats) # for lowess, rpois, rnormplot(cars)lines(lowess(cars))plot(sin, -pi, 2*pi) # see ?plot.function## Discrete Distribution Plot:plot(table(rpois(100, 5)), type = 'h', col = 'red', lwd = 10, main = 'rpois(100, lambda = 5)')## Simple quantiles/ECDF, see ecdf() {library(stats)} for a better one:plot(x <- sort(rnorm(47)), type = 's', main = 'plot(x, type = 's')')points(x, cex = .5, col = 'dark red')# }
Community examples
[email protected] at Nov 17, 2020 graphics v3.6.2
```r # Plot with multiple lines in different color: plot(sin,-pi, 4*pi, col = 'red') plot(cos,-pi, 4*pi, col = 'blue', add = TRUE) ```
[email protected] at Nov 17, 2020 graphics v3.6.2
```r ## Plot with multiple lines in different color: plot(sin,-pi, 4*pi, col = 'red') plot(cos,-pi, 4*pi, col = 'blue', add = TRUE) ```
plot(basedata1$iq, basedata$read_ab, main='Diagrama de Dispersión', xlab = 'read_ab', ylab = 'iq')
[email protected] at Dec 13, 2020 graphics v3.4.0
## Linear Regression ExamplePlot points and add linear regression model line:```rlinreg <- lm(dist ~ speed, cars)linreg_coeffs <- coef(linreg)lineq <- paste('distance = ', linreg_coeffs[2], ' * speed + ', linreg_coeffs[1])plot(cars, main = 'Car distance by speed', sub = lineq, xlab = 'speed', ylab = 'distance', pch = 19)abline(linreg, col = 'blue')```
[email protected] at Jan 17, 2017 graphics v3.3.2
Pass a numeric vector to the `x` and `y` arguments, and you get a scatter plot. The `main` argument provides a [`title()`](https://www.rdocumentation.org/packages/graphics/topics/title). ```{r} plot(1:100, (1:100) ^ 2, main = 'plot(1:100, (1:100) ^ 2)') ``` If you only pass a single argument, it is interpreted as the `y` argument, and the `x` argument is the sequence from 1 to the length of `y`. ```{r} plot((1:100) ^ 2, main = 'plot((1:100) ^ 2)') ``` `cex` ('character expansion') controls the size of points. `lwd` controls the line width. `pch` controls the shape of points - you get 25 symbols to choose from, as well as alphabetic characters. `col` controls the color of the points. When `pch` is `21:25`, the points also get a background color which is set using `bg`. [`points()`](https://www.rdocumentation.org/packages/graphics/topics/points) for more on how to change the appearance of points in a scatter plot. ```{r} plot( 1:25, cex = 3, lwd = 3, pch = 1:25, col = rainbow(25), bg = c(rep(NA, 20), terrain.colors(5)), main = 'plot(1:25, pch = 1:25, ...)' ) ``` If you specify `type = 'l'`, you get a line plot instead. See [`plot.default()`](https://www.rdocumentation.org/packages/graphics/topics/plot.default) for a demonstration of all the possible values for type. ```{r} plot( (1:100) ^ 2, type = 'l', main = 'plot((1:100) ^ 2, type = 'l')' ) ``` `lty` controls the line type. `col` and `lwd` work in the same way as with points. [`lines()`](https://www.rdocumentation.org/packages/graphics/topics/lines) for more on how to change the appearance of lines in a line plot. ```{r} plot( (1:100) ^ 2, type = 'l', lty = 'dashed', lwd = 3, col = 'chocolate', main = 'plot((1:100) ^ 2, type = 'l', lty = 'dashed', ...)' ) ``` It is best practise to keep your `x` and `y` variables together, rather than as separate variables. ```{r} with( cars, plot(speed, dist, main = 'with(cars, plot(speed, dist))') ) ``` The formula interface, similar to modeling functions like [`lm()`](https://www.rdocumentation.org/packages/stats/topics/lm), makes this convenient. See [`plot.formula()`](https://www.rdocumentation.org/packages/graphics/topics/plot.formula) for more information. ```{r} plot( dist ~ speed, data = cars, main = 'plot(dist ~ speed, data = cars)' ) ``` If you pass a two column data frame or matrix then the columns are treated as the x and y values. So in this case, you can simply do: ```{r} plot(cars, main = 'plot(cars)') ``` The [`lines()`](https://www.rdocumentation.org/packages/graphics/topics/lines), [`points()`](https://www.rdocumentation.org/packages/graphics/topics/points) and [`title()`](https://www.rdocumentation.org/packages/graphics/topics/title) functions add lines, points and titles respectively to an existing plot. ```{r} plot(cars) lines(lowess(cars)) title('plot(cars); lines(lowess(cars))') ``` If the `x` variable is categorical, `plot()` knows to draw a box plot instead of a scatter plot. See [`boxplot()`](https://www.rdocumentation.org/packages/graphics/topics/boxplot) for more information on drawing those. ```{r} with( sleep, plot(group, extra, main = 'with(sleep, plot(group, extra))') ) ``` Again, the formula interface can be useful here. ```{r} plot(extra ~ group, sleep, main = 'plot(extra ~ group, sleep)') ``` Axis limits can be set using `xlim` and `ylim`. ```{r} plot( (1:100) ^ 2, xlim = c(-100, 200), ylim = c(2500, 7500), main = 'plot((1:100) ^ 2, xlim = c(-100, 200), ylim = c(2500, 7500))' ) ``` You can set log-scale axes using the `log` argument. ```{r} plot( exp(1:10), 2 ^ (1:10), main = 'plot(exp(1:10), 2 ^ (1:10))' ) plot( exp(1:10), 2 ^ (1:10), log = 'x', main = 'plot(exp(1:10), 2 ^ (1:10), log = 'x')' ) plot( exp(1:10), 2 ^ (1:10), log = 'y', main = 'plot(exp(1:10), 2 ^ (1:10), log = 'y')' ) plot( exp(1:10), 2 ^ (1:10), log = 'xy', main = 'plot(exp(1:10), 2 ^ (1:10), log = 'xy')' ) ``` If you pass a table of counts for a vector, `plot()` draws a simple histogram-like plot. See [`hist()`](https://www.rdocumentation.org/packages/graphics/topics/hist) for a more comprehensive histogram function. ```{r} plot( table(rpois(100, 5)), main = 'plot(table(rpois(100, 5)))' ) ``` For multi-dimensional tables, you get a mosaic plot. See [`mosaicplot()`](https://www.rdocumentation.org/packages/graphics/topics/mosaicplot) for more information. ```{r} plot( table(X = rpois(100, 5), Y = rbinom(100, 10, 0.75)), main = 'plot(table(X = rpois(100, 5), Y = rbinom(100, 10, 0.75)))' ) ``` You can also pass functions to plot. See [`curve()`](https://www.rdocumentation.org/packages/graphics/topics/curve) for more examples. ```{r} plot( sin, from = -pi, to = 2 * pi, main = 'plot(sin, from = -pi, to = 2 * pi)' ) ``` Use the axis function to give fine control over how the axes are created. See [`axis()`](https://www.rdocumentation.org/packages/graphics/topics/axis) and [`Axis()`](https://www.rdocumentation.org/packages/graphics/topics/Axis) for more info. ```{r} plot( sin, from = -pi, to = 2 * pi, axes = FALSE, main = 'plot(sin, axes = FALSE, ...); axis(1, ...); axis(2)' ) axis( 1, # bottom axis pi * (-1:2), c(expression(-pi), 0, expression(pi), expression(2 * pi)) ) axis(2) # left axis ``` Further graphical parameters can be set using [`par()`](https://www.rdocumentation.org/packages/graphics/topics/par). See [`with_par()`](https://www.rdocumentation.org/packages/withr/topics/with_par) for the best way to use `par()`. ```{r} old_pars <- par(las = 1) # horizontal axis labels plot((1:100) ^ 2, main = 'par(las = 1); plot((1:100) ^ 2)') par(old_pars) # reset parameters ```
API documentation
[This article was first published on Carlisle Rainey » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
(This post is part of the #cumpa series of blog posts and tweets I am writing leading up to SPSA. For more information, see this blog post. To follow along, subscribe to my blog here or follow me on Twitter here. To engage in the conversation, reply to this tweet.)
R has powerful graphical capabilities and I use it in all my papers to plot data and illustrate theoretical ideas. The default plot function, however, doesn’t give the reader needed control over the axis labels. Below I’ve plotted the some data using the R defaults and then made several changes for comparison. I think the plot on the right looks much better. In this post, I’ll show you how to make the changes.
The default plot function in R works something like this.
n = 100
x = rnorm(n)
y = rnorm(n, x)
png('fig1.png', width = 400, height = 300)
plot(x, y,
xlab = 'Explanatory Variable',
ylab = 'Outcome Variable')
abline(lm(y~x), col = 'red', lwd = 2)
dev.off()
This code produces the following figure.
Notice three things about the figure above.
- The tick marks along the axes are too long.
- The tick mark labels along the vertical axis are written vertically.
- The tick mark labels are too far from the tick marks.
- The axis labels are too far from the axes.
Below I discuss in detail how to fix each of these issues.
Orienting the Tick Mark Labels
To make the desired changes, we need to not rely so heavily on the axis annotation of the plot function. Instead, I like to initiate the plot using the plot function and add in the notations later.
png('fig2.png', width = 400, height = 300)
R Plot Lines Not Showing
plot(x, y, axes = F, xlab = NA, ylab = NA)
abline(lm(y~x), col = 'red', lwd = 2)
dev.off()
This code produces the following figure without any axes or axis labels.
Now, all we need to do is add the axes into the figure and add the axis labels. To do this, I simply call the axis() command twice, once for each side I want to add an axis to. A simply call to the box() function adds a box around the plot. The code below adds a box around the plot and the tick marks and tick mark labels.
png('fig3.png', width = 400, height = 300)
plot(x, y, axes = F,
xlab = NA,
ylab = NA)
abline(lm(y~x), col = 'red', lwd = 2)
box()
axis(side = 1)
axis(side = 2)
dev.off()
This code creates the following figure.
This plot matches the first plot produced using the notations in the plot function, but gives more control. We can make a simple change to the command that adds the axis to the vertical axis by setting the las option. This orients the tick mark labels horizontally.
png('fig4.png', width = 400, height = 300)
plot(x, y, axes = F,
xlab = NA,
ylab = NA)
abline(lm(y~x), col = 'red', lwd = 2)
box()
axis(side = 1)
axis(side = 2, las = 1)
dev.off()
Now our tick mark labels along the vertical axis are labeled horizontally–the only way that really makes sense.
Length of Tick Marks
Let’s now turn to the length of the tick marks. Use the tck option in the axis command to control the length of the tick marks.
png('fig5.png', width = 400, height = 300)
plot(x, y, axes = F,
xlab = NA,
ylab = NA)
R Plot Title Not Showing
abline(lm(y~x), col = 'red', lwd = 2)
box()
axis(side = 1, tck = -.01)
axis(side = 2, las = 1, tck = -.01)
dev.off()
Now that’s more like it. Unfortunately, making the tick marks shorter has only made their labels appear even further from the plot.
Tick Mark Labels
Fixing the tick mark labels requires a little bit of trickery. I fix this by calling the axis() command twice for each axis to be created. The first call plots the tick marks, but no labels. The second call plots the labels, but no tick marks. But adjust the line option in the second call, the labels can be repositioned.
png('fig6.png', width = 400, height = 300)
plot(x, y, axes = F,
xlab = NA,
ylab = NA)
abline(lm(y~x), col = 'red', lwd = 2)
box()
axis(side = 1, tck = -.015, labels = NA)
R Shiny Plot Not Showing
axis(side = 2, tck = -.015, labels = NA)
axis(side = 1, lwd = 0, line = -.4)
axis(side = 2, lwd = 0, line = -.4, las = 1)
dev.off()
Axis Labels
All that is left is to add in the axis labels. This is done with a quick call to the mtext() command. Control the distance from the axis with the line command.
png('fig7.png', width = 400, height = 300)
plot(x, y, axes = F,
xlab = NA,
ylab = NA)
R Markdown Plots Not Showing
abline(lm(y~x), col = 'red', lwd = 2)
box()
axis(side = 1, tck = -.015, labels = NA)
axis(side = 2, tck = -.015, labels = NA)
axis(side = 1, lwd = 0, line = -.4)
axis(side = 2, lwd = 0, line = -.4, las = 1)
mtext(side = 1, 'Explanatory Variable', line = 2)
mtext(side = 2, 'Outcome Variable', line = 2)
R Plot Legend Not Showing
dev.off()
R Plots Not Visible
Done.
To leave a comment for the author, please follow the link and comment on their blog: Carlisle Rainey » R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.