代码之家  ›  专栏  ›  技术社区  ›  Jérémz

使用ggplot2创建一个条形图,其中包含条形项目符号

  •  2
  • Jérémz  · 技术社区  · 10 年前

    我想用ggplot2从一组数据(Y中的$proteinN和X中的$method)中创建一个带有SDM的条形图 并在与图例中的指示符相同的条形图(重叠)中包含另一组数据($特定),其形状为子弹条形图。 有点像这样(但第一组数据的垂直条和SDM)


    (来源: yaksis.com )

    这是我的代码和数据:

        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。(Defunct;最后在0.9.2版中使用)

    错误:将变量映射到y,同时使用stat=“bin”。 如果stat=“bin”,它将尝试将y值设置为每组的病例数。 这可能会导致意外行为,并且在ggplot2的未来版本中不允许。 如果你想用y表示案例数,请使用stat=“bin”,不要将变量映射到y。 如果希望y表示数据中的值,请使用stat=“identity”。 看见例如geom_bar。(Defunct;最后在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 回复  |  直到 5 年前
        1
  •  2
  •   joran    10 年前

    也许可以试试这样的东西?

    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)
    

    几张纸条。

    你拼错了“width”。

    你的两个 scale_fill 台词毫无意义。 ggplot 将只采用一个填充比例,以最后出现的为准。你不能那样“修改”填充比例。你应该收到一个关于它的警告,明确地说:

    “fill”的比例已存在。为“fill”添加另一个比例,这将替换现有比例。

    您收到的错误消息是:

    将变量映射到y,并使用stat=“bin”

    i、 e.您指定 y = proteinN 同时也使用 stat = "bin" 在里面 geom_bar (默认值)。它接着解释道:

    如果stat=“bin”,它将尝试将y值设置为每组的病例数。

    i、 e.而不是绘制 价值观 在里面 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=“identity”。

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

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

    或者提前将其分割成自己的数据帧。