Last Updated
Viewed 17,040 Times
        

I was trying to make 2 separate plots which I want to present side by side in my poster (I need to make them separate and cannot make use of facet_wrap). One of the plots has several boxplots, while the second plot only has one. How can I manipulate the width of the boxplots such that the second boxplot is the same dimension as the width of any one of the individual boxplots in plot 1, when I put the two plots side by side? A reproducible example:

tvalues <- sample(1:10000,1200)
sex <- c(rep('M',600),rep('F',600))
region <- c('R1','R2','R3','R4','R5')
df1 <- data.frame(tvalues,sex,region)

tvalues2 <- sample(1:10000,200)
sex2 <- sample(c('M','F'),200,replace=T)
region2 <- 'R6'
df2 <- data.frame(tvalues2,sex2,region2)

p1 <- ggplot(data=df1,aes(x=region,y=tvalues,color=sex)) + 
geom_boxplot(width=0.5)
p2 <- ggplot(data=df2,aes(x=region2,y=tvalues2,color=sex2)) + 
geom_boxplot(width=0.5)

Plot 1 plot 1:

Plot2 plot 2:

Similar Question 1 : ggplot2 boxplot

I am trying to plot boxplot using ggplot2. sample data is like this.

> sampe

count genotype
71       mt
50       mt
71       mt
95       wt
60       mt
63       mt
75       mt
82       wt
93       wt
87       wt
61       mt
102       wt
60       mt
78       wt
78       wt
87       wt
84       wt
104       wt
81       wt
85       mt


> qplot(factor(genotype),count,data=sampe,geom="boxplot")

The above command produces plot like this: enter image description here

what's wrong here?? why is it plotting like this?? Even this below code produces same output.

ggplot(sampe,aes(x=factor(genotype),y=count))+geom_boxplot()

Similar Question 2 : Boxplot with ggplot2

I am working on a boxplot with forecast and observations which is quite long dataset. I am providing a sample format here.

> forecasts <- data.frame(f_type = c(rep("A", 9), rep("B", 9)), 
                          Date = c(rep(as.Date("2007-01-31"),3), rep(as.Date("2007-02-28"), 3), rep(as.Date("2007-03-31"), 3), rep(as.Date("2007-01-31"), 3), rep(as.Date("2007-02-28"), 3), rep(as.Date("2007-03-31"), 3)), 
                          value = c(10, 50, 60, 05, 90, 20, 30, 46, 39, 69, 82, 48, 65, 99, 75, 15 ,49, 27))
> 
> observation <- data.frame(Dt = c(as.Date("2007-01-31"), as.Date("2007-02-28"), as.Date("2007-03-31")), 
                            obs = c(30,49,57))

So far I have:

ggplot() + 
    geom_boxplot(data = forecasts,
                 aes(x = as.factor(Date), y = value, 
                     group = interaction(Date, f_type), fill = f_type)) +  
    geom_line(data = observations,
              aes(x = as.factor(Dt), y = obs, group = 1), 
              size = 2)

With this the box and whiskers are set by default. I want to assign these values so that I will know the extent of the whiskers. I have tried to pass a function with stat_summary with like:

f <- function(x) {
    r <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
    names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
    r
}

o <- function(x) {
    subset(x, x < quantile(x,probs = 0.05) | quantile(x,probs = 0.95) < x)
}

ggplot(forecasts, aes(x = as.factor(Date), y = value)) + 
    stat_summary(fun.data = f, geom = "boxplot", aes(group = interaction(Date, f_type), fill = f_type)) +
    stat_summary(fun.y = o, geom = "point") 

But, with this the groups are messed up. This produces stacked up plots. Does anyone how to accomplish this?

Similar Question 3 : Creating a boxplot, ggplot2

I have the following data set:

depth <- data.frame(Sample = c("AD_001", "AD_009", "AD_017", "AD_025", 
                           "AD_033", "AD_041", "AD_049", "AD_057", 
                           "AD_065", "AD_073", "AD_081", "AD_089"), 
                median = c(12, 13, 11, 12, 12, 12, 13, 13, 14, 15, 15, 13), 
                granular_first_quartile = c(5, 6, 5, 6, 5, 6, 6, 6, 7, 7, 7, 6), 
                granular_third_quartile = c(23, 25, 21, 22, 23, 23, 24, 25, 27, 28, 28, 24))

and would like to create a boxplot but, the graphs I'm generating do not have an equally separated x field.

ggplot(depth, aes(as.factor(Sample))) + geom_boxplot(aes(middle = median, lower = granular_first_quartile, upper = granular_third_quartile, ymin = granular_first_quartile, ymax = granular_third_quartile), stat = 'identity') + coord_flip()

Thanks for the help!

Similar Question 5 (1 solutions) : Boxplot of table using ggplot2

Similar Question 6 (1 solutions) : Sorting a ggplot2 boxplot [duplicate]

Similar Question 7 (2 solutions) : Label boxes in ggplot2 boxplot

Similar Question 8 (1 solutions) : Boxplot of CSV data with ggplot2

Similar Question 9 (1 solutions) : ggplot2 boxplot facet wrap

cc