Last Updated
Viewed 68 Times
           

I am using the following code:

 library(ggplot2)
 mtcars$carb <- as.factor(mtcars$carb)
 mtcars$am <- as.factor(mtcars$am)
 sort_table <- data.frame("carb" = c(1,2,3,4,6,8), "class" = c("class A", "class B", "class A", "class C", "class B", "class A"))
 ggplot(mtcars) + 
      geom_boxplot(aes(x = carb, y = mpg, fill = am), 
          position = position_dodge(0.9))

This results in this plot: enter image description here

How can I sort the boxplot at a higher level, i.e., by class, as defined in the sort_table data.frame? And how can I add labels that indicate each class? The desired result looks like this:

enter image description here

Please note that I need to apply this to a larger dataset, so I need to sort using the sort_table data.frame and not by "manual" ordering using something like c(1,3,8,2,6,4). Thank you.

Possible Duplicate:
Order Bars in ggplot2 bar graph

I have the following data frame (df) in R that I wish to create a box plot for:

    Entity  Mean    Min Q1      Med     Q3      Max
1   ABCD    88.65   0   75.0    100.0   100.0   100
2   BAAF    88.73   0   91.0    100.0   100.0   100
3   BCC4    70.26   0   20.0    100.0   100.0   100
4   C9FE    77.98   0   66.0    86.0    100.0   100
5   D3AA    71.97   0   51.0    82.0    100.0   100   

The code I use is as follows:

Lplot <- ggplot(df, aes(Entity, ymin=Min, lower=Q1, middle=Med, upper=Q3, ymax=Max,
         fill=Mean))
mth <- theme(axis.text.x = element_text(angle = 90, hjust = 1))
bplot <- geom_boxplot(stat="identity")
sfg <- scale_fill_gradient("Mean", low="green", high="red")

Lplot + mth + sfg + bplot

This graphs the data frame with Entity on the x-axis and the appropriate quantiles on the y-axis. However, I would like to sort the plots by Mean. I can achieve this effect by replacing aes(Entity,... with aes(factor(Mean),... but then the mean values appear on the x-axis when I still want the Entity names there.

Essentially what I am after is a boxplot sorted by Mean but labelled by Entity. How can I achieve this?

This question already has an answer here:

here is my code:

> hdata$Mono<- factor(hdata$Mon)

> ggplot(hdata, aes(y= Test1, x= Mono))+ geom_boxplot()

And this is what it is giving:

enter image description here

I want to format the x axis in a way so that it will start from 12 and go like this 12, 1, 2, 3, 4, 5, 6 ,7, 8

I have a data formatted like following:

name          site  numb  env        value     order1  order2 color
K_005_AllHuman  K   5     human       0.02941166    1   1   black
K_005_bacteria  K   5     bacteria    0.02941166    1   2   grey
K_005_oral      K   5     oral        0.02941166    1   3   blue
K_005_env       K   6     environment 0.02941166    1   4   green
K_006_AllHuman  K   6     human       0.02941166    1   1   black
K_006_bacteria  K   6     bacteria    0.02941166    1   2   grey
K_006_oral      K   6     oral        0.02941166    1   3   blue
K_006_env       K   6     environment 0.02945052    1   4   green
K_006_pathogen  K   6     pathogen    0.02945052    1   5   red
K_013_AllHuman  L   13    human       0.02928835    2   1   black
K_013_bacteria  L   13    bacteria    0.02870488    2   2   grey
K_013_oral      L   13    oral        0.02870488    2   3   blue
K_013_env       L   13    environment 0.0283742     2   4   green
K_023_AllHuman  L   23    human       0.0283742     2   1   black
K_023_bacteria  L   23    bacteria    0.0283742     2   2   grey
K_023_oral      L   23    oral        0.0283742     2   3   blue
K_023_env       L   23    environment 0.0283742     2   4   green
K_023_pathogen  L   23    pathogen    0.0283742     2   5   red

I want to plot the data in boxplots, such that boxes are forst ordered by the site name, within this, they are ordered by the numb column, and lastly they follow ordering of env column. Finally, I want to assign to the boxes colors as in color column.

I managed to produce ordering, but still the plot doesn't look like I wanted it to (no to mention colors, which get completelty mingled, when assigned manually, since each sample doesn't allways have all of the facto level of env column).

Here is the code I produced till now:

    c_df$name <- factor(c_df$name, levels = c_df$name[order(c_df$numb)])
c_df$site <- factor(c_df$site, levels = c_df$site[order(c_df$order1)])
c_df$env <- factor(c_df$env, levels = c_df$env[order(c_df$order2)])


p <- ggplot(data = c_df, aes(x = name, y = value, fill=env)) + 
     geom_boxplot(outlier.size=NA) + theme_bw() 
p + theme(axis.title.x =element_blank(), 
          axis.text.x = element_text(angle = 90, hjust = 1, size =12), 
          legend.position="none") + 
     ggtitle("DeltaD") +ylab(expression(delta[d]))

Similar Question 4 (1 solutions) : grouped boxplot r ggplot2

Similar Question 5 (1 solutions) : R boxplot vs ggplot2 geom_boxplot

Similar Question 7 (3 solutions) : annotate boxplot in ggplot2

Similar Question 8 (1 solutions) : Creating a boxplot, ggplot2

Similar Question 9 (2 solutions) : ggplot2 boxplot

cc