R Programming Articles

Page 144 of 174

How to create a plot of binomial distribution in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 2K+ Views

A binomial distribution is based on the distribution of success and failure, the other two parameters of binomial distribution are the sample size and the probability of success. To create a plot of binomial distribution, we first need to define the density of the binomial distribution using dbinom function. The plotting can be done by using plot function with success and the density as shown in the below examples.Examplex

Read More

How to deal with missing column for row names when converting data frame to data.table object in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 562 Views

To deal with missing column of row names when converting data frame in R to data.table object, we need to use keep.rownames argument while converting the data frame. For example, if we have a data frame called df that needs to be converted to a data.table object without missing row names then we can use the below command −data.table(df,keep.rownames=TRUE)Examplelibrary(data.table) head(mtcars)Output            mpg     cyl    disp   hp    drat     wt      qsec   vs     am   gear carb Mazda RX4    21.0    6     160    110    3.90   2.620     16.46  0      1    4     4 Mazda RX4 Wag 21.0   6     160   110   3.90   2.875     17.02    0      1    4     4 Datsun     710      22.8   4    108 93 3.85   2.320    18.61     1      1    4     1 Hornet 4 Drive 21.4    6   258  110   3.08     3.215     19.44   1      0    3     1 Hornet Sportabout 18.7  8  360  175  3.15     3.440      17.02   0      0    3     2 Valiant      18.1    6    225  105   2.76     3.460      20.22   1      0    3     1Examplemtcars_data_table

Read More

How to remove the plot margin in base R between the axes and the points inside the plot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 1K+ Views

To remove the plot margin in base R between the axes and the points inside the plot, we can use xaxs and yaxs argument in plot function. Depending on the choices of the arguments xaxs and yaxs, the plot region in the respective direction is 4% larger than specified by these limits or exactly matches the "i" limits.Examplex

Read More

How to increase the thickness of histogram lines in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 2K+ Views

To increase the thickness of histogram lines in base R, we would need to use par function by defining the thickness size of the line. If we want to do so then line thickness must be defined first before creating the histogram. An example of line size could be line

Read More

How to create a cumulative sum plot in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 4K+ Views

To create a cumulative sum plot in base R, we can simply use plot function. For cumulative sums inside the plot, the cumsum function needs to be used for the variable that has to be summed up with cumulation. For example, if we have two vectors say x and y then the plot with cumulative sum plot can be created as plot(x,cumsum(y)).Examplex1

Read More

How to change the linetype for geom_vline in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Feb-2021 8K+ Views

To change the linetype for geom_vline, we can use linetype argument in geom_vline function of ggplot2 package. There are mainly six linetypes that can be used and these values are 0=blank, 1=solid (default), 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash.Consider the below data frame −x

Read More

How to create a perpendicular arrow in base R plot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Feb-2021 294 Views

To create a perpendicular arrow in base R plot, we can use arrows function. There are five arguments of arrows function that will be used to create the perpendicular arrow. The first four values define the position of the arrow and the last argument xpd allows R to create the arrow. Check out the below examples to understand how it works.Exampleplot(1:10) arrows(1,-1,1,0,xpd=TRUE)OutputExampleplot(1:10) arrows(1,-1,1,2,xpd=TRUE)OutputExampleplot(1:10) arrows(2,-1,2,2,xpd=TRUE)Output

Read More

How to fill histogram bars using ggplot2 in R with different colors?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Feb-2021 1K+ Views

When we create histogram using ggplot2 we need to pass the number of bins we want to have in the histogram and on the basis of these bin numbers the histogram will be created, these bin numbers are actually the number of bars we will have in the histogram. To fill those bars with different colors, we need to use fill argument and pass a range of values equal to the number of bins as shown in the below example.Consider the below data frame −x

Read More

How to display mean in a histogram using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Feb-2021 5K+ Views

To display mean in a histogram using ggplot2, we can use geom_vline function where we need to define the x-intercept value as the mean of the column for which we want to create the histogram. Also, we can change the size of the line for mean in the histogram by using size argument inside geom_vline function.Consider the below data frame −x

Read More

How to remove Y-axis labels in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Feb-2021 15K+ Views

When we create a plot in R, the Y-axis labels are automatically generated and if we want to remove those labels, the plot function can help us. For this purpose, we need to set ylab argument of plot function to blank as ylab="" and yaxt="n" to remove the axis title. This is a method of base R only, not with ggplot2 package.Examplex

Read More
Showing 1431–1440 of 1,740 articles
« Prev 1 142 143 144 145 146 174 Next »
Advertisements