Total members 11893 |It is currently Fri Nov 15, 2024 8:05 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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

Attachment:
ggplot2_boxplot.jpg
ggplot2_boxplot.jpg [ 39.75 KiB | Viewed 7665 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.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

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



Topic Tags

R Analysis
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com