Switch to full style
Examples of R Scripting Language
Post a reply

ggplot2 for many boxplots in one figure example

Sun Apr 02, 2017 2:57 am

To plot a ggplot2 Boxplot, you can look at the following snippet.


This R script loads several libraries, including foreign, MASS, bootES, ggplot2, plyr, reshape2, scales, RColorBrewer, beanplot, crop, directlabels, grid, and moments. The script then loads a dataset named "NewCalculation" which is then used to create a box plot using ggplot2 library. The box plot shows the relationship between the variable "trainingSize" and "recall" from the loaded dataset. The plot is further customized by setting the x and y axis labels, titles, and adding a horizontal line with a specific y-intercept value, and changing the theme and color. The final plot is saved in the variable "p".

The script loads a dataset named "NewCalculation" which is not provided in the script, and creates a ggplot object with the data, using the "factor(trainingSize)" and "recall" as the x and y variables respectively. The plot is a boxplot with fill color "#ffff99" and y-axis limit between 0 and 1. The plot also includes a horizontal line with y-intercept "yvals" and color "RED" and with linetype "dashed". The plot also includes labels for the x and y axis and a title for the plot, and it also changes the text size of the legend to 14.

Code:
require(foreign)
require(
MASS)
require(
bootES)
library(ggplot2)
library(plyr) # might be not needed here anyway it is a must-have package I think in R 
library(reshape2) # to "melt" your dataset
library (scales) # it has a "rescale" function which is needed in heatmaps 
library(RColorBrewer) # for convenience of heatmap colors, it reflects your mood sometimes
require(beanplot)
library(plyr) # For the desc() function
library(ggplot2)
library(crop)
library(directlabels)
library(scales)
library(grid)
library(moments)

### Load your data here
## I loaded here data named"NewCalculation".


  p<- ggplot(NewCalculation, aes(factor(trainingSize), recall))+ geom_boxplot(fill="#ffff99")+ylim(0,1)+theme_bw()+
      labs(color = "Type",title =  ", Title here,")+ geom_hline(yintercept = yvals,color="RED",linetype="dashed")+
      ylab("Ylabel text")+xlab("Xlabel text")+ theme(legend.position = "bottom",text = element_text(size=14))
    
    p

ggplot2_boxplot.jpg
ggplot2_boxplot.jpg (39.75 KiB) Viewed 7711 times

In this example, I actually print multiple boxplots from the data same data-frame. I used the x-axis by passing the "factor(trainingSize)" to aes function. Filled the boxplots with yellow color using "fill=...". The other parameters as just the traditional ones for any ggplot function.



Post a reply
  Related Posts  to : ggplot2 for many boxplots in one figure example
 Good looking HeatMap example in R using ggplot2     -  

Topic Tags

R Analysis