Plots With R 4,8/5 2382 votes

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. The plot command will try to produce the appropriate plots based on the data type. The data that is defined above, though, is numeric data. You need to convert the data to factors to make sure that the plot command treats it in an appropriate way. The as.factor command is used to cast the data as factors and ensures that R treats it as discrete.

Plots With R

Plots With R

This is the first post of a series that will look at how to create graphics in R using the plot function from the base package. There are of course other packages to make cool graphs in R (like ggplot2 or lattice), but so far plot always gave me satisfaction.

In this post we will see how to add information in basic scatterplots, how to draw a legend and finally how to add regression lines.

Data simulation

Adding colors

First plot adding colors for the different treatments, one way to do this is to pass a vector of colors to the col argument in the plot function.

Here is the plot:

Change plotting symbols

We can also create a vector of plotting symbols to represent data from the two different sites, the different plotting symbols available can be seen here.

Here is the plot:

Add a legend to the graph

Now we should add a legend to the graph:

Plot

Here is the plot:

With

The first argument to legend is basically its position in the graph, then comes the text of the legend. Optionally one may also specify the colors, plotting symbols etc … of the legend symbol. Have a look at ?legend for more options.
We can also add a legend outside of the graph by setting xpd=TRUE and by specifying the x and y coordinates of the legend.

Here is the plot:

Add regression lines

The last thing we might want to add are regression lines

Here is the plot:

There is a whole bunch of function to draw elements within the plotting area, a few examples are: points, lines, rect, text. They are handy in many situations and are very similar of use.

That’s it for this basic post, next times we’ll see how to control axis labels and tick marks.

Rstudio plots

This is the website for “Interactive web-based data visualization with R, plotly, and shiny”. In this book, you’ll gain insight and practical skills for creating interactive and dynamic web graphics for data analysis from R. It makes heavy use of plotly for rendering graphics, but you’ll also learn about other R packages that augment a data science workflow, such as the tidyverse and shiny. Along the way, you’ll gain insight into best practices for visualization of high-dimensional data, statistical graphics, and graphical perception. By mastering these concepts and tools, you’ll impress your colleagues with your ability to generate more informative, engaging, and repeatable interactive graphics using free software that you can share over email, export to pdf/png, and more.

Plots With Regression

An online version of this book, available at https://plotly-r.com, is free to use and is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 United States License. If you’d like a physical copy of the book, you can order it from CRC Press and Amazon. Both the print and online versions of the book are written in rmarkdown with bookdown and those source files are available at https://github.com/cpsievert/plotly_book. The online version will continue to evolve in between reprints of the physical book.