Use of Type H in Base R for Plotting a Graph

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:40:18

3K+ Views

The type = "h" is a graphing argument in base R which is generally used inside a plot function. It helps to generate the vertical lines in the R environment instead of points. For example, if we want to plot values from 1 to 10 then type = "h" will plot the vertical lines starting from X-axis and the upper end of the lines will represent the actual value.Example1Live Demo> plot(1:10,type="h")Output:Example2Live Demo> plot(rnorm(10),type="h")Output:

Find Less Than Probability Using Normal Distribution in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:36:10

1K+ Views

The less than probability using normal distribution is the cumulative probability which can be found by using cumulative distribution function of the normal distribution. In R, we have pnorm function that directly calculates the less than probability for a normally distributed random variable that takes Z score, mean and standard deviation.ExamplesLive Demopnorm(0.95,1,0) pnorm(0.95,0,1) pnorm(0.10,0,1) pnorm(0.10,1,5) pnorm(0.10,1,50) pnorm(0.10,25,50) pnorm(0.12,25,50) pnorm(0.12,2,0.004) pnorm(0.12,2,0.5) pnorm(1,2,0.5) pnorm(12,20,3) pnorm(12,12,3) pnorm(12,15,3) pnorm(200,15,3) pnorm(200,201,3) pnorm(200,201,5) pnorm(20,25,5)Output[1] 0 [1] 0.8289439 [1] 0.5398278 [1] 0.4285763 [1] 0.4928194 [1] 0.309242 [1] 0.309383 [1] 0 [1] 8.495668e-05 [1] 0.02275013 [1] 0.003830381 [1] 0.5 [1] 0.1586553 [1] 1 [1] 0.3694413 [1] 0.4207403 [1] 0.1586553

Change Legend Values in a Bar Plot Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:33:08

693 Views

How to change legend values in a bar plot created by using ggplot2 in R?By default, the legend values are taken as the different levels of the categorical variable for which a bar plot is created using ggplot2 package and if we want to change those values then scale_color_manual function of the ggplot2 package can be used where we need to pass the values for color and labels for legend values.ExampleConsider the below data frame:Live Demo> set.seed(1214) > x1 y1 df1 df1Output x1 y1 1 B 4 2 B 5 3 C 5 ... Read More

Check Difference Between Plot Generation Time in Base R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:28:33

96 Views

One of the mostly used time measurement function in R is microbenchmark function of microbenchmark package. We can pass the function to create the plot inside microbenchmark function and this will result in the processing time for each of the plots then a comparison can be done for the difference.Example1Loading microbenchmark package:> library(microbenchmark)Finding the plot generation time:> x1 x2 x3 X XUnit: milliseconds expr min lq mean median uq max neval plot(x1) 12.7488 14.88815 15.65040 15.2515 15.90765 23.9348 100 plot(x2) 20.9810 21.67780 23.92976 22.2116 23.29665 137.2474 100 plot(x3) 93.6965 95.03440 96.67086 95.6717 97.12290 125.3670 100Plots:Example> plot(x1)Output:Example> plot(x2)Output:Example> plot(x3)Output:

Change Resolution of a Plot in Base R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:22:07

3K+ Views

In base R, we can save a plot as a png and pass the resolution in the same stage. The procedure to do this is creating the png image with resolution with res argument then creating the plot and using dev.off() to create the file. Check out the below examples to understand how it works.Example1Live Demo> png(file="example1.png",res=100) > plot(1:10) > dev.off()Output:Example2Live Demo> png(file="example2.png",res=200) > plot(1:10) > dev.off()Output

Create Point Chart with Size Increment in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:16:14

274 Views

The cex argument is used to increase or decrease the point size in a point chart created in base R. If we want to create a point chart with points of size in increment manner then we can pass a vector of the same size as the vector for which we want to create the point chart. For example, if we have a vector x that contains 10 elements then cex will be set as cex=1:10.Example1Live Demo> x plot(x, cex=1:10, xlim=c(1, 12), ylim=c(-2, 12))Output:ExampleLet’s have a look at another example:Live Demo> y plot(y, cex=1:10, xlim=c(1, 12), ylim=c(-1, 12))Output:Read More

Create Point Chart in R with Alternative Points of Different Shapes

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:12:18

273 Views

How to create a point chart in R with alternative points having different shape?The pch argument is used to create different points in a base R chart and if we want to have alternative points of different shape then we can use rep function with pch argument but we need to use it for the exact length of the vector. For example, if we want to create a point chart of a vector that contains 10 values having different shapes of alternate points then we can use pch=rep(1:2, 5)).Example1Live Demo> x plot(x, pch=rep(1:2, 5))Output:ExampleLet’s have a look at another example:Live ... Read More

Fix Coefficient of Independent Variable in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:09:08

655 Views

While doing the analysis we might know the variation in an independent variable or we might want to understand how other independent variables behave if we fix a certain variable. Therefore, we can fix the coefficient of an independent variable while creating the model and this can be done by using offset function with the coefficient of the variable for which we want to fix the value of the coefficient.ExampleConsider the below data frame:Live Demo> set.seed(854) > x1 x2 y1 df1 df1Output x1 x2 ... Read More

Create a Barplot with Gaps on Y-Axis Scale in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:03:03

685 Views

If we want to have gaps on Y-axis scale in a barplot then it cannot be done in base R. For this purpose, we can make use of gap.barplot function of plotrix package. The gap.barplot function is very handy, we just need to pass the vector for which we want to create the barplot and the gap values simply by using gap argument.Loading plotrix package:> library(plotrix)Example1Live Demo> x xOutput[1] 2 6 5 4 7 2 5 2 5 2 8 6 8 13 3 5 7 7 5 6> gap.barplot(x, gap=c(2, 4)) ylim 0 11Warning message:In gap.barplot(x, gap = c(2, ... Read More

Set Diagonal Elements of a Matrix to 1 in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 10:58:04

1K+ Views

First thing we need to understand is diagonal elements are useful only if we have a square matrix, otherwise it would not make sense to set diagonal elements, this is known to almost all mathematicians but some freshman might get confused because we can create diagonal in a non-square matrix which should not be called a diagonal. In R, we can set the diagonal elements of a matrix to 1 by using diag function.Example1Live Demo> M1 M1Output     [, 1] [, 2] [, 3] [, 4] [, 5] [1, ]   1    6   11   16   21 ... Read More

Advertisements