Last Updated
Viewed 1,583 Times
        

I would like a label to appear above each box in a boxplot generated by ggplot2.

For example:

#Example data
test = c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B")
patient = c(1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3)
result =  c(5, 7, 2 ,4, 6, 7, 3, 5, 5, 6, 2 ,3)
data <- tibble(test, patient, result)

#Labels I want to include
Alabs = c(1, 3, 500)
Blabs = c(8, 16, -32)

#Plot data
ggplot(data, aes(x = factor(patient), y = result, color = factor(test))) + 
  geom_boxplot(outlier.shape = 1)

Gives the plot:

enter image description here

I would like to print the first element of Alabs above the red box for the first patient, the second element of Alabs above the red box for the second patient, the first element of Blabs above the blue box for the first patient, etc.

How do I do this?

I display a boxplot chart as following:

enter image description here

But what I would like is to have the x axis as real x axis, meaning that the space between values should be respected. Adding to this scale I would like a log scale on the x axis. How can I do this using geom_boxplot?

thanks, Jerome

Similar Question 2 : 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 3 : 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 4 (3 solutions) : annotate boxplot in ggplot2

Similar Question 5 (1 solutions) : ggplot2 width of boxplot

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

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

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

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

cc