R Programming Articles

Page 4 of 174

How to perform group-wise linear regression for a data frame in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

The group−wise linear regression means creating regression model for group levels. For example, if we have a dependent variable y and the independent variable x also a grouping variable G that divides the combination of x and y into multiple groups then we can create a linear regression model for each of the group. In R, we can convert data frame to data.table object, this will help us to create the regression models easily.Exampledf2[,as.list(coef(lm(Salary~Ratings))),by=Class]OutputClass (Intercept) Ratings 1: I 31894.13 194.9152 2: III 35270.10 663.4089 3: II 40405.42 -1087.9103

Read More

How to change the color of points in a scatterplot using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 20K+ Views

To color the points in a scatterplot using ggplot2, we can use colour argument inside geom_point with aes. The color can be passed in multiple ways, one such way is to name the particular color and the other way is to giving a range or using a variable. If range or a variable will be used then the color of the points will be in different shades.Exampleggplot(df,aes(x,y))+geom_point(aes(colour=x))Output

Read More

How to increase the width of the median line in boxplot using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

The default width of the median line is wider than the rest of the lines that represent minimum, first quartile, third quartile or maximum but we can make it a little wider to make it more appealing. This can be done with the help of fatten argument inside geom_boxplot function, the default value of fatten is 2.Exampleggplot(df,aes(x,y))+geom_boxplot(fatten=6)Output

Read More

How to create a step histogram using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 413 Views

To create a step histogram using ggplot2, we can use geom="step" argument inside stat_bin function. For example, if we have a data frame that contains a single column then the step histogram can be created using the command − ggplot(df,aes(x))+stat_bin(geom="step",bins=30)Examplelibrary(ggplot2) ggplot(df,aes(x))+stat_bin(geom="step",bins=30)Output

Read More

How to display star for significance in base R boxplot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

To display the star for significance in a base R boxplot, we can use text function. The text function will be helpful in defining the star sign (that is asterisk or *). If the significance is high then three stars are used and the significance is low then a single star is used. We need to use the appropriate position using x and y values.Exampletext(x=1,y=max(df$y[df$x==1]),"***",pos=3,cex=1.5)Output

Read More

How to extract the factor levels from factor column in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 13K+ Views

To extract the factor levels from factor column, we can simply use levels function. For example, if we have a data frame called df that contains a factor column defined with x then the levels of factor levels in x can be extracted by using the command levels(df$x). This extraction is helpful if we have a large number of levels.Example1x2

Read More

How to create bar chart based on two groups in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 715 Views

To create a bar chart based on two groups, we can use geom_bar function of ggplot2 package with position argument that defines the position of the groups. For example, if we have a data frame called df that contains two categorical variable x1 and x2 and the one response variable y then the bar chart can be created by using the below command −ggplot(df,aes(x1,y,fill=x2))+geom_bar(position=position_dodge(),stat="identity")Examplelibrary(ggplot2) ggplot(df,aes(Age_Group,Y,fill=Gender))+geom_bar(position=position_dodge(),stat="identity")Output

Read More

How to change the Y-axis title to horizontal using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 4K+ Views

The default direction of Y-axis title using ggplot2 in R is vertical and we can change to horizontal. For this purpose, we can use theme function of ggplot2 package. We would need to use the argument of theme function as axis.title.y=element_text(angle=0)) and this will write the Y-axis title to horizontal but the position will be changed to top.Exampleggplot(df,aes(x))+geom_histogram(bins=30)+theme(axis.title.y=element_text(angle=0))Output

Read More

How to deal with the error “Error in int_abline---plot.new has not been called yet” in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

The above error means plot is not being created yet hence abline function cannot be used to draw anything on the plot. Therefore, a plot needs to be created first to use abline function for creating a line or any other thing. Mostly, abline is used to create regression line on the plot, thus we need to create a scatterplot first before using abline.Exampleabline(lm(y~x))Output

Read More

How to create correlation matrix plot in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 440 Views

To create a correlation matrix plot, we can use ggpairs function of GGally package. For example, if we have a data frame called df that contains five columns then the correlation matrix plot can be created as ggpairs(df). A correlation matrix plot using ggpairs display correlation value as well as scatterplot and the distribution of variable on diagonal.Examplelibrary(GGally) ggpairs(df)Output

Read More
Showing 31–40 of 1,740 articles
« Prev 1 2 3 4 5 6 174 Next »
Advertisements