Start Learning for Free

Join over 1,000,000 other Data Science learners and start one of our interactive tutorials today!

Topic r small

15 Questions All R Users Have About Plots

July 30th, 2015 in R Programming

R allows you to create different plot types, ranging from the basic graph types like density plots, dot plots, boxplots and scatter plots, to the more statistically complex types of graphs such as probability plots.

In addition, it allows you to go from producing basic graphs with little customization to plotting advanced graphs with full-blown customization in combination with interactive graphics. Nevertheless, not always do we get the results that we want for our R plots: Stack Overflow is flooded with our questions on plots, with many recurring questions.

question

This is why DataCamp decided to put all the frequently asked questions and their top rated answers together in a blog post.

To practice creating plots in R, try this course on data visualization in R.

The 15 Questions

1. How To Draw An Empty R Plot?

Open A New Plot Frame

You can open an empty plot frame and activate the graphics device in R as follows:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJwbG90Lm5ldygpXG5cbiMgT3IgdHJ5IG91dCB0aGUgZm9sbG93aW5nIGNvbW1hbmQgXG4jIGZyYW1lKCkifQ==

Note that the plot.new() and frame() functions define a new plot frame without it having any axes, labels, or outlining. It indicates that a new plot is to be made: a new graphics window will open if you don’t have one open yet, otherwise the existing window is prepared to hold the new plot. You can read up on these functions here.

  • x11() can also opens a new windows device in R for the X Window System (version 11)!
  • quartz() starts a graphics device driver for the OS X System.

Set Up The Measurements Of The Graphics Window

You can also use the plot.window() function to set the horizontal and vertical dimensions of the empty plot you just made:

plot.new()
pWidth = 3
pHeight = 2
plot.window(c(0,pWidth),
            c(0,pHeight))

Draw An Actual Empty Plot

You can draw an empty plot with the plot() function. Additionally, fill up the empty plot with axes, axes labels and a title with the mtext() and title() functions.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERyYXcgYW4gZW1wdHkgcGxvdFxucGxvdCg1LCBcbiAgICAgNSwgXG4gICAgIHR5cGU9XCJuXCIsIFxuICAgICBheGVzPUZBTFNFLCBcbiAgICAgYW5uPUZBTFNFLCBcbiAgICAgeGxpbT1jKDAsIDEwKSwgXG4gICAgIHlsaW0gPSBjKDAsMTApKVxuXG4jIEFkZCB0ZXh0IHRvIHRoZSB4LWF4aXNcbm10ZXh0KFwieC1heGlzXCIsIFxuICAgICAgc2lkZT0xKVxuXG4jIEFkZCB0ZXh0IHRvIHRoZSB5LWF4aXNcbm10ZXh0KFwieS1heGlzXCIsXG4gICAgICBzaWRlPTIpIFxuXG4jIEFkZCBhIHRpdGxlXG50aXRsZShcIkFuIFIgUGxvdFwiKSJ9

You give the coordinates (5,5) for the plot, but that you don’t show any plotting by putting the type argument to "n".

Tip: try to put this argument to "p" or "b" and see what happens!

What’s more, you don’t annotate the plot, but you do put limits from 0 to 10 on the x- and y-axis.

Lastly, you may choose to draw a box around your plot by using the box() function and add some points to it with the points() function:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERyYXcgYW4gZW1wdHkgcGxvdFxucGxvdCg1LCBcbiAgICAgNSwgXG4gICAgIHR5cGU9XCJuXCIsIFxuICAgICBheGVzPUZBTFNFLCBcbiAgICAgYW5uPUZBTFNFLCBcbiAgICAgeGxpbT1jKDAsIDEwKSwgXG4gICAgIHlsaW0gPSBjKDAsMTApKVxuXG4jIEFkZCB0ZXh0IHRvIHRoZSB4LWF4aXNcbm10ZXh0KFwieC1heGlzXCIsIFxuICAgICAgc2lkZT0xKVxuXG4jIEFkZCB0ZXh0IHRvIHRoZSB5LWF4aXNcbm10ZXh0KFwieS1heGlzXCIsXG4gICAgICBzaWRlPTIpIFxuXG4jIEFkZCBhIHRpdGxlXG50aXRsZShcIkFuIFIgUGxvdFwiKVxuXG4jRHJhdyBhIGJveFxuYm94KClcblxuI1B1dCAocmVkKSBwb2ludCBpbiB0aGUgcGxvdCBhdCAoNSw1KVxucG9pbnRzKDUsXG4gICAgICAgNSwgXG4gICAgICAgY29sPVwicmVkXCIpIFxucG9pbnRzKDUsIFxuICAgICAgIDcsIFxuICAgICAgIGNvbD1cIm9yYW5nZVwiLCBcbiAgICAgICBwY2g9MywgXG4gICAgICAgY2V4PTIpXG5wb2ludHMoYygwLCAwLCAxKSwgXG4gICAgICAgYygyLCA0LCA2KSwgXG4gICAgICAgY29sPVwiZ3JlZW5cIiwgXG4gICAgICAgcGNoPTQpIn0=

Note that you can put your x-coordinates and y-coordinates in vectors to plot multiple points at once. The pch argument allows you to select a symbol, while the cex argument has a value assigned to it that indicates how much the plotted text and symbols should be scaled with respect to the default.

Tip: if you want to see what nuber links to what symbol, click here.

2. How To Set The Axis Labels And Title Of The R Plots?

The axes of the R plots make up one of the most popular topics of Stack Overflow questions; The questions related to this topic are very diverse.

Keep on reading to find out what type of questions DataCamp has found to be quite common!

Name Axes (With Up- Or Subscripts) And Put A Title To An R Plot?

You can easily name the axes and put a title in place to make your R plot more specific and understandable for your audience.

This can be easily done by adding the arguments main for the main title, sub for the subtitle, xlab for the label of the x-axis and ylab for the label of the y-axis.

Note that if you want to have a title with up-or subscripts, you can easily add these in the main argument of the plot() function. Make sure to use expression() and use the ^ symbol for upscripts or square brackets for a subscript.

Try it out in the DataCamp Light chunk below:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBkYXRhXG54IDwtIHNlcSgwLDIqcGksMC4xKVxueSA8LSBzaW4oeClcblxuIyBQbG90IHRoZSBkYXRhXG5wbG90KHgsXG4gICAgIHksXG4gICAgIG1haW49ZXhwcmVzc2lvbihcIm1haW4gdGl0bGVcIl4yKSwgXG4gICAgIHN1Yj1leHByZXNzaW9uKFwic3ViLXRpdGxlXCJbMl0pLCBcbiAgICAgeGxhYj1cIngtYXhpcyBsYWJlbFwiLCBcbiAgICAgeWxhYj1cInktYXhpcyBsYWJlbFwiKSJ9

R plots tutorial

You can also make sure that you have Greek letters in your axis labels by using code which combines the expression() and paste() functions. Execute the following piece of code:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBkYXRhXG54IDwtIHNlcSgwLDIqcGksMC4xKVxueSA8LSBzaW4oeClcblxuIyBQbG90IHRoZSBkYXRhXG5wbG90KHgsXG4gICAgIHksXG4gICAgIHhsYWIgPSBleHByZXNzaW9uKHBhc3RlKFwiR3JlZWsgbGV0dGVyIFwiLCBwaGkpKSxcbiAgICAgeWxhYiA9IGV4cHJlc3Npb24ocGFzdGUoXCJHcmVlayBsZXR0ZXIgXCIsbXUpKSkifQ==

Adjust The Appearance Of The Axes’ Labels

To adjust the appearance of the x-and y-axis labels, you can use the arguments col.lab and cex.lab. The first argument is used to change the color of the x-and y-axis labels, while the second argument is used to determine the size of the x-and y-axis labels, relative to the (default) setting of cex.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBkYXRhXG54IDwtIHNlcSgwLDIqcGksMC4xKVxueSA8LSBzaW4oeClcblxuIyBQbG90IHRoZSBkYXRhXG5wbG90KHgsXG4gICAgIHksXG4gICAgIG1haW49ZXhwcmVzc2lvbihcIm1haW4gdGl0bGVcIl4yKSwgXG4gICAgIHN1Yj1leHByZXNzaW9uKFwic3ViLXRpdGxlXCJbMl0pLCBcbiAgICAgeGxhYj1cIngtYXhpcyBsYWJlbFwiLCBcbiAgICAgeWxhYj1cInktYXhpcyBsYWJlbFwiLFxuICAgICBjb2wubGFiPVwiYmx1ZVwiLFxuICAgICBjZXgubGFiPTAuNzUpIn0=

Rplot2

For more information on these arguments, go to this page.

Remove A Plot’s Axis Labels And Annotations

If you want to get rid of the axis values of a plot, you can first add the arguments xaxt and yaxt, set as "n". These arguments are assigned a character which specifies the x-axis type. If you put in an "n", like in the command below, you can suppress the plotting of the axis.

Note that by giving any other character to the xaxt and yaxt arguments, the x-and y-axes are plotted.

Next, you can add the annotation argument ann and set it to FALSE to make sure that any axis labels are removed.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBkYXRhXG54IDwtIHNlcSgwLDIqcGksMC4xKVxueSA8LSBzaW4oeClcblxuIyBQbG90IHRoZSBkYXRhXG5wbG90KHgsXG4gICAgIHksXG4gICAgIHhheHQ9XCJuXCIsXG4gICAgIHlheHQ9XCJuXCIsXG4gICAgIGFubj1GQUxTRSkifQ==

Rplot3

Tip: not the information you are looking for? Go to this page.

Rotate A Plot’s Axis Labels

You can add the las argument to the axis() function to rotate the numbers that correspond to each axis:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBkYXRhXG54IDwtIHNlcSgwLDIqcGksMC4xKVxueSA8LSBzaW4oeClcblxuIyBQbG90IHRoZSBkYXRhLCBubyBheGVzXG5wbG90KHgsXG4gICAgIHksIFxuICAgICBheGVzPUZBTFNFKVxuXG4jIERyYXcgYSBib3hcbmJveCgpXG5cbiMgQWRkIChtb2RpZmllZCkgYXhlcyB0byB0aGUgcGxvdFxuYXhpcygyLCBcbiAgICAgbGFzPTIpXG5heGlzKDEsIFxuICAgICBsYXM9MCkifQ==

Rplot4

Note how this actually requires you to add an argument to the plot() function that basically says that there are no axes to be plotted (yet): this is the task of the two axis() function that come next.

The las argument can have three values attributed to it. According to whichever option you choose, the placement of the label will differ: if you choose 0, the label will always be parallel to the axis (which is the default); If you choose 1, the label will be put horizontally. Pick 2 if you want it to be perpendicular to the axis and 3 if you want it to be placed vertically.

But there is more. If you want to know more about the possibilities of the axis() function, keep on reading!

Move The Axis Labels Of Your R Plot

So, you want to move your axes’ labels around?

No worries, you can do this with the axis() function; As you may have noticed before in this tutorial, this function allows you to first specify where you want to draw the axes. Let’s say you want to draw the x-axis above the plot area and the y-axis to the right of it.

Remember that if you pass 1 or 2 to the axis() function, your axis will be drawn on the bottom and on the left of the plot area. Scroll a bit up to see an example of this in the previous piece of code!

This means that you will want to pass 3 and 4 to the axis() function:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBkYXRhXG54PC1zZXEoMCwyKnBpLDAuMSlcbnk8LXNpbih4KVxuXG4jIFBsb3QgdGhlIGRhdGEsIGJ1dCBkb24ndCBwbG90IGF4ZXMgb3IgYW5ub3RhdGlvbnNcbnBsb3QoeCxcbiAgICAgeSxcbiAgICAgYXhlcz1GQUxTRSwgXG4gICAgIGFubj1GQUxTRSlcblxuIyBEcmF3IHRoZSB4LWF4aXMgYWJvdmUgdGhlIHBsb3QgYXJlYVxuYXhpcygzKVxuXG4jIERyYXcgdGhlIHktYXhpcyB0byB0aGUgcmlnaHQgb2YgdGhlIHBsb3QgYXJlYVxuYXhpcyg0KVxuXG4jIERyYXcgYSBib3hcbmJveCgpIn0=

Rplot5

As you can see, at first, you basically plot x and y, but you leave out the axes and annotations. Then, you add the axes that you want to see and specify their location with respect to the plot.

The flexibility that the axis() function creates for you keeps on growing! Check out the next frequently asked question to see what else you can solve by using this basic R function.

Tip: go to the last question to see more on how to move around text in the axis labels with hjust and vjust!

3. How To Add And Change The Spacing Of The Tick Marks Of Your R Plot

How To Change The Spacing Of The Tick Marks Of Your R Plot

Letting R determine the tick marks of your plot can be quite annoying and there might come a time when you will want to adjust these.

1. Using The axis() Function To Determine The Tick Marks Of Your Plot

Consider the following piece of code:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERlZmluZSB0aGUgcG9zaXRpb24gb2YgdGljayBtYXJrc1xudjEgPC0gYygwLHBpLzIscGksMypwaS8yLDIqcGkpXG5cbiMgRGVmaW5lIHRoZSBsYWJlbHMgb2YgdGljayBtYXJrc1xudjIgPC0gYyhcIjBcIixcIlBpLzJcIixcIlBpXCIsXCIzKlBpLzJcIixcIjIqUGlcIilcblxuIyBUaGUgZGF0YVxueCA8LSBzZXEoMCwyKnBpLDAuMSlcbnkgPC0gc2luKHgpXG5cbiMgUGxvdCB0aGUgZGF0YVxucGxvdCh4LFxuICAgICB5LFxuICAgICB4YXh0ID0gXCJuXCIpXG5cbiMgQWRkIGFuIGF4aXMgdG8gdGhlIHBsb3QgXG5heGlzKHNpZGUgPSAxLCBcbiAgICAgYXQgPSB2MSwgXG4gICAgIGxhYmVscyA9IHYyLFxuICAgICB0Y2s9LS4wNSkifQ==

Rplot6

As you can see, you first define the position of the tick marks and their labels. Then, you draw your plot, specifying the x axis type as "n", suppressing the plotting of the axis.

Then, the real work starts:

  • The axis() function allows you to specify the side of the plot on which the axis is to be drawn. In this case, the argument is completed with 1, which means that the axis is to be drawn below. If the value was 2, the axis would be drawn on the left and if the value was 3 or 4, the axis would be drawn above or to the right, respectively;
  • The at argument allows you to indicate the points at which tick marks are to be drawn. In this case, you use the positions that were defined in V1;
  • Likewise, the labels that you want to use are the ones that were specified in V2;
  • You adjust the direction of the ticks through tck: by giving this argument a negative value, you specify that the ticks should appear below the axis.

Tip: try passing a positive value to the tck argument and see what happens!

You can further specify the size of the ticks through tcl and the appearance of the tick labels is controlled with cex.axis, col.axis and font.axis.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERlZmluZSB0aGUgcG9zaXRpb24gb2YgdGljayBtYXJrc1xudjEgPC0gYygwLHBpLzIscGksMypwaS8yLDIqcGkpXG5cbiMgRGVmaW5lIHRoZSBsYWJlbHMgb2YgdGljayBtYXJrc1xudjIgPC0gYyhcIjBcIixcIlBpLzJcIixcIlBpXCIsXCIzKlBpLzJcIixcIjIqUGlcIilcblxuIyBUaGUgZGF0YVxueCA8LSBzZXEoMCwyKnBpLDAuMSlcbnkgPC0gc2luKHgpXG5cbiMgUGxvdCB0aGUgZGF0YVxucGxvdCh4LFxuICAgICB5LFxuICAgICB4YXh0ID0gXCJuXCIpXG5cbiMgQWRkIGF4aXMgdG8gdGhlIHBsb3QgXG5heGlzKHNpZGUgPSAxLCBcbiAgICAgYXQgPSB2MSwgXG4gICAgIGxhYmVscyA9IHYyLFxuICAgICB0Y2s9LS4xLFxuICAgICB0Y2wgPSAtMC41LFxuICAgICBjZXguYXhpcz0xLjA1LFxuICAgICBjb2wuYXhpcz1cImJsdWVcIixcbiAgICAgZm9udC5heGlzPTUpIn0=

Rplot7

2. Using Other Functions To Determine The Tick Marks Of Your R Plot

You can also use the par() and plot() functions to define the positions of tickmarks and the number of intervals between them.

Note that then you use the argument xaxp to which you pass the position of the tick marks that are located at the extremes of the x-axis, followed by the number of intervals between the tick marks:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBkYXRhXG54IDwtIHNlcSgwLDIqcGksMC4xKVxueSA8LSBzaW4oeClcblxuIyBQbG90IHRoZSBkYXRhXG5wbG90KHgsXG4gICAgIHksXG4gICAgIHhheHAgPSBjKDAsIDIqcGksIDUpKSJ9

Rplot8

You can also run the following code to basically get the same result:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBkYXRhIFxueCA8LSBzZXEoMCwyKnBpLDAuMSlcbnkgPC0gc2luKHgpXG5cbiMgUGxvdCB0aGUgZGF0YSBcbnBsb3QoeCwgXG4gICAgIHksIFxuICAgICB4YXh0PVwiblwiKVxuXG4jIFVzZSBgcGFyKClgIHRvIHNldCBncmFwaGljYWwgYHhheHBgIHBhcmFtZXRlclxucGFyKHhheHA9IGMoMCwgMipwaSwgMikpIFxuXG4jIEFkZCBhbiBheGlzIHRvIHRoZSBwbG90IFxuYXhpcygxKSJ9

Rplot8b

Note that this works only if you use no logarithmic scale. If the log coordinates set as true, or, in other words, if par(xlog=T), the three values that you pass to xaxp have a different meaning: for a small range, n is negative. Otherwise, n is in 1:3, specifying a case number, and x1 and x2 are the lowest and highest power of 10 inside the user coordinates, 10 ^ par(“usr”)[1:2].

An example:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJuIDwtIDFcblxuIyBUaGUgZGF0YVxueCA8LSBzZXEoMCwgMTAwMDAsIDEpXG55IDwtIGV4cChuKS8oZXhwKG4pK3gpXG5cbiMgU2V0IGdyYXBoaWNhbCBwYXJhbWV0ZXJzIGB4bG9nYCBhbmQgYHhheHBgXG5wYXIoeGxvZz1UUlVFLCBcbiAgICB4YXhwPSBjKDEsIDQsIDMpKVxuXG4jIFBvdCB0aGUgZGF0YSBcbnBsb3QoeCwgXG4gICAgIHksIFxuICAgICBsb2c9XCJ4XCIpICJ9

Rplot9

In this example, you use the par() function: you set xlog to TRUE and add the xaxp argument to give the coordinates of the extreme tick marks and the number of intervals between them. In this case, you set the minimal value to 1, the maximal value to 4 and you add that the number of intervals between each tick mark should be 3.

Then, you plot x and y, adding the log argument to specify whether to plot the x-or y-axis or both on a log scale. You can pass "x", "y", and "xy" as values to the log arguments to do this.

An example with both axes in logarithmic scale is:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJuIDwtIDFcblxuIyBUaGUgZGF0YVxueCA8LSBzZXEoMCwgMjAsIDEpXG55IDwtIGV4cCh4KS8oeClcblxuIyBTZXQgZ3JhcGhpY2FsIHBhcmFtZXRlcnMgYHhsb2dgIGFuZCBgeGF4cGBcbnBhcih4bG9nPVRSVUUsIFxuICAgIHhheHA9IGMoMSwgNCwgMykpXG4jIFNldCBncmFwaGljYWwgcGFyYW1ldGVycyBgeWxvZ2AgYW5kIGB5YXhwYFxucGFyKHlsb2c9VFJVRSwgXG4gICAgeWF4cD0gYygxLCAxMSwgMikpIFxuXG4jIFBsb3QgdGhlIGRhdGEgXG5wbG90KHgsIFxuICAgICB5LCBcbiAgICAgbG9nPVwieHlcIikifQ==

Rplot10

How To Add Minor Tick Marks To An R Plot

You can quickly add minor tick marks to your plot with the minor.tick() function from the Hmisc package:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIEltcG9ydCBgSG1pc2NgIGxpYnJhcnlcbmxpYnJhcnkoSG1pc2MpXG5cbiMgTWFrZSBhIG5ldyBwbG90XG5wbG90Lm5ldygpXG5cbiMgU2V0IG1pbm9yIHRpY2sgbWFya1xubWlub3IudGljayhueCA9IDEuNSwgXG4gICAgICAgICAgIG55ID0gMiwgXG4gICAgICAgICAgIHRpY2sucmF0aW89MC43NSkifQ==
  • The nx argument allows you to specify the number of intervals in which you want to divide the area between the major tick marks on the axis. If you pass the value 1 to it, the minor tick marks will be suppressed;
  • ny allows you to do the same as nx, but then for the y-axis;
  • The tick.ratio indicates the ratio of lengths of minor tick marks to major tick marks. The length of the latter is retrieved from par(tck=x).

4. How To Create Two Different X- or Y-axes

The first option is to create a first plot and to execute the par() function with the new argument put to TRUE to prevent R from clearing the graphics device:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFNldCBhIHNlZWQgXG5zZXQuc2VlZCgxMDEpXG5cbiMgVGhlIGRhdGFcbnggPC0gMToxMFxueSA8LSBybm9ybSgxMClcbnogPC0gcnVuaWYoMTAsIG1pbj0xMDAwLCBtYXg9MTAwMDApXG5cbiMgUGxvdCB0aGUgZGF0YVxucGxvdCh4LCB5KSBcblxuIyBEb24ndCBjbGVhbiB0aGUgZnJhbWUgYmVmb3JlIGRyYXdpbmdcbnBhcihuZXcgPSBUUlVFKSJ9

Then, you create the second plot with plot(). Pass in the data, and then also make sure that you adjust some of the arguments, such as type, axes, bty, xlab and ylab:

  • type: you can pass in l to make sure that you plot with lines;
  • axes is set to FALSE so that no axes are plotted;
  • bty suppresses the drawing of the box around the plot;
  • xlab is set to an empty string, which makes that there is no label on the x-axis, and
  • ylab is also set to an empty string, so the y-axis also has no label.

Check it all out in the following DataCamp Light chunk. Note that the code from above has already been loaded in into your working environment. You can double check by using the R Console on the right-hand side of the DataCamp Light box:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6InNldC5zZWVkKDEwMSlcbnggPC0gMToxMFxueSA8LSBybm9ybSgxMClcbnogPC0gcnVuaWYoMTAsIG1pbj0xMDAwLCBtYXg9MTAwMDApXG5wbG90KHgsIHkpIFxucGFyKG5ldyA9IFRSVUUpIiwic2FtcGxlIjoicGxvdCh4LCBcbiAgICAgeiwgXG4gICAgIHR5cGUgPSBcImxcIiwgXG4gICAgIGF4ZXMgPSBGQUxTRSxcbiAgICAgYnR5ID0gXCJuXCIsIFxuICAgICB4bGFiID0gXCJcIiwgXG4gICAgIHlsYWIgPSBcIlwiKSJ9

Next, it’s time to add an axis and to write text into the margins of the plot with axis() and mtext() respectively.

You can add a new axis on the right-hand side by adding the argument side and assigning it the value 4 to the axis() function. Next, you specify the the at argument to indicate the points at which tick-marks need to be drawn. In this case, you compute a sequence of n+1 equally spaced “round” values which cover the range of the values in z with the pretty() function. This ensures that you actually have the numbers from the y-axis from your second plot, which you named z.

Try all of this out below:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6InNldC5zZWVkKDEwMSlcbnggPC0gMToxMFxueSA8LSBybm9ybSgxMClcbnogPC0gcnVuaWYoMTAsIG1pbj0xMDAwLCBtYXg9MTAwMDApXG5wbG90KHgsIHkpIFxucGFyKG5ldyA9IFRSVUUpXG5wbG90KHgsIFxuICAgICB6LCBcbiAgICAgdHlwZSA9IFwibFwiLCAjUGxvdCB3aXRoIGxpbmVzXG4gICAgIGF4ZXMgPSBGQUxTRSwgI05vIGF4ZXNcbiAgICAgYnR5ID0gXCJuXCIsICNCb3ggYWJvdXQgcGxvdCBpcyBzdXBwcmVzc2VkXG4gICAgIHhsYWIgPSBcIlwiLCAgI05vIGxhYmVscyBvbiB4LWFuZCB5LWF4aXNcbiAgICAgeWxhYiA9IFwiXCIpIiwic2FtcGxlIjoiYXhpcyhzaWRlPTQsIFxuICAgICBhdCA9IHByZXR0eShyYW5nZSh6KSkpXG5tdGV4dChcInpcIiwgXG4gICAgICBzaWRlPTQsIFxuICAgICAgbGluZT0zKSJ9

Note that the side argument can have three values assigned to it: 1 to place the text to the bottom, 2 for a left placement, 3 for a top placement and 4 to put the text to the right. The line argument indicates on which margin line the text starts.

The total end result of your code will look like this:

set.seed(101)
x <- 1:10
y <- rnorm(10)
z <- runif(10, min=1000, max=10000)
plot(x, y) 
par(new = TRUE)
plot(x, 
     z, 
     type = "l", 
     axes = FALSE, 
     bty = "n", 
     xlab = "", 
     ylab = "")
axis(side=4, 
     at = pretty(range(z)))
mtext("z", 
      side=4, 
      line=3)

Rplot11

As an additional exercise, try constructing an R plot with two different x-axes! Do this in this DataCamp Light chunk below:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJwbG90Lm5ldygpXG5zZXQuc2VlZCgxMDEpXG54IDwtIDE6MTBcbnkgPC0gcm5vcm0oMTApXG56IDwtIHJ1bmlmKDEwLCBtaW49MTAwMCwgbWF4PTEwMDAwKSBcbnBhcihtYXIgPSBjKDUsIDQsIDQsIDQpICsgMC4zKVxucGxvdCh4LCB5KVxucGFyKG5ldyA9IFRSVUUpXG5wbG90KHosIHksIHR5cGUgPSBcImxcIiwgYXhlcyA9IEZBTFNFLCBidHkgPSBcIm5cIiwgeGxhYiA9IFwiXCIsIHlsYWIgPSBcIlwiKVxuYXhpcyhzaWRlPTMsIGF0ID0gcHJldHR5KHJhbmdlKHopKSlcbm10ZXh0KFwielwiLCBzaWRlPTMsIGxpbmU9MykifQ==

Rplot12

Note that, even though you can do all this manually with the basic plot(), axis() and mtext() functions, there are packages that automate all of this: the twoord.plot() function of the plotrix package and doubleYScale() function of the latticeExtra package are quite handy and perhaps a bit faster if you want to do the same thing:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIEltcG9ydCBgbGF0dGljZUV4dHJhYFxubGlicmFyeShsYXR0aWNlRXh0cmEpXG5cbiMgVGhlIGRhdGFcbmNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSlcbkFnZSA8LSBjaG9sJEFHRVxuQ2hvbCA8LSBjaG9sJENIT0xcblNtb2tlIDwtIGNob2wkU01PS0VcblN0YXRlIDwtIGNob2wkTU9SVFxuXG4jIENyZWF0ZSBhIGJpdmFyaWF0ZSBzY2F0dGVycGxvdCBgYWAgXG5hIDwtIHh5cGxvdChDaG9sIH4gQWdlfFN0YXRlKVxuXG4jIENyZWF0ZSBhIGJpdmFyaWF0ZSBzY2F0dGVycGxvdCBgYmAgXG5iIDwtIHh5cGxvdChTbW9rZSB+IEFnZXxTdGF0ZSlcblxuIyBEcmF3IHRoZSB0d28gcGxvdHMgd2l0aCBkaWZmZXJlbnQgeSBzY2FsZXMgXG5kb3VibGVZU2NhbGUoYSwgYiwgc3R5bGUxID0gMCwgc3R5bGUyID0gMywgYWRkLnlsYWIyID0gVFJVRSwgY29sdW1ucz0zKSJ9

Rplot13

Note that the example above is made with this dataset. If you have any doubts about the importing of the data or the read.table() function, check out DataCamp’s tutorial on importing data in R.

5. How To Add Or Change The R Plot’s Legend?

Adding And Changing An R Plot’s Legend With Basic R

You can easily add a legend to your R plot with the legend() function:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBkYXRhXG54IDwtIHNlcSgwLHBpLDAuMSlcbnkxIDwtIGNvcyh4KVxueTIgPC0gc2luKHgpXG5cbiMgUGxvdCBcbnBsb3QoYygwLDMpLCBjKDAsMyksIHR5cGU9XCJuXCIsIHhsYWI9XCJ4XCIsIHlsYWI9XCJ5XCIpXG5cbiMgRHJhdyBsaW5lc1xubGluZXMoeCwgeTEsIGNvbD1cInJlZFwiLCBsd2Q9MilcbmxpbmVzKHgsIHkyLCBjb2w9XCJibHVlXCIsIGx3ZD0yKVxuXG4jIEFkZCBsZWdlbmQgdG8gcGxvdCBcbmxlZ2VuZChcInRvcHJpZ2h0XCIsIFxuICAgICAgIGluc2V0PS4wNSwgXG4gICAgICAgY2V4ID0gMSwgXG4gICAgICAgdGl0bGU9XCJMZWdlbmRcIiwgXG4gICAgICAgYyhcIkNvc2ludXNcIixcIlNpbnVzXCIpLCBcbiAgICAgICBob3Jpej1UUlVFLCBcbiAgICAgICBsdHk9YygxLDEpLCBcbiAgICAgICBsd2Q9YygyLDIpLCBcbiAgICAgICBjb2w9YyhcInJlZFwiLFwiYmx1ZVwiKSwgXG4gICAgICAgYmc9XCJncmV5OTZcIikifQ==

Rplot14

Note that the arguments pt.cex and title.cex that are described in the documentation of legend() don’t really work. There are some workarounds:

  1. Put the title or the labels of the legend in a different font by adding the text.font argument to legend():
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBkYXRhXG54IDwtIHNlcSgw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AgICBjZXggPSAwLjc1LFxuICAgICAgIGMoXCJDb3NpbnVzXCIsXCJTaW51c1wiKSxcbiAgICAgICB0aXRsZT1cIlwiLFxuICAgICAgIGhvcml6PVRSVUUsXG4gICAgICAgbHR5PWMoMSwxKSwgXG4gICAgICAgbHdkPWMoMiwyKSwgXG4gICAgICAgY29sPWMoXCJyZWRcIixcImJsdWVcIikpXG5cbiMgQWRkIHRpdGxlIHRvIGxlZ2VuZCBcbmxlZ2VuZCgyLjA1LCAyLjk3LCBcbiAgICAgICBpbnNldD0uMDUsXG4gICAgICAgYyhcIlwiLFwiXCIpLFxuICAgICAgIHRpdGxlPVwiTGVnZW5kXCIsXG4gICAgICAgY2V4PTEuMTUsIFxuICAgICAgIGJ0eT1cIm5cIikifQ==

Rplot16

Tip: if you’re interested in knowing more about the colors that you can use in R, check out this very helpful PDF document.

How To Add And Change An R Plot’s Legend And Labels In ggplot2

Adding a legend to your ggplot2 plot is fairly easy. You can just execute the following:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIExvYWQgaW4gYGdncGxvdDJgXG5saWJyYXJ5KGdncGxvdDIpXG5cbiMgUmVhZCBpbiB0aGUgZGF0YVxuY2hvbCA8LSByZWFkLnRhYmxlKHVybChcImh0dHA6Ly9hc3NldHMuZGF0YWNhbXAuY29tL2Jsb2dfYXNzZXRzL2Nob2wudHh0XCIpLCBcbiAgICAgICAgICAgICAgICAgICBoZWFkZXIgPSBUUlVFKVxuXG4jIFBsb3QgdGhlIGRhdGEgXG5nZ3Bsb3QoY2hvbCwgYWVzKHg9Y2hvbCRXRUlHSFQsIFxuICAgICAgICAgICAgICAgICB5PWNob2wkSEVJR0hUKSkgKyBcbiAgZ2VvbV9wb2ludChhZXMoY29sb3VyID0gZmFjdG9yKGNob2wkTU9SVCksIFxuICAgICAgICAgICAgICAgICBzaGFwZT1jaG9sJFNNT0tFKSkgKyBcbiAgeGxhYihcIldlaWdodFwiKSArIFxuICB5bGFiKFwiSGVpZ2h0XCIpICJ9

Rplot17

And it gives you a default legend. But, in most cases, you will want to adjust the appearance of the legend some more.

There are two ways of changing the legend title and labels in ggplot2:

  1. Tell the scale to use have a different title and labels
If you have specified arguments such as colour or shape, or other aesthetics, you need to change the names and labels through the scale_color_discrete() and scale_shape_discrete() functions, respectively:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIExvYWQgaW4gYGdncGxvdDJgXG5saWJyYXJ5KGdncGxvdDIpXG5cbiMgUmVhZCBpbiBkYXRhXG5jaG9sIDwtIHJlYWQudGFibGUodXJsKFwiaHR0cDovL2Fzc2V0cy5kYXRhY2FtcC5jb20vYmxvZ19hc3NldHMvY2hvbC50eHRcIiksIFxuICAgICAgICAgICAgICAgICAgIGhlYWRlciA9IFRSVUUpXG5cbiMgUGxvdCB0aGUgZGF0YVxuZ2dwbG90KGNob2wsIGFlcyh4PWNob2wkV0VJR0hULCBcbiAgICAgICAgICAgICAgICAgeT1jaG9sJEhFSUdIVCkpICsgXG4gIGdlb21fcG9pbnQoYWVzKGNvbG91ciA9IGZhY3RvcihjaG9sJE1PUlQpLCBcbiAgICAgICAgICAgICAgICAgc2hhcGUgPSBjaG9sJFNNT0tFKSkgKyBcbiAgeGxhYihcIldlaWdodFwiKSArIFxuICB5bGFiKFwiSGVpZ2h0XCIpICsgXG4gIHRoZW1lKGxlZ2VuZC5wb3NpdGlvbj1jKDEsMC41KSxcbiAgICAgICAgbGVnZW5kLmp1c3RpZmljYXRpb249YygxLDEpKSArIFxuICBzY2FsZV9jb2xvcl9kaXNjcmV0ZShuYW1lID1cIkNvbmRpdGlvblwiLCBcbiAgICAgICAgICAgICAgICAgICAgICAgbGFiZWxzPWMoXCJBbGl2ZVwiLCBcIkRlYWRcIikpICtcbiAgc2NhbGVfc2hhcGVfZGlzY3JldGUobmFtZT1cIlNtb2tlclwiLCBcbiAgICAgICAgICAgICAgICAgICAgICAgbGFiZWxzPWMoXCJOb24tc21va2VyXCIsIFwiU2lnYXJlXCIsIFwiUGlwZVwiICkpIn0=

Note that the plot looks a bit squished in the Plot tab of the DataCamp Light chunk, but that this wouldn’t be a problem if you execute the code in RStudio, for example:

Rplot18

Note that you can create two legends if you add the argument shape into the geom_point() function and into the labels arguments!

If you want to move the legend to the bottom of the plot, you can specify the legend.position as "bottom". The legend.justification argument, on the other hand, allows you to position the legend inside the plot.

Tip: check out all kinds of scales that could be used to let ggplot know that other names and labels should be used here.

  1. Change the data frame so that the factor has the desired form
For example:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFJlYWQgaW4gZGF0YVxuY2hvbCA8LSByZWFkLnRhYmxlKHVybChcImh0dHA6Ly9hc3NldHMuZGF0YWNhbXAuY29tL2Jsb2dfYXNzZXRzL2Nob2wudHh0XCIpLCBoZWFkZXIgPSBUUlVFKVxuXG4jIE1hbmlwdWxhdGUgdGhlIGRhdGFcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJTaWdhcmVcIl0gPC0gXCJTaWdhcmVcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJQaXBlXCJdIDwtIFwiUGlwZVwiXG5uYW1lcyhjaG9sKVtuYW1lcyhjaG9sKT09XCJTTU9LRVwiXSAgPC0gXCJTTU9LRVJcIlxuXG4jIEluc3BlY3QgcmVzdWx0IFxuaGVhZChjaG9sKSJ9

You can then use the new factor names to make your plot in ggplot2, avoiding the “hassle” of changing the names and labels with extra lines of code in your plotting.

Tip: for a complete cheat sheet on ggplot2, you can go here.

6. How To Draw A Grid In Your R Plot?

Drawing A Grid In Your R Plot With Basic R

For some purposes, you might find it necessary to include a grid in your plot. You can easily add a grid to your plot by using the grid() function:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERhdGFcbnggPC0gYygxLDIsMyw0LDUpXG55IDwtIDIqeFxuXG4jIFBsb3RcbnBsb3QoeCx5KVxuXG4jIEdyaWRcbmdyaWQoMTAsMTApIn0=

Rplot19

Drawing A Grid In An R Plot With ggplot2

The code chunk below shows you how you can make sure that your plot has a grid with the ggplot2 package. Note that the chol data has already been loaded in and that the data manipulation steps from above have been applied to it.

If you’re not sure what the data looks like, print out the first rows of the data frame in the R Console of the DataCamp Light chunk:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJTaWdhcmVcIl0gPC0gXCJTaWdhcmVcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJQaXBlXCJdIDwtIFwiUGlwZVwiXG5uYW1lcyhjaG9sKVtuYW1lcyhjaG9sKT09XCJTTU9LRVwiXSAgPC0gXCJTTU9LRVJcIiIsInNhbXBsZSI6IiMgTG9hZCBpbiBgZ2dwbG90MmBcbmxpYnJhcnkoZ2dwbG90MilcblxuIyBQbG90IGRhdGEgXG5nZ3Bsb3QoY2hvbCwgYWVzKHg9Y2hvbCRXRUlHSFQsIHk9Y2hvbCRIRUlHSFQpKSArIFxuICBnZW9tX3BvaW50KGFlcyhjb2xvdXIgPSBmYWN0b3IoY2hvbCRNT1JUKSwgc2hhcGUgPSBjaG9sJFNNT0tFKSkgKyBcbiAgeGxhYihcIldlaWdodFwiKSArIFxuICB5bGFiKFwiSGVpZ2h0XCIpICsgXG4gIHRoZW1lKHBhbmVsLmdyaWQubWFqb3IgPSBlbGVtZW50X2xpbmUoY29sb3VyID0gXCJncmV5NDBcIiksXG4gICAgICAgIHBhbmVsLmdyaWQubWlub3IgPSBlbGVtZW50X2xpbmUoY29sb3VyID0gXCJncmV5NDBcIikpIn0=

Rplot20

Tip: if you don’t want to have the minor grid lines, just pass element_blank() to panel.grid.minor.

If you want to fill the background up with a color, add the panel.background = element_rect(fill = "navy") to your code. You can see how this is done in the DataCamp Light block below. The chol data has been loaded and lightly manipulated, just like before. Try it out for yourself!

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJTaWdhcmVcIl0gPC0gXCJTaWdhcmVcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJQaXBlXCJdIDwtIFwiUGlwZVwiXG5uYW1lcyhjaG9sKVtuYW1lcyhjaG9sKT09XCJTTU9LRVwiXSAgPC0gXCJTTU9LRVJcIiIsInNhbXBsZSI6ImxpYnJhcnkoZ2dwbG90MilcbmdncGxvdChjaG9sLCBhZXMoeD1jaG9sJFdFSUdIVCwgeT1jaG9sJEhFSUdIVCkpICsgXG4gIGdlb21fcG9pbnQoYWVzKGNvbG91ciA9IGZhY3RvcihjaG9sJE1PUlQpLCBzaGFwZSA9IGNob2wkU01PS0VSKSkgKyBcbiAgeGxhYihcIldlaWdodFwiKSArIFxuICB5bGFiKFwiSGVpZ2h0XCIpICsgXG4gIHNjYWxlX2NvbG9yX2Rpc2NyZXRlKG5hbWUgPVwiQ29uZGl0aW9uXCIsIGxhYmVscz1jKFwiQWxpdmVcIiwgXCJEZWFkXCIpKSArXG4gIHNjYWxlX3NoYXBlX2Rpc2NyZXRlKG5hbWU9XCJTbW9rZXJcIiwgbGFiZWxzPWMoXCJOb24tc21va2VyXCIsIFwiU2lnYXJlXCIsIFwiUGlwZVwiICkpICtcbiAgdGhlbWUobGVnZW5kLnBvc2l0aW9uPWMoMSwwLjUpLFxuICAgICAgICBsZWdlbmQuanVzdGlmaWNhdGlvbj1jKDEsMSksXG4gICAgICAgIHBhbmVsLmdyaWQubWFqb3IgPSBlbGVtZW50X2xpbmUoY29sb3VyID0gXCJncmV5NDBcIiksXG4gICAgICAgIHBhbmVsLmdyaWQubWlub3IgPSBlbGVtZW50X2xpbmUoY29sb3VyID0gXCJncmV5NDBcIiksXG4gICAgICAgIHBhbmVsLmJhY2tncm91bmQgPSBlbGVtZW50X3JlY3QoZmlsbCA9IFwibmF2eVwiKVxuICAgICAgICApIn0=

7. How To Draw A Plot With A PNG As Background?

You can quickly draw a plot with a .png as a background with the help of the png package. You install the package if you need to, activate it for use in your workspace through the library function library() and you can start plotting!

First, you want to load in the image. Use the readPNG() function to specify the path to the picture!

tip: you can check where your working directory is set at and change it by executing the following commands.

If your picture is saved in your working directory, you can just specify readPNG("picture.png") instead of passing the whole path.

Next, you want to set up the plot area and you want to call the par() function to set the graphical parameters in rasterImage(). You use the argument usr to define the extremes of the user coordinates of the plotting region. In this case, you put 1, 3, 2 and 4.

Next, you draw a grid and add some lines:

install.packages("png")
library(png)
image <- readPNG("<path to your picture>")

getwd()
setwd("<path to a folder>")

plot(1:2, type='n', main="Plotting Over an Image", xlab="x", ylab="y")

lim <- par()

rasterImage(image, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])

grid()
lines(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="red")

This can give you the following result if you use the DataCamp logo:

library(png)
image <- readPNG("datacamp.png")
plot(1:2, type="n", main="Plotting Over an Image", xlab="x", ylab="y", asp=1)
lim <- par()
rasterImage(image, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
lines(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1.5, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="red")

Rplot21

Note that you need to give a .png file as input to readPNG() and that, if you would like to, you can also follow the same procedure to work with jpg images. Of course, you then need to load in the jpeg library!

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERvd25sb2FkIGFuIGltYWdlXG5kb3dubG9hZC5maWxlKFwiaHR0cHM6Ly9ob2x0enlhbi5maWxlcy53b3JkcHJlc3MuY29tLzIwMTUvMTAvY3JvcHBlZC1wMTA5MDUxOC5qcGdcIiAsIGRlc3RmaWxlPVwibXlfaW1hZ2UuanBnXCIpXG5cbiMgTG9hZCBganBlZ2AgXG5saWJyYXJ5KGpwZWcpXG5cbiMgUmVhZCBpbiB0aGUganBnIGZpbGUgXG5teV9pbWFnZT1yZWFkSlBFRyhcIm15X2ltYWdlLmpwZ1wiKVxuXG4jIFNldCB1cCBhIHBsb3QgYXJlYSB3aXRoIG5vIHBsb3RcbnBsb3QoMToyLCB0eXBlPSduJywgbWFpbj1cIlwiLCB4bGFiPVwieFwiLCB5bGFiPVwieVwiKVxuXG4jIEdldCB0aGUgcGxvdCBpbmZvcm1hdGlvblxubGltIDwtIHBhcigpXG5yYXN0ZXJJbWFnZShteV9pbWFnZSwgbGltJHVzclsxXSwgbGltJHVzclszXSwgbGltJHVzclsyXSwgbGltJHVzcls0XSlcbmdyaWQoKVxuXG4jQWRkIHlvdXIgcGxvdFxubGluZXMoYygxLCAxLjIsIDEuNCwgMS42LCAxLjgsIDIuMCksIGMoMSwgMS4zLCAxLjcsIDEuNiwgMS43LCAxLjApLCB0eXBlPVwiYlwiLCBsd2Q9NSwgY29sPVwid2hpdGVcIikifQ==

Note that in this example, you first download an image from the web to use in your plot, but that you can also just save an image and pass the file path to readJPEG() to make use of it during plotting.

Also, note that this example was taken from the R Graph Gallery, which offers a ton of inspiration for R data visualization, as you can find the code and the renderings in one space! It’s ideal if you’re looking for inspiration or a quick way to find the code you need for your graphs!

8. How To Adjust The Size Of Points In An R Plot?

Adjusting The Size Of Points In An R Plot With Basic R

To adjust the size of the points with basic R, you might just simply use the cex argument:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJ4IDwtIGMoMSwyLDMsNCw1KVxueSA8LSBjKDYsNyw4LDksMTApXG5cbnBsb3QoeCx5LGNleD0yLGNvbD1cInJlZFwiKSJ9
Remember, however, that that R allows you to have much more control over your symbols through the function symbols():
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJkZiA8LSBkYXRhLmZyYW1lKHgxPTE6MTAsIFxuICAgICAgICAgICAgICAgICB4Mj1zYW1wbGUoMTA6OTksIDEwKSwgXG4gICAgICAgICAgICAgICAgIHgzPTEwOjEpXG5zeW1ib2xzKHg9ZGYkeDEsIFxuICAgICAgICB5PWRmJHgyLCBcbiAgICAgICAgY2lyY2xlcz1kZiR4MywgXG4gICAgICAgIGluY2hlcz0xLzMsIFxuICAgICAgICBhbm49RiwgXG4gICAgICAgIGJnPVwic3RlZWxibHVlMlwiLCBcbiAgICAgICAgZmc9TlVMTCkifQ==

The circles of this plot receive the values of df$x3 as the radii, while the argument inches controls the size of the symbols. When this argument receives a positive number as input, the symbols are scaled to make largest dimension this size in inches.

Adjusting The Size Of Points In Your R Plot With ggplot2

In this case, you will want to adjust the size of the points in your scatterplot. You can do this with the size argument. Note that the chol data has been loaded in for you:
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgXG4gICAgICAgICAgICAgICAgICAgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJTaWdhcmVcIl0gPC0gXCJTaWdhcmVcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJQaXBlXCJdIDwtIFwiUGlwZVwiXG5uYW1lcyhjaG9sKVtuYW1lcyhjaG9sKT09XCJTTU9LRVwiXSAgPC0gXCJTTU9LRVJcIiIsInNhbXBsZSI6ImxpYnJhcnkoZ2dwbG90MilcblxuZ2dwbG90KGNob2wsIFxuICAgICAgIGFlcyh4PWNob2wkV0VJR0hULCB5PWNob2wkSEVJR0hUKSwgXG4gICAgICAgc2l6ZSA9IDIpICsgXG4gIGdlb21fcG9pbnQoKSJ9

Note that you can also pass the size argument to the geom_point() function:

ggplot(chol, 
       aes(x=chol$WEIGHT, y=chol$HEIGHT)) + 
  geom_point(size = 2)

You’ll get the same result, but don’t take my word for it! Try replacing the last piece of code in the DataCamp Light chunk with this chunk.

9. How To Fit A Smooth Curve To Your R Data

The loess() function is probably every R programmer’s favorite solution for this kind of question. It actually “fits a polynomial surface determined by one or more numerical predictors, using local fitting”.

In short, you have your data and you use the loess() function, in which you correlate y and x. Through this, you specify the numeric response and one to four numeric predictors.

Next, you plot x and y and you plot lines in the original plot where you predicted the values of lo:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJ4IDwtIDE6MTBcbnkgPC0gYygyLDQsNiw4LDcsMTIsMTQsMTYsMTgsMjApXG5cbiNFc3RpbWF0aW9ucyBiZXR3ZWVuIGRhdGFcbmxvIDwtIGxvZXNzKHl+eClcblxucGxvdCh4LHkpXG5cbmxpbmVzKHByZWRpY3QobG8pKSJ9

This gives you the following result:

Rplot22

10. How To Add Error Bars In An R Plot

Drawing Error Bars With Basic R

The bad news: R can’t draw error bars just like that. The good news: you can still draw the error bars without needing to install extra packages!

First, you’ll need to define the following function to calculate some summary statistics of your data frame:

summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
    library(plyr)

    # New version of length which can handle NA's: if na.rm==T, don't count them
    length2 <- function (x, na.rm=FALSE) {
        if (na.rm) sum(!is.na(x))
        else       length(x)
    }

    # This does the summary. For each group's data frame, return a vector with
    # N, mean, and sd
    datac <- ddply(data, groupvars, .drop=.drop,
      .fun = function(xx, col) {
        c(N    = length2(xx[[col]], na.rm=na.rm),
          mean = mean   (xx[[col]], na.rm=na.rm),
          sd   = sd     (xx[[col]], na.rm=na.rm)
        )
      },
      measurevar
    )

    # Rename the "mean" column    
    datac <- rename(datac, c("mean" = measurevar))

    datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean

    # Confidence interval multiplier for standard error
    # Calculate t-statistic for confidence interval: 
    # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
    ciMult <- qt(conf.interval/2 + .5, datac$N-1)
    datac$ci <- datac$se * ciMult

    return(datac)
}

Note that the function definition comes from this source and that it is also available through the Rmisc library.

Next, you can use this to calculate some statistics of the chol data, which has already been loaded in for you. After, you can use these resulting numbers to make your plot.

Make sure to inspect the data before you start this exercise!

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgXG4gICAgICAgICAgICAgICAgICAgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2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ZGF0YVxucGxvdChjaG9sYyROLCBcbiAgICAgY2hvbGMkQ0hPTCxcbiAgICAgeWxpbT1yYW5nZShjKGNob2xjJENIT0wtY2hvbGMkc2QsIFxuICAgICAgICAgICAgICAgICAgY2hvbGMkQ0hPTCtjaG9sYyRzZCkpLFxuICAgICBwY2g9MTksIFxuICAgICB4bGFiPVwiQ2hvbGVzdGVyb2wgTWVhc3VyZW1lbnRzXCIsIFxuICAgICB5bGFiPVwiQ2hvbGVzdGVyb2wgTWVhbiArLy0gU0RcIixcbiAgICAgbWFpbj1cIlNjYXR0ZXJwbG90IFdpdGggc2QgRXJyb3IgQmFyc1wiXG4pXG5cbiNEcmF3IGFycm93cyBvZiBhIFwic3BlY2lhbFwiIHR5cGVcbmFycm93cyhjaG9sYyROLCBcbiAgICAgICBjaG9sYyRDSE9MLWNob2xjJHNkLCBcbiAgICAgICBjaG9sYyROLCBcbiAgICAgICBjaG9sYyRDSE9MK2Nob2xjJHNkLCBcbiAgICAgICBsZW5ndGg9MC4wNSwgXG4gICAgICAgYW5nbGU9OTAsIFxuICAgICAgIGNvZGU9MykifQ==

If you want to read up on all the arguments that arrows() can take, go here.

Drawing Error Bars With ggplot2

Error Bars Representing Standard Error Of Mean

First summarize your data with the summarySE() function from the Rmisc package or use the function that has been defined above. Then, you can use the resulting dataframe to plot some of the variables, drawing error bars for them at the same time, with, for example, the standard error of mean.

For convenience, the data as well as the function have been loaded in for you:
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgXG4gICAgICAgICAgICAgICAgICAgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxub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vbGMgZGF0YXNldFxubGlicmFyeShnZ3Bsb3QyKVxuZ2dwbG90KGNob2xjLCBhZXMoeD1TTU9LRVIsIFxuICAgICAgICAgICAgICAgICAgeT1DSE9MLCBcbiAgICAgICAgICAgICAgICAgIGNvbG91cj1NT1JUKSkgKyBcbiAgICBnZW9tX2Vycm9yYmFyKGFlcyh5bWluPUNIT0wtc2UsIFxuICAgICAgICAgICAgICAgICAgICAgIHltYXg9Q0hPTCtzZSwgXG4gICAgICAgICAgICAgICAgICAgICAgZ3JvdXA9TU9SVCksIFxuICAgICAgICAgICAgICAgICAgd2lkdGg9LjEpICtcbiAgICBnZW9tX2xpbmUoYWVzKGdyb3VwPU1PUlQpKSArXG4gICAgZ2VvbV9wb2ludCgpIn0=

Rplot23

If you want to change the position of the error bars, for example, when they overlap, you might consider using the position_dodge() function:
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJTaWdhcmVcIl0gPC0gXCJTaWdhcmVcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJQaXBlXCJdIDwtIFwiUGlwZVwiXG5uYW1lcyhjaG9sKVtuYW1lcyhjaG9sKT09XCJTTU9LRVwiXSAgPC0gXCJTTU9LRVJcIlxuc3VtbWFyeVNFIDwtIGZ1bmN0aW9uKGRhdGE9TlVMTCwgbWVhc3VyZXZhciwgZ3JvdXB2YXJzPU5VTEwsIG5hLnJtPUZBTFNFLF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AgICAgICB3aWR0aD0uMSwgXG4gICAgICAgICAgICAgICAgICBwb3NpdGlvbj1wZCkgK1xuICAgIGdlb21fbGluZShhZXMoZ3JvdXA9TU9SVCkpICtcbiAgICBnZW9tX3BvaW50KCkifQ==

Rplot24

Tip: if you get an error like “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?”, it usually requires you to adjust the group aesthetic.

Error Bars Representing Confidence Intervals

Continuing from the summary of your data that you made with the summarySE() function of the Rmisc library or of the previous section, you can also draw error bars that represent confidence intervals. In this case, a plot with error bars of 95% confidence are plotted.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJTaWdhcmVcIl0gPC0gXCJTaWdhcmVcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJQaXBlXCJdIDwtIFwiUGlwZVwiXG5uYW1lcyhjaG9sKVtuYW1lcyhjaG9sKT09XCJTTU9LRVwiXSAgPC0gXCJTTU9LRVJcIlxuc3VtbWFyeVNFIDwtIGZ1bmN0aW9uKGRhdGE9TlVMTCwgbWVhc3VyZXZhciwgZ3JvdXB2YXJzPU5VTEwsIG5hLnJtPUZBTFNFLFxuICAgICAgICAgICAgICAgICAgICAgIGNvbmYuaW50ZXJ2YWw9Ljk1LCAuZHJvcD1UUlVFKSB7XG4gICAgbGlicmFyeShwbHlyKVxuXG4gICAgIyBOZXcgdmVyc2lvbiBvZiBsZW5ndGggd2hpY2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aXN0aWMgZm9yIGNvbmZpZGVuY2UgaW50ZXJ2YWw6IFxuICAgICMgZS5nLiwgaWYgY29uZi5pbnRlcnZhbCBpcyAuOTUsIHVzZSAuOTc1IChhYm92ZS9iZWxvdyksIGFuZCB1c2UgZGY9Ti0xXG4gICAgY2lNdWx0IDwtIHF0KGNvbmYuaW50ZXJ2YWwvMiArIC41LCBkYXRhYyROLTEpXG4gICAgZGF0YWMkY2kgPC0gZGF0YWMkc2UgKiBjaU11bHRcblxuICAgIHJldHVybihkYXRhYylcbn0iLCJzYW1wbGUiOiIjQ2FsY3VsYXRlIHNvbWUgc3RhdGlzdGljcyBmb3IgdGhlIGNob2wgZGF0YXNldFxuY2hvbGMgPC0gc3VtbWFyeVNFKGNob2wsIFxuICAgICAgICAgICAgICAgICAgIG1lYXN1cmV2YXI9XCJDSE9MXCIsIFxuICAgICAgICAgICAgICAgICAgIGdyb3VwdmFycz1jKFwiTU9SVFwiLFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFwiU01PS0VSXCIpKVxuXG4jUGxvdCB0aGUgY2hvbGMgZGF0YXNldFxubGlicmFyeShnZ3Bsb3QyKVxucGQgPC0gcG9zaXRpb25fZG9kZ2UoMC4xKVxuZ2dwbG90KGNob2xjLCBhZXMoeD1TTU9LRVIsIFxuICAgICAgICAgICAgICAgICAgeT1DSE9MLFxuICAgICAgICAgICAgICAgICAgY29sb3VyPU1PUlQpKSArIFxuICAgIGdlb21fZXJyb3JiYXIoYWVzKHltaW49Q0hPTC1jaSwgXG4gICAgICAgICAgICAgICAgICAgICAgeW1heD1DSE9MK2NpLCBcbiAgICAgICAgICAgICAgICAgICAgICBncm91cD1NT1JUKSwgXG4gICAgICAgICAgICAgICAgICB3aWR0aD0uMSwgXG4gICAgICAgICAgICAgICAgICBjb2xvdXI9XCJibGFja1wiLFxuICAgICAgICAgICAgICAgICAgcG9zaXRpb249cGQpICtcbiAgICBnZW9tX2xpbmUoYWVzKGdyb3VwPU1PUlQpKSArXG4gICAgZ2VvbV9wb2ludCgpIn0=

Rplot25

Note how the color of the error bars is now set to black with the colour argument.

Error Bars Representing The Standard Deviation

Lastly, you can also use the results of the summarySE() function of the Rmisc package or from the function definition that you have copied from one of the last sections to plot error bars that represent the standard deviation.

Specifically, you would just have to adjust the ymin and ymax arguments that you pass to geom_errorbar():

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJTaWdhcmVcIl0gPC0gXCJTaWdhcmVcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJQaXBlXCJdIDwtIFwiUGlwZVwiXG5uYW1lcyhjaG9sKVtuYW1lcyhjaG9sKT09XCJTTU9LRVwiXSAgPC0gXCJTTU9LRVJcIlxuc3VtbWFyeVNFIDwtIGZ1bmN0aW9uKGRhdGE9TlVMTCwgbWVhc3VyZXZhciwgZ3JvdXB2YXJzPU5VTEwsIG5hLnJtPUZBTFNFLFxuICAgICAgICAgICAgICAgICAgICAgIGNvbmYuaW50ZXJ2YWw9Ljk1LCAuZHJvcD1UUlVFKSB7XG4gICAgbGlicmFyeShwbHlyKVxuXG4gICAgIyBOZXcgdmVyc2lvbiBvZiBsZW5ndGggd2hpY2ggY2FuIGhhbmRsZSBOQSdzOiBpZiBuYS5ybT09VCwgZG9uJ3QgY291bnQgdGhlbVxuICAgIGxlbmd0aDIgPC0gZnVuY3Rpb24gKHgsIG5hLnJtPUZBTFNFKSB7X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IGFuZCB1c2UgZGY9Ti0xXG4gICAgY2lNdWx0IDwtIHF0KGNvbmYuaW50ZXJ2YWwvMiArIC41LCBkYXRhYyROLTEpXG4gICAgZGF0YWMkY2kgPC0gZGF0YWMkc2UgKiBjaU11bHRcblxuICAgIHJldHVybihkYXRhYylcbn0iLCJzYW1wbGUiOiIjQ2FsY3VsYXRlIHNvbWUgc3RhdGlzdGljcyBmb3IgdGhlIGNob2wgZGF0YXNldFxuY2hvbGMgPC0gc3VtbWFyeVNFKGNob2wsIFxuICAgICAgICAgICAgICAgICAgIG1lYXN1cmV2YXI9XCJDSE9MXCIsIFxuICAgICAgICAgICAgICAgICAgIGdyb3VwdmFycz1jKFwiTU9SVFwiLFwiU01PS0VSXCIpKVxuXG4jUGxvdCB0aGUgY2hvbGMgZGF0YXNldFxubGlicmFyeShnZ3Bsb3QyKVxucGQgPC0gcG9zaXRpb25fZG9kZ2UoMC4xKVxuZ2dwbG90KGNob2xjLCBhZXMoeD1TTU9LRVIsIFxuICAgICAgICAgICAgICAgICAgeT1DSE9MLCBcbiAgICAgICAgICAgICAgICAgIGNvbG91cj1NT1JUKSkgKyBcbiAgICBnZW9tX2Vycm9yYmFyKGFlcyh5bWluPUNIT0wtc2QsIFxuICAgICAgICAgICAgICAgICAgICAgIHltYXg9Q0hPTCtzZCwgXG4gICAgICAgICAgICAgICAgICAgICAgZ3JvdXA9TU9SVCksIFxuICAgICAgICAgICAgICAgICAgd2lkdGg9LjEsXG4gICAgICAgICAgICAgICAgICBwb3NpdGlvbj1wZCkgK1xuICAgIGdlb21fbGluZShhZXMoZ3JvdXA9TU9SVCkpICtcbiAgICBnZW9tX3BvaW50KCkifQ==

Big tip*: also take a look at this for more detailed examples on how to plot means and error bars.

11. How To Save A Plot As An Image On Disc

You can use dev.copy() to copy your graph, made in the current graphics device to the device or folder specified by yourself.

x <- seq(0,2*pi,0.1)
y <- sin(x)
plot(x,y)
dev.copy(jpeg,
         filename="<path to your file/name.jpg>");
dev.off();
(x)
(y)

12. How To Plot Two R Plots Next To Each Other?

Plot Two Plots Side By Side Using Basic R

You can do this with basic R commands:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERhdGFcbmQwIDwtIG1hdHJpeChybm9ybSgxNSksIG5jb2w9MylcbmQxIDwtIG1hdHJpeChybm9ybSgxNSksIG5jb2w9MylcblxuIyBTZXQgbGltaXRzIFxubGltaXRzIDwtIHJhbmdlKGQwLGQxKVxuXG4jIFNldCBncmFwaGljYWwgcGFyYW1ldGVyIGBtZnJvd2BcbnBhcihtZnJvdyA9IGMoMSwgMikpIFxuXG4jIE1ha2UgMiBib3hwbG90c1xuYm94cGxvdChkMCxcbiAgICAgICAgeWxpbT1saW1pdHMpXG5ib3hwbG90KGQxLFxuICAgICAgICB5bGltPWxpbWl0cykifQ==

By adding the par() function with the mfrow argument, you specify a vector, which in this case contains 1 and 2: all figures will then be drawn in a 1-by-2 array on the device by rows (mfrow).

In other words, the boxplots from above will be printed in one row inside two columns.

Plot Two Plots Next To Each Other Using ggplot2

If you want to put plots side by side and if you don’t want to specify limits, you can consider using the ggplot2 package to draw your plots side-by-side.

Your data chol and the summarySE() function have been loaded in. Inspect them in the R Console if you’re not sure what they look like:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJTaWdhcmVcIl0gPC0gXCJTaWdhcmVcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XC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5PUNIT0wsIFxuICAgICAgICAgICAgICAgICAgY29sb3VyPU1PUlQpKSArIFxuICBnZW9tX2Vycm9yYmFyKGFlcyh5bWluPUNIT0wtc2QsIFxuICAgICAgICAgICAgICAgICAgICB5bWF4PUNIT0wrc2QsIFxuICAgICAgICAgICAgICAgICAgICBncm91cD1NT1JUKSwgXG4gICAgICAgICAgICAgICAgd2lkdGg9LjEsIFxuICAgICAgICAgICAgICAgIHBvc2l0aW9uPXBkKSArIFxuICBnZW9tX2xpbmUoYWVzKGdyb3VwPU1PUlQpKSArIFxuICBnZW9tX3BvaW50KCkgKyBcbiAgZmFjZXRfZ3JpZCguIH4gTU9SVCkifQ==

Rplot26

Note how you just add the facet_grid() function to indicate that you want two plots next to each other. The element that is used to determine how the plots are drawn, is MORT, as you can well see above!

Plot More Plots Side By Side Using gridExtra

To get plots printed side by side, you can use the gridExtra package; Make sure you have the package installed and activated in your workspace and then execute something like this:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIExvYWQgaW4gYGdyaWRFeHRyYWAgYW5kIGBnZ3Bsb3QyYFxubGlicmFyeShncmlkRXh0cmEpXG5saWJyYXJ5KGdncGxvdDIpXG5cbiMgUGxvdFxucGxvdDEgPC0gcXBsb3QoMSlcbnBsb3QyIDwtIHFwbG90KDEpXG5cbiMgQXJyYW5nZSBgcGxvdDFgIGFuZCBgcGxvdDJgXG5ncmlkLmFycmFuZ2UocGxvdDEsIFxuICAgICAgICAgICAgIHBsb3QyLCBcbiAgICAgICAgICAgICBuY29sPTIpIn0=

Note how here again you determine how the two plots will appear to you thanks to the ncol argument.

Plot More Plots Side By Side Using lattice

Just like the solution with ggplot2 package, the lattice package also doesn’t require you to specify limits or the way you want your plots printed next to each other.

Instead, you use bwplot() to make trellis graphs with the graph type of a box plot. Trellis graphs display a variable or the relationship between variables, conditioned on one or more other variables.

In this case, if you’re using the chol data set (which you can find here or load in with the read.table() function, you display the variable CHOL separately for every combination of factor SMOKE and MORT levels.

Note that the data has been loaded in for you in the DataCamp Light chunk below!

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgXG4gICAgICAgICAgICAgICAgICAgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJTaWdhcmVcIl0gPC0gXCJTaWdhcmVcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJQaXBlXCJdIDwtIFwiUGlwZVwiXG5uYW1lcyhjaG9sKVtuYW1lcyhjaG9sKT09XCJTTU9LRVwiXSAgPC0gXCJTTU9LRVJcIiIsInNhbXBsZSI6IiMgTG9hZCBgbGF0dGljZWBcbmxpYnJhcnkobGF0dGljZSlcblxuI1Bsb3QgdHdvIHBsb3RzIHNpZGUgYnkgc2lkZVxuYndwbG90KH4gQ0hPTHxTTU9LRVIrTU9SVCxcbiAgICAgICBjaG9sKSJ9

Rplot27

Plotting Plots Next To Each Other With gridBase

Another way even to put two plots next to each other is by using the gridBase package, which takes care of the “integration of base and grid graphics”. This could be handy when you want to put a basic R plot and a ggplot next to each other.

You work as follows: first, you activate the necessary packages in your workspace. In this case, you want to have gridBase ready to put the two plots next to each other and grid and ggplot2 to actually make your plots:

library(grid)
library(gridBase)
library(ggplot2)

plot.new()

gl <- grid.layout(nrow=1, 
                  ncol=2)
vp.1 <- viewport(layout.pos.col=1, 
                 layout.pos.row=1) 
vp.2 <- viewport(layout.pos.col=2, 
                 layout.pos.row=1)
                 
pushViewport(viewport(layout=gl))
pushViewport(vp.1)

par(new=TRUE, 
    fig=gridFIG())
plot(x = 1:10, 
     y = 10:1)
     
popViewport()

pushViewport(vp.2)

ggplotted <- qplot(x=1:10,y=10:1, 'point')
print(ggplotted, newpage = FALSE)

popViewport(1)

Rplot28

If you want and need it, you can start an empty plot to then set up the layout. Note that since you want the two plots to be generated next to each other, this requires you to make a grid layout consisting of one row and two columns.

Now, you want to fill up the cells of the grid with viewports. These define rectangular regions on your graphics device with the help of coordinates within those regions. In this case, it’s much more handy to use the specifications of the grid that have just been described above rather than real x-or y-coordinates. That is why you should use the layout.pos.col and layout.pos.row arguments.

Since the viewports are only descriptions or definitions, these kinds of objects need to be pushed onto the viewport tree before you can see any effect on the drawing. You want to use the pushViewport() function to accomplish this.

Now you can proceed to adding the first rectangular region vp.1 to the ViewPort tree, after which you tell R with gridFig() to draw a base plot within a grid viewport (vp.1, that is). The fig argument normally takes the coordinates of the figure region in the display region of the device. In this case, you use the fig argument to start a new plot, adding it to an existing plot use by adding new = TRUE in the par() function as well. You plot the base graphic and remove the viewport from the tree:

plot.new()

gl <- grid.layout(nrow=1, 
                  ncol=2)

vp.1 <- viewport(layout.pos.col=1, 
                 layout.pos.row=1) 
vp.2 <- viewport(layout.pos.col=2, 
                 layout.pos.row=1)

pushViewport(viewport(layout=gl))

pushViewport(vp.1)

par(new=TRUE, 
    fig=gridFIG())
plot(x = 1:10, 
     y = 10:1)
popViewport()

Since you want the two plots to be generated next to each other, you want to put one plot in the first column and the other in the second column, both located on the first row.

Note that the pushViewport() function takes the viewport() function, which in itself contains a layout argument. This last argument indicates “a grid layout object which splits the viewport into subregions”.

Note also that you can specify in the popViewport() function an argument to indicate how many viewports you want to remove from the tree. If this value is 0, this indicates that you want to remove the viewports right up to the root viewport. The default value of this argument is 1.

Go to add the second rectangular region vp.2 to the ViewPort tree. You can then make the ggplot and remove the viewport from the tree.

pushViewport(vp.2)
ggplotted <- qplot(x=1:10,
                   y=10:1, 
                   'point')
print(ggplotted, 
      newpage = FALSE)
popViewport(1)

Note that you need to print to print the graphics object made by qplot() in order to actually draw it and get it displayed. At the same time, you also want to specify newpage = FALSE, otherwise you’ll only see the qplot()

Also remember that the default value of viewports to remove in the function popViewport() is set at 1. This makes it kind of redundant to put popViewport(1) in the code.

13. How To Plot Multiple Lines Or Points?

Using Basic R To Plot Multiple Lines Or Points In The Same R Plot

To plot two or more graphs in the same plot, you basically start by making a usual basic plot in R. Then, you start adding more lines or points to the plot. In this case, you add more lines to the plot, so you’ll define more y axes:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERhdGFcbnggPC0gc2VxKDAscGksMC4xKVxueTEgPC0gY29zKHgpXG5cbiMgUGxvdFxucGxvdCh4LFxuICAgICB5MSxcbiAgICAgdHlwZT1cImxcIixcbiAgICAgY29sID0gXCJyZWRcIilcblxuIyBNb3JlIGRhdGFcbnkyIDwtIHNpbih4KVxueTMgPC0gdGFuKHgpXG55NCA8LSBsb2coeClcblxuIyBMaW5lc1xubGluZXMoeCx5Mixjb2w9XCJncmVlblwiKVxubGluZXMoeCx5Mixjb2w9XCJncmVlblwiKVxubGluZXMoeCx5Myxjb2w9XCJibGFja1wiKVxubGluZXMoeCx5NCxjb2w9XCJibHVlXCIpIn0=

Next, you see that these lines are plotted with the use of the lines() function.

This gives the following result:

Rplot29

Note that the lines() function takes in three arguments: the x-axis and the y-axis that you want to plot and the color (represented with the argument col) in which you want to plot them. You can also include the following features:

Feature Argument Input
Line type lty Integer or character string
Line width lwd Integer
Plot type pch Integer or single character
Line end style lend Integer or string
Line join style ljoin Integer or string
Line mitre limit lmitre Integer < 1

Here are some examples:

lines(x,y2,col="green", lty = 2, lwd = 3)
lines(x,y2,col="green", lty = 5, lwd = 2, pch = 2)
lines(x,y3,col="black", lty = 3, lwd = 5, pch = 3, lend = 0, ljoin = 2)
lines(x,y4,col="blue", lty = 1, lwd = 2, pch = 3, lend = 2, ljoin = 1, lmitre = 2)

Note that the pch argument does not function all that well with the lines() function and that it’s best to use it only with points().

Tip: if you want to plot points in the same graph, you can use the points() function:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERhdGFcbnggPC0gc2VxKDAscGksMC4xKVxueTUgPC0geF4zXG5cbiMgUG9pbnRzXG5wb2ludHMoeCxcbiAgICAgICB5NSxcbiAgICAgICBjb2w9XCJ5ZWxsb3dcIikifQ==

You can add the same arguments to the points() function as you did with the lines() function and that are listed above. There are some additions, though:

Feature Argument Input
Background (fill) color bg Only if pch = 21:25
Character (or symbol) expansion cex Integer

Code examples of these arguments are the following:

points(x,y4,col="blue", pch=21, bg = "red") 
points(x, y5, col="yellow", pch = 5, bg = "blue") 
If you incorporate these changes into the plot that you see above, you will get the following result:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERhdGFcbnggPC0gc2VxKDAscGksMC4xKVxueTEgPC0gY29zKHgpXG5cbiNiYXNpYyBncmFwaGljYWwgb2JqZWN0XG5wbG90KHgseTEsdHlwZT1cImxcIiAsY29sID0gXCJyZWRcIilcblxuIyBNb3JlIGRhdGFcbnkyIDwtIHNpbih4KVxueTMgPC0gdGFuKHgpXG55NCA8LSBsb2coeClcbnk1IDwtIHheM1xuXG4jZmlyc3QgbGF5ZXJcbmxpbmVzKHgseTIsY29sPVwiZ3JlZW5cIiwgbHR5ID0gMSwgbHdkID0gMykgXG4jc2Vjb25kIGxheWVyXG5saW5lcyh4LHkyLGNvbD1cImdyZWVuXCIsIGx0eSA9IDMsIGx3ZCA9IDIsIHBjaCA9IDIpIFxuI3RoaXJkIGxheWVyXG5saW5lcyh4LHkzLGNvbD1cImJsYWNrXCIsIGx0eSA9IDIsIGx3ZCA9IDEsIHBjaCA9IDMsIGxlbmQgPSAwLCBsam9pbiA9IDIpIFxuI2ZvdXJ0aCBsYXllclxucG9pbnRzKHgseTQsY29sPVwiYmx1ZVwiLCBwY2g9MjEsIGJnID0gXCJyZWRcIikgXG4jZmlmdGggbGF5ZXJcbnBvaW50cyh4LCB5NSwgY29sPVwieWVsbG93XCIsIHBjaCA9IDI0LCBiZyA9IFwiYmx1ZVwiKSAifQ==

Rplot30

Using ggplot2 To Plot Multiple Lines Or Points In One R Plot

The ggplot2 package conveniently allows you to also create layers, which allows you to basically plot two or more graphs into the same R plot without any difficulties and pretty easily:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIExvYWQgaW4gYGdncGxvdDJgXG5saWJyYXJ5KGdncGxvdDIpXG5cbiMgRGF0YVxueCA8LSAxOjEwXG55MSA8LSBjKDIsNCw2LDgsNywxMiwxNCwxNiwxOCwyMClcbnkyIDwtIHJub3JtKDEwLCBtZWFuID0gNSlcbmRmIDwtIGRhdGEuZnJhbWUoeCwgeTEsIHkyKVxuXG4jIGJhc2ljIGdyYXBoaWNhbCBvYmplY3RcbmdncGxvdChkZiwgYWVzKHgpKSArICBcbiAgIyBmaXJzdCBsYXllclxuICBnZW9tX2xpbmUoYWVzKHk9eTEpLCBcbiAgICAgICAgICAgIGNvbG91cj1cInJlZFwiKSArIFxuICAjIHNlY29uZCBsYXllclxuICBnZW9tX2xpbmUoYWVzKHk9eTIpLCAgICAgIFxuICAgICAgICAgICAgY29sb3VyPVwiZ3JlZW5cIikgIn0=

14. How To Fix The Aspect Ratio For Your R Plots

If you want to put your R plot to be saved as an image where the axes are proportional to their size, it’s a sign that you want to fix the aspect ratio.

Adjusting The Aspect Ratio With Basic R

When you’re working with basic R commands to produce your plots, you can add the argument asp of the plot() function, completed with an integer, to set your aspect ratio. Look at this first example without a defined aspect ratio:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERhdGFcbnggPC0gc2VxKDAsMipwaSwwLjEpXG55IDwtIHNpbih4KVxuXG4jIFBsb3RcbnBsb3QoeCx5KSJ9

Rplot31

And compare this now to the plot where the aspect ratio is defined with the argument asp:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERhdGFcbnggPC0gc2VxKDAsMipwaSwwLjEpXG55IDwtIHNpbih4KVxuXG4jIFBsb3RcbnBsb3QoeCxcbiAgICAgeSwgXG4gICAgIGFzcD0yKSJ9

Rplot32

Adjusting The Aspect Ratio For Your Plots With ggplot2

To fix the aspect ratio for ggplot2 plots, you just add the function coord_fixed(), which provides a “fixed scale coordinate system [that] forces a specified ratio between the physical representation of data units on the axes”.

In other words, this function allows you to specify a number of units on the y-axis which is equivalent to one unit on the x-axis. The default is always set at 1, which means that one unit on the x-axis has the same length as one unit on the y-axis. If your ratio is set at a higher value, the units on the y-axis are longer than units on the x-axis and vice versa.

Compare the following examples:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIExvYWQgaW4gYGdncGxvdDJgXG5saWJyYXJ5KGdncGxvdDIpXG5cbiMgRGF0YVxuZGYgPC0gZGF0YS5mcmFtZSh4ID0gcnVuaWYoMTAwLCAwLCA1KSxcbiAgICAgICAgICAgICAgICAgeSA9IHJ1bmlmKDEwMCwgMCwgNSkpXG5cbiMgUGxvdFxuZ2dwbG90KGRmLCBhZXMoeD14LCB5PXkpKSArIGdlb21fcG9pbnQoKSJ9

Rplot33

versus
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIExvYWQgaW4gYGdncGxvdDJgXG5saWJyYXJ5KGdncGxvdDIpXG5cbiMgRGF0YVxuZGYgPC0gZGF0YS5mcmFtZSh4ID0gcnVuaWYoMTAwLCAwLCA1KSxcbiAgICAgICAgICAgICAgICAgeSA9IHJ1bmlmKDEwMCwgMCwgNSkpXG5cbiMgUGxvdFxuZ2dwbG90KGRmLCBhZXMoeD14LCB5PXkpKSArIFxuICBnZW9tX3BvaW50KCkgKyBcbiAgY29vcmRfZml4ZWQocmF0aW89MSkifQ==

Rplot34

Adjusting The Aspect Ratio For Your Plots With MASS

You can also consider using the MASS package, which encompasses the eqscplot() function: it produces plots with geometrically equal scales. It does this for scatterplots:
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImNob2wgPC0gcmVhZC50YWJsZSh1cmwoXCJodHRwOi8vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSlcbmxldmVscyhjaG9sJFNNT0tFKVtsZXZlbHMoY2hvbCRTTU9LRSk9PVwiTm9uLXNtb2tlclwiXSA8LSBcIk5vbi1zbW9rZXJcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJTaWdhcmVcIl0gPC0gXCJTaWdhcmVcIlxubGV2ZWxzKGNob2wkU01PS0UpW2xldmVscyhjaG9sJFNNT0tFKT09XCJQaXBlXCJdIDwtIFwiUGlwZVwiXG5uYW1lcyhjaG9sKVtuYW1lcyhjaG9sKT09XCJTTU9LRVwiXSAgPC0gXCJTTU9LRVJcIiIsInNhbXBsZSI6IiMgTG9hZCBpbiBgTUFTU2BcbmxpYnJhcnkoTUFTUylcblxuIyBEYXRhXG54ID0gY2hvbCRIRUlHSFRcbnkgPSBjaG9sJFdFSUdIVFxueiA9IGFzLm51bWVyaWMoY2hvbCRNT1JUKVxuXG4jIFBsb3QgXG5lcXNjcGxvdCh4LCBcbiAgICAgICAgIHksIFxuICAgICAgICAgcmF0aW8gPSAxLCBcbiAgICAgICAgIGNvbD1jKFwicmVkXCIsIFwiZ3JlZW5cIiksIFxuICAgICAgICAgcGNoPWMoMSwyKSkifQ==

Rplot35

Tip: you might do well starting a new plot frame before executing the code above!

Note that you can give additional arguments to the eqscplot() function to customize the scatterplot’s look!

15. What Is The Function Of hjust And vjust In ggplot2?

Well, you basically use these arguments when you want to set the position of text in your ggplot. hjust allows you to define the horizontal justification, while vjust is meant to control the vertical justification. See the documentation on geom_text() for more information.

To demonstrate what exactly happens, you can create a data frame from all combinations of factors with the expand.grid() function:

hjustvjust <- expand.grid(hjust=c(0, 0.5, 1),
                          vjust=c(0, 0.5, 1),
                          angle=c(0, 45, 90),
                          text="Text"
                          )

Note that hjust and vjust can only take values between 0 and 1.

  • 0 means that the text is left-justified; In other words, all text is aligned to the left margin. This is usually what you see when working with text editors such as Microsoft Word.
  • 1 means that the text is right-justified: all text is aligned to the right margin.
Then, you can plot the data frame that you have just made above with the ggplot() function, defining the x-and y-axis as hjust and vjust respectively:
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImhqdXN0dmp1c3QgPC0gZXhwYW5kLmdyaWQoaGp1c3Q9YygwLCAwLjUsIDEpLFxuICAgICAgICAgICAgICAgICAgICAgICAgICB2anVzdD1jKDAsIDAuNSwgMSksXG4gICAgICAgICAgICAgICAgICAgICAgICAgIGFuZ2xlPWMoMCwgNDUsIDkwKSxcbiAgICAgICAgICAgICAgICAgICAgICAgICAgdGV4dD1cIlRleHRcIlxuICAgICAgICAgICAgICAgICAgICAgICAgICApIiwic2FtcGxlIjoibGlicmFyeShnZ3Bsb3QyKVxuZ2dwbG90KGhqdXN0dmp1c3QsIGFlcyh4PWhqdXN0LCBcbiAgICAgICAgICAgICAgIHk9dmp1c3QpKSArIFxuICAgIGdlb21fcG9pbnQoKSArXG4gICAgZ2VvbV90ZXh0KGFlcyhsYWJlbD10ZXh0LCBcbiAgICAgICAgICAgICAgICAgIGFuZ2xlPWFuZ2xlLCBcbiAgICAgICAgICAgICAgICAgIGhqdXN0PWhqdXN0LCBcbiAgICAgICAgICAgICAgICAgIHZqdXN0PXZqdXN0KSkgKyBcbiAgICBmYWNldF9ncmlkKH5hbmdsZSkgK1xuICAgIHNjYWxlX3hfY29udGludW91cyhicmVha3M9YygwLCAwLjUsIDEpLCBcbiAgICAgICAgICAgICAgICAgICAgICAgZXhwYW5kPWMoMCwgMC4yKSkgK1xuICAgIHNjYWxlX3lfY29udGludW91cyhicmVha3M9YygwLCAwLjUsIDEpLCBcbiAgICAgICAgICAgICAgICAgICAgICAgZXhwYW5kPWMoMCwgMC4yKSkifQ==

Rplot36

Also note how the hjust and vjust arguments are added to geom_text(), which takes care of the textual annotations to the plot.

In the plot above you see that the text at the point (0,0) is left-aligned, horizontally as well as vertically. On the other hand, the text at point (1,1) is right-aligned in horizontal as well as vertical direction. The point (0.5,0.5) is right in the middle: it’s not really left-aligned nor right-aligned for what concerns the horizontal and vertical directions.

Note that when these arguments are defined to change the axis text, the horizontal alignment for axis text is defined in relation to the entire plot, not to the x-axis!
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJsaWJyYXJ5KGdncGxvdDIpXG5kZiA8LSBkYXRhLmZyYW1lKHg9TEVUVEVSU1sxOjNdLFxuICAgICAgICAgICAgICAgICB5PTE6MylcbnAgPC0gZ2dwbG90KGRmLCBhZXMoeCx5KSkgKyBcbiAgZ2VvbV9wb2ludCgpICsgXG4gIHlsYWIoXCJWZXJ5IGxvbmcgbGFiZWwgZm9yIHlcIikgKyBcbiAgdGhlbWUoYXhpcy50aXRsZS55PWVsZW1lbnRfdGV4dChhbmdsZT0wKSlcblxuXG5wMSA8LSBwICsgdGhlbWUoYXhpcy50aXRsZS54PWVsZW1lbnRfdGV4dChoanVzdD0wKSkgKyB4bGFiKFwiWC1heGlzIGF0IGhqdXN0PTBcIilcbnAyIDwtIHAgKyB0aGVtZShheGlzLnRpdGxlLng9ZWxlbWVudF90ZXh0KGhqdXN0PTAuNSkpICsgeGxhYihcIlgtYXhpcyBhdCBoanVzdD0wLjVcIilcbnAzIDwtIHAgKyB0aGVtZShheGlzLnRpdGxlLng9ZWxlbWVudF90ZXh0KGhqdXN0PTEpKSArIHhsYWIoXCJYLWF4aXMgYXQgaGp1c3Q9MVwiKVxuXG5saWJyYXJ5KGdyaWRFeHRyYSlcbmdyaWQuYXJyYW5nZShwMSwgcDIsIHAzKSJ9

Rplot37

Also try for yourself what defining the vjust agument to change the axis text does to the representation of your plot:
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJsaWJyYXJ5KGdncGxvdDIpXG5kZiA8LSBkYXRhLmZyYW1lKHg9YyhcImFcXG5hXCIsXCJiXCIsXCJjZGVmZ2hpamtcIixcImxcIiksXG4gICAgICAgICAgICAgICAgIHk9MTo0KVxucCA8LSBnZ3Bsb3QoZGYsIGFlcyh4LHkpKSArIGdlb21fcG9pbnQoKVxuXG5wMSA8LSBwICsgdGhlbWUoYXhpcy50ZXh0Lng9ZWxlbWVudF90ZXh0KHZqdXN0PTAsIGNvbG91cj1cInJlZFwiKSkgKyBcbiAgICAgICAgeGxhYihcIlgtYXhpcyBsYWJlbHMgYWxpZ25lZCB3aXRoIHZqdXN0PTBcIilcbnAyIDwtIHAgKyB0aGVtZShheGlzLnRleHQueD1lbGVtZW50X3RleHQodmp1c3Q9MC41LCBjb2xvdXI9XCJyZWRcIikpICsgXG4gICAgICAgIHhsYWIoXCJYLWF4aXMgbGFiZWxzIGFsaWduZWQgd2l0aCB2anVzdD0wLjVcIilcbnAzIDwtIHAgKyB0aGVtZShheGlzLnRleHQueD1lbGVtZW50X3RleHQodmp1c3Q9MSwgY29sb3VyPVwicmVkXCIpKSArIFxuICAgICAgICB4bGFiKFwiWC1heGlzIGxhYmVscyBhbGlnbmVkIHdpdGggdmp1c3Q9MVwiKVxuXG5cbmxpYnJhcnkoZ3JpZEV4dHJhKVxuZ3JpZC5hcnJhbmdlKHAscDEscDIscDMpIn0=

To go to the original excellent discussion, from which the code above was adopted, click here.

As A Last Note…

It’s really worth checking out this article, which lists 10 tips for making your R graphics look their best!

Also, if you want to know more about data visualization, you might consider checking out DataCamp’s interactive course on data visualization with ggvis, given by Garrett Grolemund, author of Hands on Programming with R, as well as Data Science with R.

Or maybe our course on reporting with R Markdown can interest you!

Comments

markperfect36
Great info. Really helped my understanding of plots. I would contribute one addition.

In the grid section, the grids are not helpful for identifying point values. You can mess around with the grid options, but I've found it far easier to, in this case, use:

abline(v=1:5,lty=2,col="lightgray")
abline(h=1:10,lty=2,col="lightgray")
04/05/16 6:18 PM |
dae2014
The click here link is dead.
(at end of Question 1) "Tip: if you want to see what number links to what symbol, click here."
03/16/16 12:59 AM |
karthik-gen
Thanks a lot..great post!
03/14/16 12:14 PM |
ivofilipe1
Great job!
03/07/16 2:27 PM |
parevest
Really useful thanks a lot
02/07/16 10:54 AM |