Found 2038 Articles for R Programming

How to create empty bar plot in base R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:59:47

291 Views

To create a bar plot in base R, we can use the function barplot and pass the vector or column of the data frame for which we want to create the bar plot but the bars created by using barplot by default has grey color. Therefore, if we want to create an empty bar plot then setting the color of bars to NA will make the plot an empty bar plot.Example1x

How to check if a time series is stationary in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:57:19

6K+ Views

To check if a time series is stationary, we can use Dickey-Fuller test using adf.test function of tseries package. For example, if we have a time series object say TimeData then to check whether this time series is stationary or not we can use the command adf.test(TimeData).Example1 Live Demox1

How to deal with Error: stat_count() can only have an x or y aesthetic in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:49:42

3K+ Views

To deal with Error: stat_count() can only have an x or y aesthetic, we need to pass the stat="identity" argument inside geom_bar function. Since we do not pass the count for bars and a bar graph can only contain only count variable, hence stat="identity" is needed so that geom_bar considers only one variable in aes for counting. Check out the below example to understand the difference.ExampleConsider the below data frame − Live Demofactor

How to create data frame using nested list elements in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:48:00

363 Views

To create data frame using nested list elements, we would need to unlist the list elements and store them in a matrix then read as a data frame using data.frame function. For example, if we have a nested called LIST then the data frame can be created by using the command −data.frame(matrix(unlist(LIST),ncol=”No of columns we want”,byrow=F))Check out the below example to understand how it works.Example Live DemonestedList

How to subset an R data frame based on string match in two columns with OR condition?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:42:33

918 Views

To subset an R data frame based on string match in two columns with OR condition, we can use grepl function with double square brackets and OR operator |. For example, if we have a data frame called df that contains two string columns say x and y then subsetting based on a particular string match in any of the columns can be done by using the belowSyntaxdf[grepl("text",df[["x"]])|grepl("text",df[["y"]]),]Check out the below examples to understand how it works.Example1Consider the below data frame − Live Demof1

How to change default point size of geom_point in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:37:41

348 Views

To change the default point size of geom_point, we need to use update_geom_defaults function. Specifically, for the change of point size the syntax will be as follows −update_geom_defaults("point",list(size=”value”))Here, we can change the value according to our need.ExampleConsider the below data frame − Live Demox

How to find the proportion of each value for a cross tab obtained from a data frame in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:33:10

807 Views

To find the proportion of each value for a cross tab obtained from a data frame, we can use prop.table function. Suppose we have a data frame called df that contains three columns, two categorical say C1 and C2 and one numerical say Y then the cross tab will be created by using the command xtabs(Y~.,df). Now the proportion of each value can be found by using prop.table(xtabs(Y~.,df),1).Example1Consider the below data frame − Live Demof1

How to change the point size in geom_point conditionally in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:24:20

2K+ Views

To change the point size in geom_point conditionally, we can define the condition in geom_point with aes and the size using scale_size_manual function of ggplot2 package. For example, if we have a data frame called df that contains two columns say x and y then the scatterplot with different size of points for x values greater than 5 and less than equal to 5 can be drawn by using the below command −ggplot(df, aes(x, y))+geom_point(aes(size=x>5))+scale_size_manual(values=c(4, 7))ExampleConsider the below data frame − Live Demox6))+scale_size_manual(values=c(4, 7)) OutputRead More

How to create a list of regression models for predefined vectors in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:23:20

2K+ Views

To create a list of regression models for predefined vectors, we can create a blank list and then use the for loop to create the list of regression models. For example, if we have two vectors say x and y, and we want to create a list of regression model between x and y then we create a blank list using list() and perform for loop a particular number of times as shown in the below examples.Example1 Live Demox

How to add approximately equal sign in a plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:22:11

362 Views

To add approximately equal sign in a plot using ggplot2, we can use tilde sign twice as ~~ in geom_text function of ggplot2 package. For example, we can do this by using the following syntax geom_text(aes(label="NULL%~~%")). Check out the below example to understand how it works.ExampleConsider the below data frame − Live Demox

Advertisements