Found 33676 Articles for Programming

How to display long X-axis labels in a bar chart using plotly in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:50:16

453 Views

Plotly in R is a package specifically designed to create highly-interactive and publication-quality charts. The chart can be created by using plot_ly function of the package and there are three main arguments of plot_ly defined as x, y, and type, where x refers to the X-axis, y refers to the Y-axis and type refers to the chart type but the axes values are stored in a data frame or itself a shared.ExampleLoading plotly package:> library(plotly)Consider the below data frame:Live Demo> x y df dfOutputx y 1 United States of America 501 2 United Kingdom 510 3 Republic of China 505Creating ... Read More

How to extract columns of a data frame with their names after converting it to a time series object in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:47:56

471 Views

To access columns of data frame in R, we just need to use $ sign but if the data frame is converted to a time series object then all the columns will behave as a time series, hence, we cannot simply use $ sign. For this purpose, we would need to use single square brackets and pass the appropriate column inside it. Look at the below examples to understand how it works.Example 1Consider the below data frame:Live Demo> set.seed(147) > x1 x2 x3 df1 df1Outputx1 x2 x3 1 5 11 4 2 5 5 3 3 4 6 2 4 ... Read More

How to create a function in R with two inputs?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:42:36

5K+ Views

To create a function with two inputs, we just need to provide two different arguments inside function. For example, if we want to create a function to find the square of a+b then we can use x and y inside function. Check out the below examples to understand how we can do it.Example1Live Demo> F F(x=1, y=1) > F(x=2, y=3) > F(x=c(1, 2), y=c(2, 3))Output[1] 4 [1] 25 [1] 9 25Example> F(x=rpois(50, 2), y=rpois(50, 7))Output[1] 36 169 121 36 49 100 144 169 144 81 100 256 121 121 36 64 49 225 121 [20] 16 64 100 36 ... Read More

How to extract variables of an S4 object in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:38:45

3K+ Views

Suppose we want to create an S4 with defined name data and two numerical columns called by x and y then we can use setClass("data", representation(x1="numeric", x2="numeric")). Now, if we want to extract the variables of this S4 object then we would need to use @ sign instead of $ sign as in a data frame.Example1> setClass("data1", representation(x1="numeric", x2="numeric")) > data1 data1OutputAn object of class "data1" Slot "x1": [1] -0.586187627 0.853689097 -0.602612795 -2.194235741 -1.318522292 [6] -0.984882420 0.273584140 0.364691611 1.025472248 1.198547297 [11] -0.709282551 -0.001441127 -0.201348012 1.296811172 1.520093861 [16] 2.071031215 0.472877022 0.616211695 0.642165615 -0.122773000 Slot "x2": [1] 0.38902289 0.20631450 0.02105516 0.24891420 ... Read More

How to remove rows in an R data frame using row names?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:36:10

16K+ Views

There are a lot of ways to subset an R data frame and sometimes we need to do it by removing rows. In general, the rows are removed by using the row index number but we can do the same by using row names as well. This can be done by storing the row names that should be removed in a vector and then removing through subsetting with single square brackets as shown in the below examples.ExampleConsider the below data frame:> x y row.names(df) dfOutput x y A ... Read More

How to display star/asterisk sign (*) inside a base R plot?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:19:07

3K+ Views

To display characters inside a base R plot we can simply use text function with expression and if we want to display an asterisk then we need to put the asterisk within double quotes. For example, if we want to display three stars then only expression(paste("***"))) should be used. Check out the below examples to understand how it works.Example1> plot(1:10,type="n") > text(8,9,expression(paste(Sig.^"***")))OutputExample2> plot(1:10,type="n") > text(5,6,expression(paste(Less_Sig.^"**")))OutputExample3> plot(1:10,type="n") > text(2,3,expression(paste(Very_Less_Sig.^"**")))Output

How to create a bar plot with ggplot2 using stat_summary in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:16:27

1K+ Views

There are multiple ways to create a bar plot in R and one such way is using stat_summary of ggplot2 package. In this function, we need to supply a function for the y-axis and to create the bars we must use geom="bar". The main thing is to decide which function should be used for y-axis values.ExampleConsider the below data frame:Live Demo> x y df dfOutput x y 1 Female 3 2 Male 3 3 Female 7 4 Male 3 5 Female 8 6 Female 5 7 Male 11 8 Male 6 9 Male 5 ... Read More

How to create heatmap in base R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:14:41

339 Views

A heatmap is a diagrammatic representation of data where the values are represented with colours. Mostly, it is used to display data that has slight variation and applied on matrix data. We can draw it for a full matrix, an upper triangular matrix as well as a lower triangular matrix. This can be done with the help of image function.Example1Live Demo> M MOutput [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 7 5 4 4 3 6 5 8 3 5 [2,] 8 7 3 5 7 4 5 2 6 6 [3,] 3 2 4 2 5 12 7 3 10 2 [4,] 5 3 6 9 5 9 2 4 5 8 [5,] 3 8 5 5 4 4 4 1 2 5 [6,] 2 3 2 4 7 8 5 8 4 4 [7,] 5 6 4 4 7 3 4 8 8 2 [8,] 4 5 2 10 5 3 5 4 6 7 [9,] 8 6 4 1 4 11 6 4 6 6 [10,] 9 5 5 4 6 2 7 3 6 5Example> image(M)Output:Example2Live Demo> M1 M1Output [,1] [,2] [,3] [,4] [,5] [,6] [1,] 24.75339 25.40680 23.76650 26.47724 24.54639 25.79895 [2,] 24.08571 25.17951 25.03599 25.63532 23.45812 25.39614 [3,] 24.53005 25.77095 26.21571 24.44029 24.69933 25.62839 [4,] 22.91202 25.49497 24.86587 25.25701 23.16166 24.34106 [5,] 25.37322 24.15308 25.58580 23.52173 25.25538 25.10577 [6,] 24.39613 26.06243 26.56054 25.19265 26.54187 24.35313Example> image(M1)Output:

How to make the axes widths in a plot wider than default in base R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:11:23

128 Views

The axes widths are generally very thin in plots but we can make them wider. This will be useful if we want to highlight the axes labels for reasons such as getting attention of the viewer on axes labels etc. To increase the width of the axes in a base R plot, we can use axis function and set the lwd argument.Example> x hist(x) > axis(side=1,lwd=4)Output:Example> axis(side=2,lwd=4)Output:

What is the difference between na.omit and complete.cases in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:10:11

876 Views

The na.omit function removes all the missing values in a data frame and complete.cases also does the same thing if applied to the whole data frame. The main difference between the two is that complete.cases can be applied to some columns or rows. Check out the below example to understand the difference.ExampleConsider the below data frame:Live Demo> set.seed(2584) > x y df dfOutput x y 1 NA 25 2 5 5 3 8 NA 4 6 5 5 4 NA 6 4 5 7 6 NA 8 4 NA 9 4 5 10 8 5 11 8 5 ... Read More

Advertisements