Found 2038 Articles for R Programming

How to write partial title of X-axis in italics using ggplot2 of R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:37:06

5K+ Views

Of course, writing axes titles help viewers to understand the plot in a better way because they add more information to the plot. In general, the axes titles have simple font but we can change partial or complete title to italics to get the viewers attraction. This is needed when we want to highlight the title by making it different. In ggplot2, we can do this by using expression.ExampleConsider the below data frame −set.seed(1) x

How to select top rows of an R data frame based on groups of factor column?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:33:35

157 Views

We use head function to take a look at some top values in an R data frame but it shows the top values for the whole data frame without considering the groups of factor column. Therefore, if we have a large number of values in a particular group then head function does not seem to be helpful alone, we must use something to extract the top values for each of the groups. This can be done through using by function with single square brackets and head function.Examplesdata(iris) str(iris) 'data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 ... Read More

How to delete rows of an R data frame based on string match?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:32:18

1K+ Views

Often, we need to subset our data frame and sometimes this subsetting is based on strings. If we have a character column or a factor column then we might be having its values as a string and we can subset the whole data frame by deleting rows that contain a value or part of a value, for example, we can get rid of all rows that contain set or setosa word in Species column.ExampleConsider the below data frame −Character

How to convert a matrix to a data frame with column names and row names as new columns in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:30:43

2K+ Views

Sometimes we want to create a factor column of the column names and row names of a matrix so that we can use them in the analysis. It is required in situations where we want to know the effect of factor variables on the response and the factor variables were recorded as column names and row names in a matrix. To do this, we can convert the matrix into table and the table obtained is converted to data frame.ExampleConsider the below matrix −M1

How to remove the boxes around legend of a plot created by ggplot2 in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:29:10

2K+ Views

When we create a plot with legend using ggplot2, the legend values are covered with a box and that makes an impact on the smoothness of the plot. These boxes around the legend values can be removed so that complete the chart becomes more appealing to the viewer and it can be done with the help of theme function by setting the legend.key element to blank.ExampleConsider the below data frame −set.seed(1) x

How to put labels on a scatterplot that is created plot function in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:25:19

83 Views

Labelling of points on a scatterplot helps us to identify the pair of observations. For example, if we are plotting weight and height of people then we can label it with person’s name, therefore, we will be able to understand which pair of points belong to which person. This can be done by using text function after creating the scatterplot with plot function.Examplex

How to cbind vectors of different length without repetition of elements of the smaller vector in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:21:48

806 Views

We can join vectors by columns using cbind and it does not matter whether these vectors are of same length or not. If the vectors are of same length then all the values of both the vectors are printed but if the length of these vectors are different then the values of the smaller vector gets repeated. But we might not want to repeat the values/elements of the smaller vector and it is possible by setting the length of the smaller vector to the length of larger vector, this will create NA values in the smaller at places where the ... Read More

How to merge data frames by row names in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:19:15

724 Views

Mostly, we merge the data frames by columns because column names are considered prominent in data sets but it is also possible to merge two data frames by using rows. Merging by rows is likely to result in more uncleaned data as compared to the merging by columns. This can be done with the help of merge function and its by argument.ExampleConsider the below data frames −df1

How to select of data.table based on substring of row values for a column in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:16:03

369 Views

We often create subsets of data in R to perform calculations based on smaller objectives of a whole objective in data analysis projects. Sometimes this subsetting is conditional on strings instead of numeric values. We can also create a subset of data.table on the basis of substring of row values for a column by using grep function.ExampleConsider the below data.table object −x1

How to write a common title for par(mfrow) plots in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:12:10

1K+ Views

We can create multiple plots using par(mfrow) on a single plot window in R. It might be possible that all of these plots are different or same as well. Irrespective of the type of plots, we can give a common title to all the plots. This can be a situation where we want to show, say, scatterplots, and histograms of few variables but the object of our analysis is fixed. To write a common title between plots in the plot window we can use mtext and adjust the title position by changing line argument.Exampleset.seed(100) x1

Advertisements