

Now we can add regression line to the scatter plot by adding geom_smooth() function. And Thanks to Alison Horst, we now have the the data easily available. Kristen Gorman and the Palmer Station, Antarctica LTER. Penguin Data was originally collected and made available by Dr.

palmer penguin’s data set to make scatterplots with regression lines. We will use our most beloved data set, i.e. Let us load tidyverse and set ggplot2 theme with bigger base size for legible axis labels. And then see how to add multiple regression lines, regression line per group in the data. We will first start with adding a single regression to the whole data first to a scatter plot. In ggplot2, we can add regression lines using geom_smooth() function as additional layer to an existing ggplot2. The numbers assigned to fig were arrived at with a hit-and-trial method to achieve the best looking plot.In this tutorial, we will learn how to add regression lines per group to scatterplot in R using ggplot2. Note: we have used parameters cex to decrease the size of labels and mai to define margins. For example, the whole plot area would be c(0, 1, 0, 1) with (x1, y1) = (0, 0) being the lower-left corner and (x2, y2) = (1, 1) being the upper-right corner. We need to provide the coordinates in a normalized form as c(x1, x2, y1, y2). The graphical parameter fig lets us control the location of a figure precisely in a plot. Note that only the ordering of the subplot is different. Same plot with the change par(mfcol = c(2, 2)) would look as follows. The only difference between the two is that, mfrow fills in the subplot region row wise while mfcol fills it column wise. This same phenomenon can be achieved with the graphical parameter mfcol. Par(mfrow=c(1,2)) # set the plotting area into a 1*2 array For example, if we need to plot two graphs side by side, we would have m=1 and n=2. It takes in a vector of form c(m, n) which divides the given plot into m*n array of subplots. Graphical parameter mfrow can be used to specify the number of subplot we need.

Here we will focus on those which help us in creating subplots. You will see a long list of parameters and to know what each does you can check the help section ?par. For example, you can look at all the parameters and their value by calling the function without any argument. The par() function helps us in setting or inquiring about these parameters.

R programming has a lot of graphical parameters which control the way our graphs are displayed. We can put multiple graphs in a single plot by setting some graphical parameters with the help of par() function. Sometimes we need to put two or more graphs in a single plot.
