Programming Articles - Page 930 of 3363

How to divide columns of a matrix by vector elements in R?

Nizamuddin Siddiqui
Updated on 28-Oct-2021 13:49:11

2K+ Views

Suppose we have a vector say V that contains five elements and a matrix say M that has five columns. Now again, suppose that we want to divide each column in M by corresponding value in vector V, which means first column in M will be divided by first value in the V and so on then we can use the sweep function as shown below −sweep(M,2,V,FUN="/")Example 1Consider the below matrix and vector −M1

How to remove all rows having NA in R?

Nizamuddin Siddiqui
Updated on 02-Sep-2023 12:07:50

77K+ Views

To remove all rows having NA, we can use na.omit() function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na.omit(df).That means if we have more than one column in the data frame then rows that contains even one NA will be removed. Check out the below Examples to understand how it works.Example 1Consider the below data frame −x1

How to create a circle with vertical lines in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 03:14:20

284 Views

We can create a circle in R by using draw.circle function of plotrix package and if we want to have vertical lines inside the circle then density and angle arguments will be used. The density argument will create the lines and angle argument will set the direction of those lines.For vertical lines the angle will be 270 and the number of lines depends on the value of density argument.Check out the below Examples to understand how different values of density create lines inside the circle.ExampleTo create a circle in R, use the code given below −plot(1:10, type="n")OutputIf you execute the ... Read More

How to create a 90-degree arc in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 03:12:32

313 Views

To create a 90-degree arc in R, we can use draw.arc function of plotrix package where we can use deg2 argument. Since, a 90-degree can be drawn in four ways; we need to pass the degree depending on the position of the arc we want to display.Check out an Example explained below.ExampleTo create a 90-degree arc in R, add the following code −plot(1:10, type="n")OutputIf you execute the above given snippet, it generates the following Output −To create a 90-degree arc in R, add the following code to the above snippet −plot(1:10, type="n") draw.arc(5, 5, 2, deg2=90, col="blue")OutputIf you execute all ... Read More

How to create a circle with different color border in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 03:09:39

529 Views

We can create a circle in R by using draw.circle function of plotrix package and default border color of the circle is black. If we want to change the border color of a circle then we can use border argument and pass the desired colors.For Example, if we want to create a blue colored circle then, we can use the below mentioned command −draw.circle(5, 5, 2, border="blue")Check out the below Example to understand how it works.ExampleTo create a colored circle add the following code to the above snippet −plot(1:10, type="n")OutputIf you execute the above given snippet, it generates the following ... Read More

How to fill bars of a bar plot created using ggplot2 with colors based on frequency?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 03:05:54

870 Views

To fill bars in a bar plot using ggplot2 in R with colors based on frequency, we can use fill argument with count.For Example, if we have a data frame called df that contains a single column X that contains repeated values and we want to create bar plot of values in X based on their frequencies then we can use the below command −ggplot(df)+geom_bar(aes(X,fill=..count..))ExampleConsider the data frame given below −x

How to create a time series plot in R without time vector?

Nizamuddin Siddiqui
Updated on 28-Oct-2021 10:03:49

436 Views

To create a time series plot in R without time vector, we can use ts.plot function.For Example, if we have a vector called X then we can create the time series plot of X by using the command ts.plot(X), the Output of this command will have a time axis in place of X-axis. Check out the below Examples to understand how it works.Example 1Consider the following vector −x

How to plot matrix elements using corrplot in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:41:52

3K+ Views

To create a plot of matrix elements with the help of corrplot function, we would need to set is.corr argument to FALSE so that the matrix values will be plotted in the diagram, otherwise, the corrplot function requires correlation matrix instead of a matrix hence there will be an error as shown in the Example below.ExampleConsider the matrix given below −M

How to draw concentric circles in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:36:05

786 Views

To draw concentric circles, we can use draw.circle function of plotrix package but firstly, we would need to create a blank graph with plot function as shown below.For Example, we can create three concentric circles at position X=5 and Y=5 having radius of 1, 2, and 3 by using the below command −draw.circle(5, 5, c(3, 2, 1), col=c("blue", "red", "green"))ExampleTo create three concentric circles at position X=5 and Y=5 having radius of 1, 2, and 3 add the following code to the above snippet −plot(1:10, type="n")OutputIf you execute all the above given snippets as a single program, it generates the ... Read More

How to create vertical line in xyplot in R?

Nizamuddin Siddiqui
Updated on 28-Oct-2021 09:37:20

547 Views

To create vertical line in xyplot, we can use abline function.For Example, if we have a data frame called df that contains two columns say X and Y and we want to create a scatterplot between X and Y using xyplot with a vertical line at X = 2 then we can use the below command −xyplot(y~x,df,abline=c(v=2))ExampleConsider the data frame given below −x

Advertisements