Found 2038 Articles for R Programming

How to change the legend title in ggplot2 in R?

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:19:33

374 Views

In ggplot2, by default the legend title is the title of the grouping column of the data frame. If we want to change that title then scale_color_discrete function. For example, if we have a data frame called df that contains two numerical columns x and y and one grouping column say group then the scatterplot with a different legend title can be created by using the below command −ggplot(df, aes(x, y, color=group))+geom_point()+scale_color_discrete("Gender")ExampleConsider the below data frame −Live Demo> x y grp df dfOutput             x          y    grp 1  -2.27846496  0.8121008   ... Read More

How to create a histogram with dots instead of bars in base R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 08:56:51

516 Views

To create a histogram with dots instead of bars, we can use the plot function in base R. The x values will be based on the sorting of the vector values and the y values will be based on the sequence of the table for the vector values. Therefore, we would be needing sorting and table with sequence. Check out the below examples to understand how it can be done.Example1> x1 plot(sort(x1),sequence(table(x1)))OutputExample2> x2 plot(sort(x2),sequence(table(x2)))Output

How to subset a data frame based on a vector values in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 08:55:30

3K+ Views

If we have a vector and a data frame, and the data frame has a column that contains the values similar as in the vector then we can create a subset of the data frame based on that vector. This can be done with the help of single square brackets and %in% operator. The %in% operator will help us to find the values in the data frame column that matches with the vector values. Check out the below examples to understand how it works.Example1Consider the below data frame df1 and vector v1 −Live Demo> x1 x2 df1 df1Outputx1 x2 1 ... Read More

How to display average line for y variable using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 08:54:08

2K+ Views

To display the average line for y variable using ggplot2, we can use geom_hline function along with the yintercept. In the yintercept, we would need to calculate the mean of the y variable and we can also change the colour of the line using color argument inside the geom_hline function.ExampleConsider the below data frame −Live Demo> x y df dfOutputx y 1 -1.07323904 0.368641641 2 0.92531148 -0.196530651 3 -0.57433739 0.710957804 4 1.17367100 0.300110517 5 0.00769624 -1.287517035 6 0.64901161 -0.476105351 7 0.70197701 -0.683592585 8 -0.80807441 -1.716264317 9 0.10827026 0.116964308 10 -1.10451308 0.660382307 11 -0.01612692 -1.182533283 12 2.20292198 -1.890223763 13 -1.03368161 -0.526983486 ... Read More

How to create matrix with random integer values in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 08:53:04

526 Views

To create a vector of random integers we can use the function sample.int and if we want to create the matrix of such integers then matrix function will be used along with it. For example, if we want to create a matrix with random integers between 1 to 100 of size 20 with 4 columns and 5 rows then it can be done by using the below command −matrix(sample.int(100,size=20),nrow=5,ncol=4)Example1Live Demo> M1 M1Output[,1] [,2] [,3] [,4] [1,] 61 8 68 81 [2,] 34 33 40 70 [3,] 76 29 51 41 [4,] 31 77 8 94 [5,] 35 57 50 29 [6,] 96 28 83 3 [7,] 11 68 71 81 [8,] 63 50 94 85 [9,] 21 53 99 94 [10,] 31 67 23 62 [11,] 56 47 68 66 [12,] 56 5 77 27 [13,] 59 95 88 64 [14,] 21 1 86 55 [15,] 8 3 72 17 [16,] 29 41 61 99 [17,] 7 62 48 56 [18,] 80 78 97 57 [19,] 26 96 34 19 [20,] 73 88 57 72Example2Live Demo> M2 M2Output[,1] [,2] [,3] [,4] [,5] [1,] 956 707 421 995 589 [2,] 525 300 595 548 109 [3,] 610 216 754 888 864 [4,] 744 240 997 246 371 [5,] 848 535 477 127 938 [6,] 836 648 241 597 608 [7,] 675 629 517 758 469 [8,] 238 433 296 249 776 [9,] 226 552 933 917 625 [10,] 111 584 643 699 573 [11,] 168 239 409 844 850 [12,] 587 387 587 899 672 [13,] 55 612 315 572 574 [14,] 765 646 925 848 584 [15,] 158 191 235 435 19 [16,] 68 631 493 604 65 [17,] 740 976 498 755 534 [18,] 241 548 921 265 343 [19,] 907 364 318 502 141 [20,] 150 739 614 444 189Example3Live Demo> M3 M3Output[,1] [,2] [1,] 2025 4158 [2,] 1372 4495 [3,] 2208 2306 [4,] 1091 476 [5,] 2635 4873 [6,] 1724 3327 [7,] 580 4051 [8,] 546 3927 [9,] 4115 2399 [10,] 1520 4577 [11,] 420 2441 [12,] 2251 1323 [13,] 2908 1415 [14,] 733 3886 [15,] 3556 844 [16,] 2181 2161 [17,] 2771 2349 [18,] 4805 2057 [19,] 2269 4561 [20,] 3110 1250Example4Live Demo> M4 M4Output[,1] [,2] [,3] [,4] [,5] [1,] 10 7 6 8 4 [2,] 2 3 9 7 6 [3,] 1 6 10 3 9 [4,] 8 1 8 7 3 [5,] 10 6 10 8 3 [6,] 2 3 9 7 10 [7,] 5 9 2 1 5 [8,] 6 4 10 1 7 [9,] 3 5 2 5 5 [10,] 2 9 8 8 7 [11,] 6 5 8 3 9 [12,] 4 1 9 6 7 [13,] 2 10 9 4 5 [14,] 10 5 2 1 2 [15,] 3 10 9 2 10 [16,] 3 6 8 8 7 [17,] 3 2 9 3 4 [18,] 9 3 3 5 4 [19,] 6 3 1 3 2 [20,] 3 7 1 7 5

How to convert NaN values to NA in an R data frame?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 08:52:10

507 Views

To convert NaN values to NA, we would need to detect the NaN values in the data frame and the set them to NA. For example, if we have a data frame called df that contains a column x which has some NaN values then we can convert those NaN to NA by using the command df$x[is.nan(df$x)] x1 x2 df1 df1Outputx1 x2 1 NaN -0.44923302 2 NaN -0.12670027 3 1 0.59120380 4 1 -0.18782341 5 NaN -0.28730385 6 1 0.57412261 7 NaN -0.33620181 8 1 1.37168545 9 NaN -2.24121448 10 NaN 1.05990104 11 1 1.95544957 12 NaN -2.19544854 13 1 ... Read More

How to create boxplot with multiple factor levels using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 18:23:00

4K+ Views

To create a boxplot, we have one factor and one numerical column and the boxplot is created for each category or levels in that factor. Now if we have two factors then the boxplot can be created for both factor levels by passing fill argument in geom_boxplot. This will help us to differentiate between the boxplots for the two factors. Check out the below examples to understand how it works.ExampleConsider the below data frame −Live Demo> x y grp df dfOutput       x              y  grp 1 Female    0.790349405 b ... Read More

How to remove rows in a data.table object with NA's in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 18:29:49

2K+ Views

If a row contains missing values then their sum will not finite, therefore, we can use is.finite function with the data.table object to remove the rows with NA’s. For example, if we have a data.table object called DT that contains some rows with NA’s then the removal of those rows can be done by using DT[is.finite(rowSums(DT))].Example1Loading data.table package and creating a data.table object −> library(data.table) > x1 x2 DT1 DT1Output   x1  x2 1:  1   2 2:  NA  4 3:  1   2 4:  NA  5 5:  1   6 6:  1   8 7:  NA  3 8:  1   ... Read More

How to find the mean for multiple columns in an R data frame using mean function not colMeans?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:15:04

250 Views

The easiest way to find the column means of an R data frame is to use colMeans function but if we do not want to use it then it can be done with the help of sapply. While using sapply, we need to make sure that we are selecting only numerical columns of the data frame. Check out the below examples to understand how it works.Example1Consider the CO2 data frame in base R −Live Demo> head(CO2, 20)Output  Plant  Type  Treatment  conc  uptake 1   Qn1 Quebec nonchilled  95   16.0 2   Qn1 Quebec nonchilled  175  30.4 3   Qn1 ... Read More

How to assign a column value in a data frame based on another column in another R data frame?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:18:25

4K+ Views

To assign a column value based on another column, we can use ifelse function. The ifelse function checks whether the value in one column of one data frame matches the value in another column of another data frame by using equal sign (==) and then replace the original value with the new column if there is no match else returns the original value. Check out the below example to understand how it can be done.ExampleConsider the below data frame −Live Demo> x1 x2 df1 df1Output x1 x2 1 3 5 2 3 7 3 ... Read More

Advertisements