Found 2038 Articles for R Programming

How to create a dot plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 09:28:00

426 Views

A dot plot is a type of histogram that display dots instead of bars and it is created for small data sets. In ggplot2, we have geom_dotplot function to create the dot plot but we have to pass the correct binwidth which is an argument of the geom_dotplot, so that we don’t get the warning saying “Warning: Ignoring unknown parameters: bins `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.”ExampleConsider the below data frame −> x df1 library(ggplot2)Creating the dot plot of x −> ggplot(df1, aes(x))+geom_dotplot(binwidth=0.2)OutputLet’s have a look at one more example −> y df2 ggplot(df2, aes(y))+geom_dotplot(binwidth=0.2)OutputRead More

How to determine the percentiles of a vector values in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 09:26:13

184 Views

Percentile helps us to determine the values that lie at a certain percent in a data set. For example, if we have a vector of size 100 with containing any values and suppose that the tenth-percentile of the vector is 25, which means there are ten percent values in the vector that are less than 25, or we can say, there are ninety percent values in the vector that are greater than 25. We can find percentiles of a vector values using quantile function in R.Examples Live Demo> x1 x1Output[1] 7 1 7 6 6 5 3 1 5 5 4 ... Read More

How to delete different rows and columns of a matrix using a single line code in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 09:24:37

1K+ Views

Deletion or addition of rows and columns in a matrix of any size is mostly done by using single square brackets and it is also the easiest way. To delete rows and columns, we just need to use the column index or row index and if we want to delete more than one of them then we can separate them by commas by inserting them inside c as c(-1, -2). If we want to delete more than one rows or columns in a sequence then a colon can be used.Examples Live Demo> M MOutput   [, 1] [, 2] [, 3] [, ... Read More

How to extract p-values for intercept and independent variables of a general linear model in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 09:19:37

381 Views

General linear model does not assume that the variables under consideration are normally distributed, therefore, we can use other probability distributions to create a general linear model. We should actually say that if the data does not follow normal distribution then we can try different distributions using general linear model and check whether the model is appropriate or not. The p-values plays an important role in selecting the best model and we might want to extract them from the model object. This can be done by using coef function.ExampleConsider the below data frame − Live Demo> set.seed(123) > var1 var2 var3 ... Read More

How to check whether the elements of a vector are arranged in an increasing order or decreasing order?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 11:53:03

691 Views

A vector can contain values that are increasing or decreasing in nature or they can be also random which means a higher value may come after a lower one which is followed by a higher value. An example of increasing arrangement of elements of vector is 1, 2, 3 and the opposite of that would be decreasing arrangement. We can check whether a vector is arranged in increasing order or decreasing order by checking whether the difference between all values of the vector is greater than or equal to zero or not and it can be done by using diff ... Read More

How to create two vertical lines on a plot with shaded area in-between using R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 09:09:03

250 Views

Sometimes we want to place a vertical rectangle on a plot that has different color as compared to the rest of the plot area. This vertical rectangle is created based on the conditional values of x axis and represent the pivot area or unimportant area depending on the characteristics of the data. These values of x variable are placed as vertical lines on the plot and the area between these lines is shaded. It can be done by using geom_rect function.ExampleConsider the below data frame − Live Demo> x y df dfOutput  x y 1 2 9 2 3 7 3 ... Read More

How to create a plot in R with gridlines using plot function?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 09:06:54

375 Views

Any plot created by using plot function does not display the plot with gridlines. On the other hand, if we create a plot using ggplot2 package then the plot has gridlines. Therefore, if we want to have gridlines on our plot then either we should create the plot using ggplot2 package or we can use the command grid() to add the gridlines on the plot created by plot function.ExampleCreating a histogram using plot function −> hist(rnorm(100))OutputAdding the grid lines to the above plot −> grid()Output

How to extract the frequencies from a histogram in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:58:57

222 Views

When we create a histogram and save it in an object name then we can extract the frequencies as count for the mid values or breaks by calling that object. We can consider that mid values or breaks obtained by the object are the actual value against which the frequencies are plotted on the histogram.Examples> x1 Histogram1 Histogram1Output$breaks [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 $counts [1] 45 82 150 156 172 142 113 62 43 20 9 5 1 $density [1] 0.045 0.082 0.150 0.156 0.172 0.142 0.113 0.062 0.043 0.020 ... Read More

How to add a horizontal line to the plot created by ggplot2 in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:54:26

215 Views

When we create a plot, it shows the values passed by the function for creating the plot but we might want to display some other values to provide some information through the plot and that information could be a threshold value as a horizontal line or we can also call it a cut off value. This can be done by using geom_hline function of ggplot2 package.ExampleConsider the below data frame −> x y df dfOutput      x       y 1 0.27810573 2.6545571 2 1.39185082 3.4845292 3 -0.19068920 1.7043852 4 1.00791317 1.4324814 5 -1.74964913 1.7996093 6 -0.13123079 2.5004350 ... Read More

How to generate a sequence of a date in each month for a fixed number of months using R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 13:27:07

740 Views

Every month have common dates except few such as February do not have 30 or 31 and even 29 in some years and there are months that contain 30 days while some contains 31 days. Therefore, finding a date say the first date, a middle date, or a last date is not an easy task but it can be done with the help of seq function in base R.Examples Live Demo> seq(as.Date("2020-01-01"), length=12, by="1 month")Output[1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" [6] "2020-06-01" "2020-07-01" "2020-08-01" "2020-09-01" "2020-10-01" [11] "2020-11-01" "2020-12-01"Example Live Demo> seq(as.Date("2020-01-01"), length=36, by="1 month") Output[1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" [6] ... Read More

Advertisements