![]() In order to calculate the mean for each group you can use the apply function by columns or the colMeans function. In this case, you can make use of the lapply function to avoid for loops. You can also add the mean point to boxplot by group. ![]() You can change the mean function of the previous code for other function to display other measures. # abline(v = mean(x), col = 2, lwd = 2) # Entire line # abline(h = mean(x), col = 2, lwd = 2) # Entire line ![]() In the following code block we show you how to add mean points and segments to both type of boxplots when working with a single boxplot. Note that the code is slightly different if you create a vertical boxplot or a horizontal boxplot. For that purpose, you can use the segments function if you want to display a line as the median, or the points function to just add points. Nevertheless, you may also like to display the mean or other characteristic of the data. Legend("topright", legend = "Boxplot", # Position and titleįill = rgb(1, 0, 0, alpha = 0.4), # Colorīy default, when you create a boxplot the median is displayed. Main = "Customized boxplot in base R", # Title Horizontal = FALSE, # Horizontal or vertical plot Grid(nx = NULL, ny = NULL, col = "white", lty = 1, Review the full list of graphical boxplot parameters in the pars argument of help(bxp) or ?bxp. Note that there are even more arguments than the ones in the following example to customize the boxplot, like boxlty, boxlwd, medlty or staplelwd. In the following block of code we show a wide example of how to customize an R box plot and how to add a grid. # Boxplot from the R trees datasetīoxplot(trees, col = rainbow(ncol(trees)))īoxplot(stacked_df$values ~ stacked_df$ind,Ī boxplot can be fully customized for a nice result. Thus, each boxplot will have a different color. Note that you can change the boxplot color by group with a vector of colors as parameters of the col argument. Now, you can plot the boxplot with the original or the stacked dataframe as we did in the previous section. Nevertheless, you can convert this dataset as one of the same format as the chickwts dataset with the stack function. Note the difference respect to the chickwts dataset. For illustration purposes we are going to use the trees dataset. "variable" is always equal to dY.In case all variables of your dataset are numeric variables, you can directly create a boxplot from a dataframe.Here are the first lines of the melted data frame: dX variable value L1 ![]() Ggplot(df, aes(x = dX, y = value, colour = L1)) + geom_line()īut this creates a 4-variable data frame with columns: dX, variable (always equal to dY), value (here are the dY values) and L1, which contains the index of the data frame in the list. Second approach: I tried melting the data frames as follows. In that case I would reduce the number of lines to draw. OK, 36 lines in the legend might not be practical. This displays the curves correctly, but the colours are not applied (and I don't have an appropriate legend). G <- g + geom_line(data = df.list], aes(dX, dY, colour = i)) I tried something ugly that almost works: g <- ggplot() The number of rows are however identical. They roughly cover the same range but don't have the exact same values, so using a merge creates a ton of NA values. The 36 data frames do not share the same dX values. I have stored them in a list and want to display them all on the same graph with x = dX and y = dY. I have 36 different data frames that contain dX and dY variables.
0 Comments
Leave a Reply. |