Found 33676 Articles for Programming

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

364 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

482 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

325 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

272 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

916 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

How to split a string vector that contain strings of equal sizes and have spaces between values then extract only few values in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:48:52

150 Views

A string vector can contain any value including spaces. Sometimes a string vector is read with some spaces as well and we want to split the vector then extract few values. For example, if a string has “ABC 123” then we might want to extract the number 123 so that we can use it in analysis. If the string vector has strings of equal sizes then it can be easily done with the help of substr function.Examples> x1 x1 [1] "1 00" "1 01" "1 02" "1 03" "1 03" "1 04" > Numeric_Last_two_x1 Numeric_Last_two_x1 [1] "00" "01" "02" "03" ... Read More

How to find the total of frequency based on the values of a factor column in R?

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

469 Views

Often, we have duplicate values in a factor column that means a factor column has many levels and each of these levels occur many times. In this situation, if we have a frequency column then we want to find the total of that frequency based on the values of a factor column and this can be done by using aggregate function.Example Live DemoConsider the below data frame −> set.seed(109) > Class Frequency df1 df1Output    Class Frequency 1     E       9 2     D       5 3     B       10 ... Read More

How to convert unit of measurements of a value or a vector in R?

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

744 Views

There are many units of measurements for a single object or item. For example, weight can be measured in milligrams, grams, kilograms, tons, oz, lbs, etc. Now suppose we have two variables that belong to the same unit of measurement as weight of a coca cola cans and weight of apple juice, if the weights given for both of these variables have different units like one having grams and the other having oz then we might want to convert one of them. This will help us to compare both the variables easily without conflicting the scale of measurements. Therefore, we ... Read More

How to display the legend of a bar plot in a colored box in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:38:07

347 Views

When we create a bar plot or any other plot with legend, the background of the legend is white but it can be changed to any color with the help of scales package. We can make changes in the legend of a plot using alpha in legend.background argument of theme function. This will help us to change the background color of the legend.Example Live Demo> x y df dfOutput   x  y 1   0 25 2 100 28 3 150 32 4 200 25Creating a bar plot with legend −> library(ggplot2) > ggplot(df, aes(x, y, fill=x))+geom_bar(stat="identity")OutputChanging the background color of the ... Read More

How to represent X-axis label of a bar plot with greater than equal to or less than equal to sign using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:27:27

861 Views

The values of the categorical variable can be represented by numbers, by characters, by a combination of numbers and characters, by special characters, by numerical signs or any other method. But when we create the bar plot, if the size of a label name is large then we might want to reduce it by representing it with a different word or character or sign that gives the same meaning and it can be done by using expression argument inside scale_x_discrete.ExampleConsider the below data frame − Live Demo> x y df dfOutput   x  y 1 0   25 2 100 28 3 ... Read More

Advertisements