Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Nizamuddin Siddiqui
Page 158 of 196
How to change the thickness of the borders of bars in bar plot created by using ggplot2 in R?
The border thickness highlights the bars and this could be useful in situations where we have similar frequencies. If we want to change the thickness of the bars then size argument under geom_bar function of ggplot2 package can be used and it can be set according to our need starting from 1.ExampleConsider the below data frame −x
Read MoreHow to create geometric progression series in R?
A geometric progression series is a sequence of numbers in which all the numbers after the first can be found by multiplying the previous one by a fixed number. To generate a geometric progression series in R, we can use seq function. For example, to generate a geometric progression series of 2 by having the difference of multiplication value equal to 1 up to 5 can be found as 2^seq(0, 5, by=1) and the output would be 1, 2, 4, 8, 16, 32.Examples2^seq(0, 5, by=1) [1] 1 2 4 8 16 32 2^seq(0, 5, by=2) [1] 1 4 16 2^seq(0, ...
Read MoreHow to combine two columns of a data.table object in R?
A data.table object is almost same as a data frame. To combine two columns of a data.table object, we can use paste0 function. For example, if we have a data frame defined as DT that contains two columns named as x and y then we can combine them using the below command.DT[, xy:=paste0(x, y)]ExampleLoading data.table package.> library(data.table)Consider the below data.table object.Example> x y dt1 dt1Outputx y 1: 1 a 2: 2 b 3: 3 c 4: 4 d 5: 5 e 6: 6 f 7: 7 g 8: 8 h 9: 9 i 10: 10 j 11: 11 k 12: ...
Read MoreHow to extract a data.table row as a vector in R?
A data.table object is similar to a data frame object but there are few things that can be specifically applied to a data.table because data.table package functions was defined for a data.table object only. If we want to extract a data.table row as a vector then we can use as.vector function along with as.matrix so that the as.vector can read the row properly.Loading data.table package:> library(data.table)Consider the below vectors and create a data.table object:Example> x1 x2 x3 x4 x5 DT1 DT1Outputx1 x2 x3 x4 x5 1: B C C D E 2: B C D B E 3: B C ...
Read MoreHow to create a bar chart using ggplot2 with dots drawn at the center of top edge of the bars in R?
Aesthetics is one of the most important aspect of a chart, hence we should try to use the best possible aesthetic properties in a plot. In a bar chart, we can represent the center of bars in many ways and one such way is using dots at the center of the top edge of the bars. We can use geom_point function by defining colour argument to put points at the center of top edge of the bars in a bar chart created by using ggplot2.ExampleConsider the below data frame:> freq df dfOutputx freq 1 Mango 212 2 Guava 220 3 ...
Read MoreHow to display xtable values in scientific form in R?
The xtable function of xtable package creates a Latex table. We can use digits argument with negative sign to convert the values in the original table into scientific form. For example, if we have a data frame defined as df then we can read it with xtable as xtable(df,digits=-10).Loading xtable package −library(xtable)Example1data1
Read MoreHow to create sets using vector values in R?
A set in mathematics is defined as the collection of unique elements and the order of the elements does not matter. In R, we can create sets using set_power function of sets package. For example, if we have a vector x that contains A, B, C then the sets using the vector x can be created by using set_power(x).Loading sets package −library(sets)Examplesx1
Read MoreHow to create a random sample of values between 0 and 1 in R?
The continuous uniform distribution can take values between 0 and 1 in R if the range is not defined. To create a random sample of continuous uniform distribution we can use runif function, if we will not pass the minimum and maximum values the default will be 0 and 1 and we can also use different range of values.Examplesrunif(5) [1] 0.8667731 0.7109824 0.4466423 0.1644701 0.5611908 runif(10) [1] 0.5923782 0.8793613 0.6912947 0.2963916 0.6076762 0.7683766 0.1143595 [8] 0.4782710 0.1143383 0.4540217 runif(50) [1] 0.841674685 0.325249762 0.640847906 0.203868249 0.495230429 0.897175830 [7] 0.744447459 0.490173680 0.254711280 0.144844443 0.867749180 0.004405166 [13] 0.539785687 0.739637398 0.062214554 0.648021581 0.768686809 0.305543906 [19] 0.757496413 0.527085302 0.633331579 0.700118363 0.857950259 0.929350618 [25] 0.167015719 0.775870043 0.430343200 0.528408273 0.600575697 0.612206968 [31] 0.065904791 0.061135682 0.082027863 0.193586800 0.013956337 0.156875620 [37] 0.837501421 0.971202297 0.930835689 0.292126061 0.599263353 0.826630821 [43] 0.509235736 0.741715013 0.224485511 0.113099235 0.395143355 0.375654137 [49] 0.973050494 0.107550270 round(runif(50),2) [1] 0.51 0.70 0.90 0.45 0.41 0.74 0.31 0.40 0.10 0.05 0.18 0.05 0.63 0.34 0.57 [16] 0.06 0.73 0.37 0.79 0.85 0.82 0.41 0.32 0.34 0.37 0.14 0.21 0.11 0.43 0.86 [31] 0.83 0.09 0.88 0.04 0.62 0.64 0.15 0.75 0.78 0.16 0.67 0.97 0.79 0.64 0.56 [46] 0.40 0.07 0.69 0.82 0.63 round(runif(50),4) [1] 0.2951 0.2916 0.9049 0.2669 0.7613 0.2080 0.4739 0.1110 0.6155 0.5429 [11] 0.4490 0.2941 0.8262 0.7719 0.7896 0.7634 0.6260 0.7812 0.7600 0.6852 [21] 0.9142 0.0165 0.2324 0.0821 0.0814 0.4009 0.3315 0.8843 0.9684 0.1966 [31] 0.4841 0.5795 0.7898 0.1865 0.6929 0.8599 0.0492 0.8275 0.7431 0.3122 [41] 0.8480 0.3327 0.4872 0.0503 0.1887 0.0296 0.6011 0.1162 0.7776 0.6874 round(runif(50),5) [1] 0.40368 0.33585 0.03557 0.06047 0.95041 0.18260 0.70011 0.75148 0.12414 [10] 0.01310 0.42343 0.05846 0.21341 0.05454 0.77823 0.66151 0.61406 0.59459 [19] 0.50299 0.96780 0.43033 0.64652 0.39697 0.05897 0.47169 0.79828 0.74154 [28] 0.56074 0.97303 0.35301 0.36110 0.67452 0.14553 0.45195 0.05780 0.90489 [37] 0.96745 0.28014 0.02089 0.77789 0.04797 0.03550 0.40495 0.08924 0.59908 [46] 0.89074 0.48498 0.47335 0.59422 0.00719 round(runif(100),2) [1] 0.10 0.06 0.51 0.89 0.80 0.68 0.97 0.58 0.60 0.79 0.96 0.48 0.29 0.16 0.42 [16] 0.35 0.46 0.18 0.46 0.34 0.48 0.35 0.72 0.10 0.50 0.93 0.30 0.54 0.85 0.19 [31] 0.12 0.10 0.47 0.66 0.43 0.09 0.44 0.86 0.99 0.31 0.10 0.61 0.20 0.15 0.02 [46] 0.25 0.33 0.75 0.98 0.23 0.21 0.70 0.42 0.24 0.87 0.84 0.99 0.06 0.75 0.48 [61] 0.84 0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 0.5 0.9 0.3 0.8 0.9 0.3 0.9 0.7 [58] 0.6 0.8 0.4 0.4 0.7 0.4 0.1 0.2 0.6 0.6 0.9 0.3 0.6 0.5 0.9 0.2 0.3 0.2 round(runif(75),3) [1] 0.712 0.355 0.130 0.768 0.134 0.681 0.273 0.663 0.849 0.851 0.842 0.430 [13] 0.371 0.903 0.148 0.879 0.812 0.330 0.567 0.646 0.199 0.159 0.056 0.448 [25] 0.637 0.204 0.101 0.389 0.797 0.030 0.021 0.167 0.440 0.359 0.670 0.435 [37] 0.807 0.669 0.738 0.546 0.535 0.969 0.055 0.201 0.436 0.336 0.841 0.548 [49] 0.901 0.850 0.369 0.770 0.678 0.922 0.252 0.132 0.635 0.544 0.291 0.715 [61] 0.601 0.399 0.585 0.161 0.423 0.244 0.451 0.397 0.951 0.382 0.123 0.959 [73] 0.252 0.330 0.238
Read MoreHow to set the alignment of labels in horizontal bar plot to left in R?
When we create a horizontal bar plot using ggplot2 package, the labels of the categorical variable are aligned to the right-side of the axis and if the size of these labels are different then it looks a little ambiguous. Therefore, we might want to set the alignment of the labels to left-side and this can be done by using theme function of ggplot2 package.ExampleConsider the below data frame:> df dfOutput x y 1 India 14 2 UK 15 3 Russia 12 4 United States of America 18Loading ggplot2 package and creating a horizontal ...
Read MoreHow to check the difference between plot generation time in base R?
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:
Read More