Change Title Size of a Graph Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:01:46

2K+ Views

The size of a graph title mattes a lot for the visibility because it is the first thing people look at after plot area. Its size must not be very large nor very small but is should be different from the axis titles and axes labels so that there exists a clarity in the graph. This can be done by using theme function.ExampleConsider the below data frame −set.seed(1) x

Combine Levels of a Factor Variable in an R Data Frame

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:58:43

4K+ Views

An R data frame can have numeric as well as factor variables. It has been seen that, factor levels in the raw data are recorded as synonyms even in different language versions but it is rare. For example, a factor variable can have hot and cold as levels but it is possible that hot is recorded as garam by a Hindi native speaker because garam is Hindi form of hot. Therefore, we need to combine the similar levels into one so that we do not have unnecessary factor levels for a variable.ExampleConsider the below data frame −set.seed(109) x1Read More

Find Difference in Days Between Two Date Columns in R Data Frame

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:56:29

1K+ Views

When dealing with date data, we often want to find the difference between dates if the data contains two or more date values. Same thing can be done for the two columns of an R data frame that contains dates but first we need to read those date columns in date format in case they are not recorded as date in R. The finding of difference in number of days can be done by using difftime function.ExampleConsider the below data −date1

Find Position of Non-NA Value in R Vector

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:54:06

618 Views

An NA value in R represents not available or missing value, therefore, it is not useful for any type of mathematical operations. Hence, non-NA values are the values that matters and we might want to find the position of these values. We can find the position of non-NA values in R using !is.na which means values that are not NA.Examplesset.seed(1) x1

Transform Numbers to Abbreviated Month in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:52:56

409 Views

Sometimes date vector for months is recorded in numeric form and it becomes difficult to treat or visualize it as a date vector. For example, if a vector for months has numbers 1 that represents January, 2 that represents February and so on then it is considered as a numeric vector instead of the vector to represent the month. To transform such type of vectors into abbreviated month as Jan, Feb, etc. we can use month.abb function.ExamplesMonth1

Remove Last Elements of a Vector in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:51:02

5K+ Views

A vector in R can have infinite number of elements but we might want to remove some of them. To remove the last elements of a vector, we can use head function with negative sign of the number of values we do not want. For example, if we have a vector of length 200 but we don’t want last fifty elements then we can use head(vector_name,-50).Examplesx1

Sort a Matrix Based on One Column in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:47:38

10K+ Views

Since a matrix contain only numeric values, sorting can be also done for matrices. There might be multiple reasons to sort a matrix such as we want to convert the matrix to a data frame, the data stored in matrix needs to be sorted prior to matrix calculations so that the view of the result after calculations becomes clearer, etc. To sort a matrix based on one column, we can use order function.Examplesset.seed(123) M1

Generate Random Samples from Statistical Distributions in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:45:23

204 Views

To generate random samples from statistical distributions, we use functions like rnorm, rbinom, rexp, rpois for the corresponding distribution based on their names. Using these functions, we can pass their parameters as an argument inside the function. But if we have the parameters saved as a list then generation of random sample is not straight forward, for this we need to use do.call function.Examplesparameters1

Move X-Axis Labels to the Top of the Plot Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:43:19

4K+ Views

Usually, a plot created in R or any of the statistical analysis software have X-axis labels on the bottom side but we might be interested in showing them at the top of the plot. It can be done for any type of two-dimensional plot whether it is a scatterplot, bar plot, etc. This is possible by using scale_x_continuous function of ggplot2 package in R.Example Live Demoset.seed(123) x

Total Number of Rows per Group Combination in an R Data Frame

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:40:09

306 Views

An R data frame that contains two or more factor columns then there are a greater number of combinations for the number of factors, obviously, if the number of factors is large with large number of levels then the combination of levels of the factors is also large. To find total number of rows per group combination we can use transform function.ExampleConsider the below data frame − Live Demoset.seed(101) Group

Advertisements