What Are The Two Main Colors Used In Ggplot
Box plot in ggplot2 with geom_boxplot
If you take a information frame containing a numerical variable you can use geom_boxplot
to create a box plot in ggplot2, passing the variable to aes
.
# install.packages("ggplot2") library(ggplot2) # Data set.seed(iii) y <- rnorm(500) df <- data.frame(y) # Basic box plot ggplot(df, aes(y = y)) + geom_boxplot()
Alternatively y'all can set 10 = ""
. This volition remove the values of the X centrality and make the box plot thinner.
# install.packages("ggplot2") library(ggplot2) # Data set up.seed(3) y <- rnorm(500) df <- information.frame(y) # Basic box plot ggplot(df, aes(x = "", y = y)) + geom_boxplot()
Adding error bars (whiskers) with stat_boxplot
The default box plot in ggplot doesn't add the fault bar lines, merely y'all can add them with stat_boxplot
, setting geom = "errorbar"
. Note that you tin can modify its width with width
.
# install.packages("ggplot2") library(ggplot2) # Data set.seed(3) y <- rnorm(500) df <- information.frame(y) # Basic box plot ggplot(df, aes(y = y)) + stat_boxplot(geom = "errorbar", width = 0.15) + geom_boxplot()
Horizontal box plot
There are 2 primary means to change the orientation of a box plot in ggplot2: changing the aes
variable or using coord_flip
as in the examples below.
Choice 1: changing the argument.
# install.packages("ggplot2") library(ggplot2) # Data fix.seed(3) y <- rnorm(500) df <- information.frame(y) # Horizontal box plot ggplot(df, aes(x = y)) + stat_boxplot(geom = "errorbar", width = 0.15) + geom_boxplot()
Choice 2: using coord_flip
.
# install.packages("ggplot2") library(ggplot2) # Data ready.seed(3) y <- rnorm(500) df <- information.frame(y) # Horizontal box plot ggplot(df, aes(y = y)) + stat_boxplot(geom = "errorbar", width = 0.15) + geom_boxplot() + coord_flip()
Box plot customization
The ggplot box plots can be customized making use of the arguments of stat_boxplot
and geom_boxplot
. In the following examples we are changing the colors and line types of the plots, highlighting the corresponding arguments.
Colors customization
# install.packages("ggplot2") library(ggplot2) # Data set up.seed(3) y <- rnorm(500) df <- information.frame(y) # Box plot customization ggplot(df, aes(y = y)) + stat_boxplot(geom = "errorbar", width = 0.15, colour = 1) + # Mistake bar color geom_boxplot(fill = 2, # Box colour alpha = 0.5, # Transparency colour = 1, # Border color outlier.colour = 2) # Outlier colour
Lines customization
# install.packages("ggplot2") library(ggplot2) # Data set.seed(3) y <- rnorm(500) df <- data.frame(y) # Box plot lines customization ggplot(df, aes(y = y)) + stat_boxplot(geom = "errorbar", width = 0.xv, linetype = two, # Line type lwd = 0.v) + # Line width geom_boxplot(linetype = 2, # Line type lwd = 0.5) # Line width
Source: https://r-charts.com/distribution/box-plot-ggplot2/
Posted by: jacksonmulaked.blogspot.com
0 Response to "What Are The Two Main Colors Used In Ggplot"
Post a Comment