Found 2038 Articles for R Programming

How to create an exponential curve in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 07:44:37

5K+ Views

To create an exponential curve, we can use exp function inside the plot function for the variable that we want to plot. For example, if we have a vector x then the exponential curve for the vector x can be created by using plot(x,exp(x)). We can use the exponential function for the variable that is appropriate based on the objective of the analysis, here we have shown only an example of how it works.Example1Live Demo> x plot(x,exp(x))OutputExample2Live Demo> y plot(y,exp(y))Output

How to create a staircase plot in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 07:43:03

473 Views

The simple staircase plot can be created by using geom_tile function of ggplot2 package. We just need to use the vector or the column for which we want to create the staircase plot in place of x and y as well. For example, if we have a column say x of an R data frame df then the staircase plot can be created as ggplot(df, aes(x, x))+geom_tile().ExampleConsider the below data frame:Live Demo> x df dfOutputx 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10Loading ggplot2 package and creating staircase ... Read More

How to create colored barplot using ggplot2 without legend entries in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 07:41:32

86 Views

When we create a colored barplot using ggplot2 the legend entries are automatically created. If we want to create the plot without those legend entries then theme function can be used. For example, if we have a data frame df that contains x as categorical variable and y as count variable then barplot without legend entries can be created as:ggplot(df, aes(x, y, fill=x))+geom_bar(stat="identity")+theme(legend.position="none")ExampleConsider the below data frame:Live Demo> x y df dfOutputx y 1 A 24 2 B 28 3 C 25 4 D 27 5 E 26Loading ggplot2 package and creating the barplot:> library(ggplot2) > ggplot(df, aes(x, y, fill=x))+geom_bar(stat="identity")Output:Creating ... Read More

How to create a point chart for categorical variable in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 07:40:16

363 Views

The point chart of a categorical has points corresponding to the categories just like the bar chart has bars. If we want to create point chart for categorical variable then we just need to use geom_point function of ggplot2 package. For example, if we have a data frame df that contains categorical column x and frequency column defined sd freq then the point chart for the categories in x can be ggplot(df, aes(x, freq))+geom_point().ExampleConsider the below data frame:Live Demo> set.seed(3521) > x freq df dfOutputx freq 1 B 2 2 C 12 3 A 8 4 D 12 5 C ... Read More

How to extract the first highest occurring value in an R data frame column?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 07:39:01

133 Views

The highest occurring value is called the mode and there can be multiple modes in a variable. If we have multiple modes then we can find the first mode or first highest occurring value by using sort function. For example, if we have a vector x that contains more than two modes then the first mode can be found as:sort(table(df$x), decreasing=TRUE)[1]ExampleConsider the below data frame:Live Demo> set.seed(36521) > x df1 df1Outputx 1 B 2 E 3 A 4 A 5 D 6 E 7 D 8 B 9 B 10 C 11 E 12 D 13 E 14 A 15 ... Read More

How to count the number of values that satisfy a condition in an R vector?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 07:37:47

3K+ Views

Sometimes we want to find the frequency of values that satisfy a certain condition. For example, if we have a vector say x that contains randomly selected integers starting from 1 and ends at 100, in this case we might want to find how many values are exactly equal to 10. This can be done by using which and length function.Example1Live Demo> x1 x1Output[1] 5 7 3 3 2 7 3 7 6 3Example> length(which(x1==5)) [1] 1 > length(which(x1==7)) [1] 3 > length(which(x1==3)) [1] 4Example2Live Demo> x2 x2Output[1] 4 1 5 5 5 3 8 9 8 4 8 1 ... Read More

How to remove a common suffix from column names in an R data frame?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 07:35:45

6K+ Views

To remove a common suffix from column names we can use gsub function. For example, if we have a data frame df that contains column defined as x1df, x2df, x3df, and x4df then we can remove df from all the column names by using the below command:colnames(df) x1Data x2Data x3Data df1 df1Outputx1Data x2Data x3Data 1 29.26500 26.64124 2.598983 2 21.82170 23.41442 4.134393 3 22.71918 25.21586 4.442823 4 19.88633 25.23487 3.338448 5 20.48989 23.33683 3.829757 6 29.07910 25.54084 3.519393 7 24.28573 23.67258 4.667397 8 27.99849 22.97148 4.100405 9 23.48148 25.36574 2.618030 10 26.39401 23.80191 4.235092 11 29.39867 24.36261 2.782559 12 30.11137 ... Read More

How to find the standard deviation if NA’s are present in a column of an R data frame?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 07:34:26

4K+ Views

If there exists an NA in a vector or column of an R data frame, the output of the sd command for standard deviation results in NA. To solve this problem, we need to use na.rm=TRUE as we do it for vectors that do not contain missing values. For example, if we have a column of a data frame df defined as x that contains missing values then sd of x can be calculated as sd(df$x).ExampleConsider the below data frame:Live Demo> set.seed(3521) > x df1 df1Outputx 1 NA 2 5.107864 3 4.797851 4 5.184345 5 4.680958 6 5.245151 7 5.760667 ... Read More

How to set the legends using ggplot2 on top-right side in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 07:32:44

3K+ Views

The default position of legend in a plot created by using ggplot2 is right hand side but we can change the position by using theme function that has legend.position argument and legend.justification argument. To set the legend on top-right side we can use legend.position="top" and legend.justification="right".ExampleConsider the below data frame:Consider the below data frame:Live Demo> x freq df dfOutputx freq 1 Mango 212 2 Guava 220 3 Pomegranate 218Loading ggplot2 package and creating bar chart with legend:> library(ggplot2) > ggplot(df, aes(x, freq, fill=x))+geom_bar(stat="identity")Output:Creating the bar chart with legend on top-right hand side of the chart:Example> ggplot(df, aes(x, freq, fill=x))+geom_bar(stat="identity")+theme(legend.position="top", legend.justification="right")Output:Read More

How to create a bar chart using ggplot2 with dots drawn at the center of top edge of the bars in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 07:30:58

481 Views

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 More

Advertisements