Found 2038 Articles for R Programming

Create a graph using ggplot2 without axes ticks and axes labels.

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

92 Views

To create a graph using ggplot2 without axes ticks and axes labels, we can use theme function where we can use set axes ticks and axis labels to blank with the help of arguments corresponding to each axes such as axis.ticks.x, axis.ticks.y, axis.text.x, and axis.text.y.To understand how it works, check out the below Example.ExampleConsider the below data frame −x

How to display a variable with subscript ggplot2 graph in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 03:43:22

851 Views

Sometimes we have variables that have a subscript associated with them. This subscript is used to define the characteristics of the variable or to differentiate similar variables from each other.In this type of situation, displaying a variable with subscript in a graph created with the help of ggplot2 can be done by using the geom_text function. Check out the below Example to understand how it can be done.ExampleConsider the below data frame −x1

How to display tilde ggplot2 graph in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 03:40:50

256 Views

Let’s say we want to display tilde sign at a particular position in histogram using ggplot2 graph. In this situation, we can use geom_text function and pass all the text with label argument inside aes where tilde will be written as %~%.For Example, if we want to display X follows Normal Distribution then we can write it as −geom_text(aes(label="X %~% Normal Distribution",x=0,y=200),parse=TRUE)Here, x=0 and y=200 is the position of the label in the histogram.ExampleConsider the below data frame −x

Select columns of an R data frame and skip if does not exist.

Nizamuddin Siddiqui
Updated on 28-Oct-2021 14:02:39

580 Views

Sometimes we have a large number of columns in the data frame and we know the name of some columns but among known ones some does not exist in the data frame. Now if we want to select the columns that we know and skip the ones that do not exist then we can use the subsetting.For Example, if we have a data frame called df that contains twenty columns and we believe that x, y, z exists in df but z is not there in reality. Now the selection of columns x, y, z that will skip z can ... Read More

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

71K+ 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

183 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

207 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

352 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

760 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

Advertisements