How to create a field graph based only on min and max

In ggplot, we can create a hatch plot by specifying a colum in a data frame that has a bar height

library("ggplot2")
library(plyr)
mm <- ddply(mtcars, "cyl", summarise, mmpg = mean(mpg))
ggplot(mm, aes(x = factor(cyl), y = mmpg)) + geom_bar(stat = "identity")

However, I cannot figure out how to make a similar plot, which is indicated by both the upper and lower columns. For example, using the data below

df <- read.table(text = " id  min  max 
    Sp1     8.5          13.2     
 Sp2     11.7          14.5     
 Sp3     14.7          17.7     ", header=TRUE)

We get a plot close to this: enter image description here

Any suggestions?

+3
source share
2 answers

You can use geom_crossbar:

ggplot(df) +
  geom_crossbar(aes(ymin = min, ymax = max, x = id, y = min),
                fill = "blue", fatten = 0)

enter image description here

+5
source

You can use geom_boxplotit if you pinpoint everything aes.

df$med = 0.5*(df$min+df$max)
ggplot(df, 
       aes(x=id, ymin=min, lower=min,fill=id ,
           middle=`med`, upper=max, ymax=max)) +
  geom_boxplot(stat="identity")

enter image description here

+2
source

All Articles