Found 2038 Articles for R Programming

How to create a line for equal values of x and y in scatterplot in R?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 10:25:29

170 Views

To create a line for equal values of x and y in scatterplot, we can make use of segments function in base R but this can be done after creating the plot with the help of plot function. The segments function has four arguments, x0, y0, x1, and y1, we need to put the same value in x0 and y0 and the same value in x1 and y1 to draw the appropriate line as shown in the below examples.Example1Live Demo> x xOutput[1] -1.14191974 1.11554154 -0.01255755 1.18841175 1.11300329 -0.69925814 [7] -0.88000117 0.67830803 -0.91237446 -1.14223973ExampleLive Demo> y yOutput[1] -1.69229826 -0.70352587 0.38544874 0.14022473 ... Read More

How to plot the confidence interval of the regression model using ggplot2 with transparency in R?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 10:21:50

644 Views

To plot the confidence interval of the regression model, we can use geom_ribbon function of ggplot2 package but by default it will have dark grey color. It can become transparent with the help of alpha argument inside the same function, the alpha argument can be adjusted as per our requirement but the most recommended value by me is 0.2.ExampleConsider the below data frame −Live Demo> x y df dfOutput x y 1 22.67102 29.37057 2 21.59415 29.54027 3 20.56817 28.27672 4 24.97228 31.38193 5 21.41651 31.86811 6 ... Read More

How to extract the column from a list of data frames in R?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 10:19:01

636 Views

In Data Analysis, we often required to extract a single value, a single row, or a single column for a specific analysis. For example, if data frame contains column defined as height and weight then we might want to use only height then it can be extracted, this could be a part of a list as well, therefore, extraction from list will be required. If we have a list of data frames then extraction of a column from one of the data frames in the list can be done by using double square brackets for accessing the data frame and ... Read More

How to convert a matrix into a color matrix in R?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 10:14:37

932 Views

To convert a matrix into a color matrix, we can make use of image function. There are multiple ways for assigning the colors but the easiest one might be by defining the minimum and maximum value in the matrix. Also, we can do this by using the shades of a single color as shown in the example 3.Example1Live Demo> M1 M1Output   [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 6    3    5    4    3 [2, ] 9    4    5    2    5 [3, ] 3    2   ... Read More

How to create a plot in base R with tick marks but excluding axes lines?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 10:09:28

101 Views

To create a plot with tick marks but without axes lines, we first need to create the plot without axes and then add the tick marks. This can be done with the help of plot function and axis function in base R. The axis function will help us to decide where do we need the tick marks and the ticks.Example1> plot(1:10,axes=FALSE) > axis(1,c(1:10),col=NA,col.ticks=1)OutputExample2Live Demo> x xOutput[1] 5 2 1 2 1Example> plot(x,axes=FALSE) > axis(1,c(1:5),col=NA,col.ticks=1)Output

How to combine lists of data frames in R?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 10:03:42

362 Views

If we have a list of data frames and the size of those data frames is same then we might want to combine the lists so that the data frames can be combined. This can be done by using mapply function along with cbind. For example, if we have two lists of data frames defined as List1 and List2 then we can combine them using the command −mapply(cbind, List1, List2, SIMPLIFY=FALSE).ExampleConsider the below data frame −Live Demo> x1 x2 df1 df1Output      x1        x2 1   0.2378371  0.51433808 2   0.0638975 -1.66077353 3   0.3987209  0.68480587 ... Read More

How to increase the space between horizontal legends using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 09:58:35

488 Views

Generally, the space between two legend entries is not large enough and it becomes difficult to read the legend names if the names are long. In this case, we need to increase the margin between the legend entries/names but this would be required when the legends are horizontally aligned as vertical legends can be read as it is. For this purpose, we can use legend.text argument inside theme function of ggplot2 package.ExampleConsider the below data frame −Live Demo> x y df dfOutput x y 1 Male 501 2 Female 520Loading ggplot2 ... Read More

How to add a row in an R data frame at a specific place?

Nizamuddin Siddiqui
Updated on 02-Jan-2021 09:53:56

402 Views

The data collected for the first time is utilised as it is but when we need to go for secondary data to conduct the same or similar study again, we can use new data as well as the primary data. In this type of situations, we might want to randomly organize data rows that includes new and old data. Also, there is a possibility of missing data row which is found at later stage in the study then it can be also added. Hence, a row might be required to added in the existing data frame. This can be done ... Read More

How to create three-dimensional arrays of different sizes in R?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 07:22:32

733 Views

A three-dimensional array can have matrices of different size and they are not necessarily to be square or rectangular. Also, all the elements in an array are of same data type. To create a three-dimensional array of different size we would need to use the proper number of rows and columns within the array function.Example Live DemoA1

How to find percentile rank for groups in an R data frame?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 07:18:34

842 Views

The word percentile means the percentage that falls below or above the percentile value. For example, if we have a value that lies at 50th percentile then we would say 50 percent of the values lies below or above that value. The value 50 here is called the percentile rank. To find the percentile rank for groups in an R data frame, we can use mutate function of dplyr package.ExampleConsider the below data frame − Live DemoGroup

Advertisements