Found 2038 Articles for R Programming

How to reverse the X-axis labels of scatterplot created by using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 11:58:11

833 Views

ExampleThere exists a possibility that one of the variables is recorded in an opposite manner and we want to create a scatterplot using that variable. Therefore, we would need to reverse that variable while plotting. Suppose that variable is an independent variable, hence it will be plotted on X-axis. Thus, to reverse the X-axis labels we can use scale_x_reverse function of ggplot2 package.Consider the below data frame −Example Live Demox

How to calculate weighted mean in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 11:50:08

7K+ Views

Weighted mean is the average which is determined by finding the sum of the products of weights and the values then dividing this sum by the sum of total weights. If the weights are in proportion then the total sum of the weights should be 1. In base R, we have a function weighted.mean to find the weighted mean in which we just need to pass the vector of values and the vector of weights.Examples Live Demox1

How to change the adjustment of the plot title using ggplot2 to align it above the Y-axis labels in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 11:26:38

500 Views

When we create a plot and put a title above it, the alignment of the title is left-adjusted by default and that lies on the edge of the plot area. But sometimes, we want to display the title on the above side of the Y-axis labels, therefore, we can use theme function and set the hjust argument accordingly.ExampleConsider the below data frame − Live Demox

How to create bar plot of means with error bars of standard deviations using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 11:19:25

404 Views

If we have summary data for a group variable then we might want to look at the errors or say differences between mean and standard deviations visually, therefore, we can create a bar plot with error bars of standard deviations. This can be done by using geom_errorbar function of ggplot2 package.ExampleConsider the below data frame − Live DemoGroup

How to add a vertical line with some value on a scatterplot created by ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 11:09:54

393 Views

When we draw a scatterplot, there might be some crucial points that we would want to display, hence we create a vertical or horizontal based on our objective. These vertical or horizontal lines can be drawn by using geom_vline or geom_hline function of ggplot2 but to add some value with them we can use geom_text function.ExampleConsider the below data frame − Live Demo> x y df dfOutput      x          y 1 1.2474363 -0.15892165 2 1.7511870 -1.18938250 3 -1.3001612 -0.32313571 4 -1.4220049 1.52915756 5 0.4355646 0.18282983 6 0.3128323 0.16467130 7 1.5099580 1.15199751 8 -0.4907705 -1.98635182 9 -1.4249190 ... Read More

How to create a bar plot using ggplot2 with percentage on Y-axis in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 11:06:57

2K+ Views

Mostly, the bar plot is created with frequency or count on the Y-axis in any way, whether it is manual or by using any software or programming language but sometimes we want to use percentages. It can be done by using scales package in R, that gives us the option labels=percent_format() to change the labels to percentage.ExampleConsider the below data frame − Live Demo> x df dfOutput x 1 2 2 3 3 3 4 1 5 2 6 4 7 4 8 4 9 2 10 3 11 3 12 4 13 3 14 4 15 4 16 1 17 3 ... Read More

How to replace a value in an R vector?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 11:02:04

8K+ Views

To replace a value in an R vector, we can use replace function. It is better to save the replacement with a new object, even if you name that new object same as the original, otherwise the replacements will not work with further analysis. As you can see in the object x5(in examples), when we replaced 5 with 3, the previous replacement of -1 with 0 returned as in original vector. Therefore, we should save it in a new object.Examples Live Demo> x1 x1Output[1] 1 2 3 4 5 6 7 8 9 10 > replace(x1, x1==5, 10)Output[1] 1 2 3 ... Read More

How to add a new column in an R data frame with count based on factor column?

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

3K+ Views

While doing the data analysis, often we have to deal with factor data and we might want to find the frequency or count of a level of factor and the other variable combination. This helps us to make comparison within and between factor levels. Therefore, we can add a new column as count to find the required frequency and it can be done by using group_by and mutate function of dplyr package.ExampleConsider the below data frame − Live Demo> Group Rating df head(df, 20)Output   Group Rating 1    A    1 2    B    6 3    C    2 ... Read More

How to create cumulative sum chart with count on Y-axis in R using ggplot2?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 10:43:14

1K+ Views

Cumulative sums are often used to display the running totals of values and these sums also help us to identify the overall total. In this way, we can analyze the variation in the running totals over time. To create the cumulative sum chart with count on Y-axis we can use stat_bin function of ggplot2 package.ExampleConsider the below data frame − Live Demo> x df head(df, 20)Output      x 1 1.755900133 2 1.185746239 3 0.821489888 4 1.358420721 5 2.719636441 6 2.885153151 7 1.131452570 8 0.302981998 9 0.433865254 10 2.373338327 11 0.428436149 12 1.835789725 13 2.600838211 14 2.108302471 15 1.164818373 16 1.547473189 ... Read More

How to deal with missing values to calculate correlation matrix in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 10:39:12

3K+ Views

Often the data frames and matrices in R, we get have missing values and if we want to find the correlation matrix for those data frames and matrices, we stuck. It happens with almost everyone in Data Analysis but we can solve that problem by using na.omit while using the cor function to calculate the correlation matrix. Check out the examples below for that.ExampleConsider the below data frame − Live Demo> x1 x2 x3 x4 df head(df, 20)Output x1     x2    x3    x4 1 2 2.6347839 4 2.577690 2 3 0.3082031 1 6.250998 3 1 0.3082031 3 7.786711 4 ... Read More

Advertisements