To create a regression line in base R, we use abline function after creating the scatterplot but if we want to have the line dash format then lty argument must also be used with value equals to 2 after defining the regression model inside abline. For example, if we have two columns x and y stored in a data frame called df then the plot with dashed regression line can be created by using −plot(y~x, data=df) abline(lm(df$y~df$x), lty=2)ExampleConsider the below data frame − Live Demo> x y df dfOutput x y 1 5.243553 4.969598 2 4.681088 ... Read More
To create a scatterplot with intercept equals to 1 using ggplot2, we can use geom_abline function but we need to pass the appropriate limits for the x axis and y axis values. For example, if we have two columns x and y in a data frame df and both have ranges starting from 0 to 5 then the scatterplot with intercept equals to 1 can be created as −ggplot(df,aes(x,y))+geom_point()+geom_abline(intercept=1)+lims(x=c(0,5),y=c(0,5))ExampleConsider the below data frame − Live Demox
To multiply two matrices by elements in R, we would need to use one of the matrices as vector. For example, if we have two matrices defined by names M1 and M2 then the multiplication of these matrices by elements can be done by using M1*as.vector(M2). The main thing we need to remember while doing this kind of multiplication is that the number of rows in both the matrices are equal.Example Live DemoM1
To create a plot with reversed Y-axis we need to use the rev function for the Y-axis labels with ylim but we would also need to define the range for the y-axis values, otherwise, R will throw an error. For example, if we have two vectors named as x and y then the plot with reversed Y-axis labels can be created by using plot(x,ylim=rev(range(y))).Example Live Demox
To create a plot with tick marks manually between X-axis values in base R, we first need to create the plot without X-axis labels then add the axis values using axis function with appropriate labels, this will create tick marks as well as labels. After this step, we would need to use the axis function again to add the tick marks without labels.Example Live Demoplot(1:10,xaxt='n',type="l") axis(1,at=1:10) axis(1,at=seq(0,11,0.2),labels=NA)OutputExample Live Demoplot(1,xaxt='n') axis(1,at=1) axis(1,at=seq(0,2,0.05),labels=NA)Output
To create the boxplots in base R ordered by means, we first need to order the categorical column based on the mean of the numerical column and then the boxplot will be created.For example, if we have a data frame df that has a categorical column x and a numerical column y then the boxplot ordered by means can be created by using df$x
A data frame might be very long and contain columns with only string values as well as numerical values. While doing the analysis, we might want to check which columns contain a particular string value. For example, if we have a column with string values as A, B, and C and we want to check which column contains a value “A” then apply function can be used as shown in the below examples.ExampleConsider the below data frame − Live Demox1
If we have two categorical columns along with a numerical column in an R data frame then we can find the mean of the numerical column by using the combination of the categorical columns with the help of aggregate function. For example, if a data frame df contains a numerical column X and two categorical columns C1 and C2 then the mean of X can be found for the combinations of C1 and C2 by using the below command −aggregate(X~C1+C2,data=df,FUN="mean")ExampleConsider the below data frame −C1
If we want to add variables to the model in base R then update function can be used. The update function will update the previous modle by adding the new variable and this variable can be a single variable as well as an interaction of the two or more also any possible transformation of the existing variables.ExampleConsider the below data frame − Live Demox1
The range for 95% of all values actually represents the middle 95% values. Therefore, we can find the 2.5th percentile and 97.5th percentile so that the range for middle 95% can be obtained. For this purpose, we can use quantile function in R. To find the 2.5th percentile, we would need to use the probability = 0.025 and for the 97.5th percentile we can use probability = 0.0975.Example Live Demox1