Found 2038 Articles for R Programming

How to include a factor level in bar blot using ggplot2 in R if that level has a frequency zero.

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:57:01

291 Views

In research, sometimes we get a count of zero for a particular level of a factor variable but we might want to plot that in the bar plot so that anyone who look at the plot can easily understand what is missing and compare all the factor levels. In ggplot2, it can be done with the help of scale_x_discrete function.> x df df$x df$x [1] S1 S2 S3 S4 S1 S2 S3 S4 S1 S2 S3 S4 S1 S2 S3 S4 S1 S2 S3 S4 Levels: S1 S2 S3 S4 S5Loading ggplot2 package −> library(ggplot2)Now when ... Read More

How to save matrix created in R as tables in a text file with column names same as the matrix?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:55:43

3K+ Views

Matrix data is sometimes need to be saved as table in text files, the reason behind this is storage capacity of text files. But when we save a matrix as text files in R, the column names are misplaced therefore we need to take care of those names and it can be done by setting column names to the desired value.> M M       [, 1] [, 2] [, 3] [, 4] [1, ] 1 5 9 13 [2, ] 2 ... Read More

How to create a bar chart using ggplot2 with facets that are in the order of the data in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 12:01:51

2K+ Views

Since visualization is an essential part of data analysis, we should make sure that the plots are created in a form that is easily readable for users. For this purpose, the facets in a bar chart helps us to understand the factor variable levels for another factor. To create such type of bar chart, we can use facet_grid function of ggplot2 package.ExampleConsider the below data frame −> set.seed(99) > y class quantity df library(ggplot2)Creating the plot with class on X-axis and y on Y-axis without any facet −> ggplot(df, aes(class, y))+ + geom_bar(stat="identity")OutputCreating the plot with class on X-axis, y ... Read More

How to stop printing messages while loading a package in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 11:59:32

159 Views

There are some annoying messages we get while loading a package in R and they are not useful until and unless we are not loading a new package. Since these messages looks like outputs they might be confusing especially when we are analysing string data. Therefore, we must get rid of them.An example of message while loading BSDA package:>> library(BSDA)Loading required package − latticAttaching package − ‘BSDA’The following object is masked from ‘package:datasets’ −OrangeHere we have some messages while loading the package BSDA but we might not be interested in those messages if we are sure that package is installed ... Read More

How to arrange a list of scatterplots in R using grid.arrange?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:46:12

410 Views

In predictive modeling, we get so many variables in our data set and we want to visualize the relationship among these variables at a time. This helps us to understand how one variable changes with the other, and on the basis of that we can use the better modeling technique. To create a list of plots we can use grid.arrange function in gridExtra package that can arrange plots based on our need.ExampleConsider the below data frame −> set.seed(10) > df head(df, 20)        x1            x2        x3     x4 1 ... Read More

How to display a list of plots with the help of grid.arrange in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:49:28

2K+ Views

In data analysis, we deal with many variables at a time and we want to visualize the histogram of these variables at a time. This helps us to understand the distribution of each variable in the data set, therefore we can apply the appropriate technique to deal with those variables. To create a list of plots we can use grid.arrange function in gridExtra package that can arrange plots based on our need.ExampleConsider the below data frame −> set.seed(10) > df head(df, 20)             x1     x2           x3     ... Read More

How to create a plot for the response variable grouped by two columns using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:38:28

84 Views

When two categorical variables make an impact on the response variable together then it is necessary to visualize their effect graphically because this graph helps us to understand the variation in the effect. Therefore, we can create a plot for the response variable that changes with one or both of the categorical independent variables. This can be done with the help of using interaction function in ggplot2.ExampleConsider the below data frame −> set.seed(1) > y Group1 Group2 df head(df, 20) y Group1 Group2 1    1 a Ph1 2    1 b Ph1 3    2 c Ph1 4   ... Read More

How to write text and output it as a text file using R?

Nizamuddin Siddiqui
Updated on 11-Jul-2020 12:56:06

581 Views

The easiest way to write text and obtain it as an output is done by using writeLines function and cat function, and the output of these functions are connected with the help fileConn and sink.Example> fileConn writeLines(c("TutorialsPoint", "SIMPLY EASY LEARNING"), fileConn) > close(fileConn)We can do the same and view these files in R as follows −> fileConn writeLines(c(paste("TutorialsPoint", "E-learning"), "2006", "Video Courses", "Tutorials", "Books"), fileConn) > close(fileConn) > file.show("example.txt")Using sink function> sink("example3.txt") > cat("TutorialsPoint", "E-learning") > cat("") > cat("2006") > cat("") > cat("Video Courses") > cat("") > cat("Tutorials") > cat("") > cat("Books") > sink()Using only cat function> cat("TutorialsPoint E-learning", ... Read More

Which function should be used to load a package in R, require or library?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 15:02:01

417 Views

The main difference between require and library is that require was designed to use inside functions and library is used to load packages. If a package is not available then library throws an error on the other hand require gives a warning message.Using library> library(xyz) Error in library(xyz) : there is no package called ‘xyz’Using requirerequire(xyz) Loading required package: xyz Warning message: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : there is no package called ‘xyz’Here we can see that the library shows an error and require gives a warning message, since warnings are mostly avoided ... Read More

How to deal with “could not find function” error in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 15:01:06

8K+ Views

The error “could not find function” occurs due to the following reasons −Function name is incorrect. Always remember that function names are case sensitive in R.The package that contains the function was not installed. We have to install packages in R once before using any function contained by them. It can be done as install.packages("package_name")The package was not loaded before using the function. To use the function that is contained in a package we need to load the package and it can be done as library("package_name").Version of R is older where the function you are using does not exist.If you ... Read More

Advertisements