Plots With R

Posted onby admin
Plots With R Rating: 9,5/10 8573 votes
  1. The most commonly used graphs in the R language are scattered plots, box plots, line graphs, pie charts, histograms, and bar charts. R graphs support both two dimensional and three-dimensional plots for exploratory data analysis.There are R function like plot , barplot , pie are used to develop graphs in R language.
  2. In this example we'll plot the distribution of 1000 random values that have the log-normal distribution. R r) Since log-normal distributions normally look better with log-log axes, let's use the plot function with points to show the distribution.

Dummies has always stood for taking on complex concepts and making them easy to understand. Dummies helps everyone be more knowledgeable and confident in applying what they know.

© 2006-16 by Frank McCown

The following is an introduction for producing simple graphs with the R Programming Language. Each example builds on the previous one. The areas in bold indicate new text that was added to the previous example. The graph produced by each example is shown on the right.

Jump to a section:

Line Charts

First we'll produce a very simple graph using the values in the car vector:
Let's add a title, a line to connect the points, and some color:
Now let's add a red line for trucks and specify the y-axis range directly so it will be large enough to fit the truck data:
Next let's change the axes labels to match our data and add a legend. We'll also compute the y-axis values using the max function so any changes to our data will be automatically reflected in our graph.
Now let's read the graph data directly from a tab-delimited file. The file contains an additional set of values for SUVs. We'll save the file in the C:/R directory (you'll use a different path if not using Windows).

autos.dat

We'll also use a vector for storing the colors to be used in our graph so if we want to change the colors later on, there's only one place in the file that needs to be modified. Finally we'll send the figure directly to a PNG file.
In this next example, we'll save the file to a PDF and chop off extra white space around the graph; this is useful when wanting to use figures in LaTeX. We'll also increase the line widths, shrink the axis font size, and tilt the x-axis labels by 45 degrees.

Bar Charts

Let's start with a simple bar chart graphing the cars vector:
Let's now read the auto data from the autos.dat data file, add labels, blue borders around the bars, and density lines:
Now let's graph the total number of autos per day using some color and show a legend:
Let's graph the total number of autos per day using a stacked bar chart and place the legend outside of the plot area:

Histograms

Let's start with a simple histogram graphing the distribution of the suvs vector:
Let's now read the auto data from the autos.dat data file and plot a histogram of the combined car, truck, and suv data in color.
Now change the breaks so none of the values are grouped together and flip the y-axis labels horizontally.
Now let's create uneven breaks and graph the probability density.
In this example we'll plot the distribution of 1000 random values that have the log-normal distribution.
Since log-normal distributions normally look better with log-log axes, let's use the plot function with points to show the distribution.

Pie Charts

Let's start with a simple pie chart graphing the cars vector:
Now let's add a heading, change the colors, and define our own labels:
Now let's change the colors, label using percentages, and create a legend:

Dotcharts

Let's start with a simple dotchart graphing the autos data:
Let's make the dotchart a little more colorful:

Misc

This example shows all 25 symbols that you can use to produce points in your graphs:

Plot
[This article was first published on business-science.io, 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.

Interested in automating code? Learn more R automation tips with How to Automate Excel with R and How to Automate PowerPoint with R.

When plotting different charts during your exploratory data analysis, you sometimes end up doing a lot of repetitive coding. What we’ll show here is a better way to do your EDA, and with less unnecessary coding and more flexibility. So, let me introduce you to the powerful package combo ggplot2 and purrr.

ggplot2 is an awesome package for data visualization very well know in the Data Science community and probably the library that you use to build your charts during the EDA. And, purrr package enhances R’s functional programming (FP) toolkit by providing a complete and consistent set of tools for working with functions and vectors.

Here will use an implementation similar to loops, but written in a more efficient way and easier to read, purrr::map() function.

The dataset is an imaginary HR dataset made by data scientists of IBM Company. In our analysis, we’ll look just at categorical variables, and plotting the proportion of each class within the categorical variable.

The dataset has a total of 35 features, being 9 of them categorical, and also which we will use.

Color Lines In Plot R

With

Before we start to build our plot, we need to specify which variable will be used in the analysis.
We’ll use just look at categorical features, in order to see the proportion between different classes, we’ll write a named vector with this information.

The set_names function is super handy for naming character vectors since it can use the values of the vector as names.

My approach to this problem is, first plot the chart that you want, and second, replace the variable with an input of a function. This part is where you need to put more effort into coding.

In the code chunk below you’ll find a plot for a specific variable, “Attrition”.

R Plot Make A Selection Script

I like to use this kind of plot when we have many different plots together, instead of using bar charts. The columns of bar charts can throw to the user too much information when just the end of the bar is important.

One tip to really grasp the steps for building this kind of chart (lollipop chart) is to thinking plots like layers (grammar of graphics) and put one on top of the other.

Plotting Data In R

There are three core layers that need to be built in sequence:

Plots With Residual

  1. geom_segment()
  2. geom_point()
  3. geom_label()
With

The rest of the plot is trivial to any ggplot chart that you already build.

Now we need to replace the variable used before as input in a function.

To do this replacement we will use the pronoun .data from rlang package, this pronoun allows you to be explicit about where to find objects when programming with data masked functions.

Using this strategy we will have the following function:

Here is the important step where we apply the function that we create to all character features in the dataset. And also, we’ll apply the cowplot::plot_grid() that put together all ggplot2 objects in all_plots list.

In this tutorial, you learned how to save time when was needed to plot a chart a lot of times. I hope that was useful for you.

Author: Luciano Oliveira Batista
Luciano is a chemical engineer and data scientist in training. Learn more on his blog at lobdata.com.

To leave a comment for the author, please follow the link and comment on their blog: business-science.io.
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.