使用ggplot2创建一个带有条形子弹的条形图

时间:2014-08-15 00:52:54

标签: r ggplot2 geom-bar

我想用ggplot2创建一个带有SDM的条形图来自一组数据(Y中的$ proteinN和X中的$ method) 并在图例中包含相同的条形图(重叠),并在图例中包含子弹条形图形状的另一组数据($ specific)。 有点像这样(但是垂直条和第一组数据的SDM)

http://www.yaksis.com/static/img/03/large/BulletBarChart.png

这是我的代码和数据:

    library(ggplot2) 
    data <- textConnection("proteinN, supp, method, specific
    293, protnumb, insol, 46
    259, protnumb, insol, 46
    274, protnumb, insol, 46
    359, protnumb, fasp, 49
    373, protnumb, fasp, 49
    388, protnumb, fasp, 49
    373, protnumb, efasp, 62
    384, protnumb, efasp, 62
    382, protnumb, efasp, 62
    ")

    data <- read.csv(data, h=T)

# create functions to get the lower and upper bounds of the error bars
stderr <- function(x){sqrt(var(x,na.rm=TRUE)/length(na.omit(x)))}
lowsd <- function(x){return(mean(x)-stderr(x))}
highsd <- function(x){return(mean(x)+stderr(x))}

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", 
               "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

# create a ggplot
ggplot(data=data,aes(x=method, y=proteinN, fill=method))+
  #Change _hue by _manualand remove c=45, l=80 if not desire#
  scale_fill_manual(values=cbPalette)+
  scale_fill_hue(c=45, l=80)+

  # first layer is barplot with means
  stat_summary(fun.y=mean, geom="bar", position="dodge", colour='black')+
  # second layer overlays the error bars using the functions defined above
  stat_summary(fun.y=mean, fun.ymin=lowsd, fun.ymax=highsd, 
              geom="errorbar", position="dodge",color = 'black', size=.5)

我确实尝试了一些但没有任何工作,当我尝试添加第二组数据时,我总是得到这个错误输出:

错误:将变量映射到y并使用stat =“bin”。   使用stat =“bin”,它将尝试将y值设置为每个组中的个案数。   这可能会导致意外行为,并且在将来的ggplot2版本中将不允许这样做。   如果您希望y表示案例计数,请使用stat =“bin”并且不要将变量映射到y。   如果您希望y表示数据中的值,请使用stat =“identity”。   有关示例,请参阅?geom_bar。 (已解散;最后一次在0.9.2版本中使用)

错误:将变量映射到y并使用stat =“bin”。   使用stat =“bin”,它将尝试将y值设置为每个组中的个案数。   这可能会导致意外行为,并且在将来的ggplot2版本中将不允许这样做。   如果您希望y表示案例计数,请使用stat =“bin”并且不要将变量映射到y。   如果您希望y表示数据中的值,请使用stat =“identity”。   有关示例,请参阅?geom_bar。 (已解散;最后一次在0.9.2版本中使用)

这是我的尝试:

# create functions to get the lower and upper bounds of the error bars
stderr <- function(x){sqrt(var(x,na.rm=TRUE)/length(na.omit(x)))}
lowsd <- function(x){return(mean(x)-stderr(x))}
highsd <- function(x){return(mean(x)+stderr(x))}

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", 
               "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# create a ggplot
ggplot(data=data,aes(x=method, y=proteinN, fill=method, witdh=1))+
  #Change _hue by _manualand remove c=45, l=80 if not desire#
  scale_fill_manual(values=cbPalette)+
  scale_fill_hue(c=45, l=80)+

  #Second set of data#
  geom_bar(aes(x=method, y=specific, fill="light green"), width=.4) +

  # first layer is barplot with means
  stat_summary(fun.y=mean, geom="bar", position="dodge", colour='black')+

  # second layer overlays the error bars using the functions defined above
  stat_summary(fun.y=mean, fun.ymin=lowsd, fun.ymax=highsd, 
      geom="errorbar", position="dodge",color = 'black', size=.5)

1 个答案:

答案 0 :(得分:2)

也许尝试这样的事情?

ggplot(data=data,aes(x=method, y=proteinN, fill=method, width=1))+
  scale_fill_hue(c=45, l=80) +
  stat_summary(fun.y=mean, geom="bar", position="dodge", colour='black')+
  stat_summary(fun.y=mean, fun.ymin=lowsd, fun.ymax=highsd, 
               geom="errorbar", position="dodge",color = 'black', size=.5) + 
  geom_bar(data = unique(data[,c('method','specific')]),
           aes(x = method,y = specific),
           stat = "identity",
           fill = "light green",
           width = 0.5)

几个笔记。

你错了拼写&#34;宽度&#34;。

你的两条scale_fill行毫无意义。 ggplot只会占用一个填充比例,无论哪一个出现在最后。你不能修改&#34;这样的填充比例。你应该收到一个明确说明的警告:

  

缩放&#39;填充&#39;已经存在了。为“填充”添加另一个比例,这将取代现有比例。

您说的错误消息:

  

将变量映射到y并使用stat =&#34; bin&#34;

即。您在y = proteinN中使用stat = "bin"时指定了geom_bar(默认值)。它继续解释:

  

使用stat =&#34; bin&#34;,它会尝试将y值设置为每组中的案例数。

即。而不是在y中绘制,它会尝试计算insol的实例数,然后绘制它。 (三,在这种情况下。)粗略检查?geom_bar中的示例,立即显示大多数示例仅指定x变量。直到你在帮助中得到这个例子:

# When the data contains y values in a column, use stat="identity"
library(plyr)
# Calculate the mean mpg for each level of cyl
mm <- ddply(mtcars, "cyl", summarise, mmpg = mean(mpg))
ggplot(mm, aes(x = factor(cyl), y = mmpg)) + geom_bar(stat = "identity")

它表明当您指定所需的精确y值时,您还必须说stat = "identity"。方便的是,错误消息表示:

  

如果您希望y表示数据中的值,请使用stat =&#34; identity&#34;。

最后一篇文章知道,由于重叠的条形每x值只有一个值,我们应该通过以下方式将该文件折叠到所需的最小信息:

unique(data[,c('method','specific')]

或者只是提前将其拆分为自己的数据框。