Create Multiple Regression Lines in a Single Plot Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 12-Nov-2021 03:52:33

10K+ Views

To create multiple regression lines in a single plot using ggplot2, we can use geom_jitter function along with geom_smooth function. The geom_smooth function will help us to different regression line with different colors and geom_jitter will differentiate the points.Check out the below Example to understand how it can be done.ExampleFollowing snippet creates a sample data frame −x1

Create a Graph Without Background Panel Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 12-Nov-2021 03:45:36

173 Views

To create a graph without background panel, we can use theme function of ggplot2 package where we can set panel.background argument to blank.For Example, if we have a data frame called df that contains two columns say x and y then we can create scatterplot between x and y without background panel using ggplot2 by using the below command −ggplot(df,aes(x,y))+geom_point()+theme(panel.background=element_blank())ExampleConsider the below data frame −x

Create a Graph Using ggplot2 Without Axes Ticks and Labels

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

192 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

Display Variable with Subscript in ggplot2 Graph in R

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

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

Display Tilde in ggplot2 Graph in R

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

416 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

Create a Circle with Vertical Lines in R

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

266 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

Create a 90 Degree Arc in R

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

304 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

Create Circle with Different Color Border in R

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

513 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

Fill Bars of a Bar Plot with Colors Based on Frequency in ggplot2

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

844 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

Create a Matrix with Equal Rows in R

Nizamuddin Siddiqui
Updated on 11-Nov-2021 08:26:43

779 Views

If we have a single row for a matrix then creation of a matrix with equal rows can be easily done with the help of rep function and if we do not have the row then we would need to pass the row value inside rep function.Check out the below examples to understand how to create a matrix with equal rows if one row is known.Example 1Consider the below vector −Row_1

Advertisements