Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 74 of 196

How to create multiple bar plots for varying categories with same width bars using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 1K+ Views

To create multiple bar plots for varying categories with same width bars using ggplot2, we would need to play with width argument inside geom_bar function to match the width of the bars in each bar plot. The best way to do this would be setting the larger ones to 0.25 and the shorter ones to 0.50.ExampleConsider the below data frame − Live Demox1

Read More

How to find the high leverage values for a regression model in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 2K+ Views

To find the high leverage values for a regression model, we first need to find the predicted values or hat values that can be found by using hatvalues function and then define the condition for high leverage and extract them. For example if we have a regression model say M then the hat values can be found by using the command hatvalues(M), now to find the high leverage values that are greater than 0.05 can be found by using the below code −which(hatvalues(M)>0.05)Example1Consider the below data frame − Live Demox1

Read More

How to apply multiple AND conditions to a data frame in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 355 Views

To apply multiple conditions to a data frame, we can use double and sign that is &&. For example, if we have a data frame called df that contains three columns say x, y, z and we want to add a value to all columns if first element in z equals to 5 then it can be done by using the command −if(df$x && df$y && df$y == 5){    df$x = df$x+10    df$y = df$y+10    df$z = df$z+10 }Example1Consider the below data frame − Live Demox1

Read More

How to create empty bar plot in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 434 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 4K+ 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 498 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

Read More

How to change default point size of geom_point in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 498 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 1K+ 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 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)) Output

Read More
Showing 731–740 of 1,958 articles
« Prev 1 72 73 74 75 76 196 Next »
Advertisements