Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
R Programming Articles
Page 4 of 174
How to perform group-wise linear regression for a data frame in R?
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 MoreHow to change the color of points in a scatterplot using ggplot2 in R?
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 MoreHow to increase the width of the median line in boxplot using ggplot2 in R?
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 MoreHow to create a step histogram using ggplot2 in R?
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 MoreHow to display star for significance in base R boxplot?
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 MoreHow to extract the factor levels from factor column in an R data frame?
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 MoreHow to create bar chart based on two groups in an R data frame?
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 MoreHow to change the Y-axis title to horizontal using ggplot2 in R?
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 MoreHow to deal with the error “Error in int_abline---plot.new has not been called yet” in R?
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 MoreHow to create correlation matrix plot in R?
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