Found 2038 Articles for R Programming

How to change the size of plots arranged using grid.arrange in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:15:24

8K+ Views

To change the size of plots arranged using grid.arrange, we can use heights argument. The heights argument will have a vector equal to the number of plots that we want to arrange inside grid.arrange. The size of the plots will vary depending on the values in this vector.Consider the below data frame −Example Live Demox

How to create a cumulative sum plot in base R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:11:00

3K+ Views

To create a cumulative sum plot in base R, we can simply use plot function. For cumulative sums inside the plot, the cumsum function needs to be used for the variable that has to be summed up with cumulation. For example, if we have two vectors say x and y then the plot with cumulative sum plot can be created as plot(x,cumsum(y)).Examplex1

How to create a plot in base R with tick marks of larger size?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:08:48

1K+ Views

To create a plot in base R with tick marks of larger size, we can make use of axis function tck argument. The tck argument value will decide the size of the tick mark but since the ticks lie below the plot area hence the value will have a negative associated with it. Therefore, it will be like -0.05. Check out the below examples to understand how it works.Exampleplot(1:10,axes=FALSE,frame=TRUE) axis(1,1:10,tck=-0.02) axis(2,1:10,tck=-0.02)OutputExampleplot(1:10,axes=FALSE,frame=TRUE) axis(1,1:10,tck=-0.05) axis(2,1:10,tck=-0.05)Output

How to find mode for an R data frame column?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 12:02:06

3K+ Views

To find the model for an R data frame column, we can create a function and use it for the calculation. The function for mode is created as written below −mode

How to change the linetype for geom_vline in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 11:47:16

6K+ Views

To change the linetype for geom_vline, we can use linetype argument in geom_vline function of ggplot2 package. There are mainly six linetypes that can be used and these values are 0=blank, 1=solid (default), 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash.Consider the below data frame −x

How to extract all string values from a vector in R with maximum lengths?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 11:44:17

100 Views

If we have a string vector then all the values in the vector are not likely to be of same size and we might be looking for smaller size or bigger size values. Therefore, if we want to extract all string values from a vector in R with maximum lengths even if there are duplicates can be done by using the max and nchar function as shown in the below examples.Example Live Demox1

How to find the groupwise number of positive and negative values in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 11:42:08

617 Views

To find the groupwise number of positive and negative values in an R data frame, we can use mutate function of dplyr package. For example, if we have a data frame called df with one categorical column x and one numerical column y then the number of positive and negative values for categorical column can be found by using the below command −df%>%group_by(x)%>%mutate(positive=sum(y>0),negative=sum(y

How to create a linear model with interaction term only in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 11:33:04

865 Views

To create a linear model with interaction term only, we can use the interaction variable while creating the model. For example, if we have a data frame called df that has two independent variables say V1 and V2 and one dependent variable Y then the linear model with interaction term only can be created as lm(Y~V1:V2,data=df).Consider the below data frame −Example Live Demox1

How to create a perpendicular arrow in base R plot?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 11:29:48

194 Views

To create a perpendicular arrow in base R plot, we can use arrows function. There are five arguments of arrows function that will be used to create the perpendicular arrow. The first four values define the position of the arrow and the last argument xpd allows R to create the arrow. Check out the below examples to understand how it works.Exampleplot(1:10) arrows(1,-1,1,0,xpd=TRUE)OutputExampleplot(1:10) arrows(1,-1,1,2,xpd=TRUE)OutputExampleplot(1:10) arrows(2,-1,2,2,xpd=TRUE)Output

How to fill histogram bars using ggplot2 in R with different colors?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 11:17:43

1K+ Views

When we create histogram using ggplot2 we need to pass the number of bins we want to have in the histogram and on the basis of these bin numbers the histogram will be created, these bin numbers are actually the number of bars we will have in the histogram. To fill those bars with different colors, we need to use fill argument and pass a range of values equal to the number of bins as shown in the below example.Consider the below data frame −x

Advertisements