r - 使用 ggplot2 创建一个带有条形项目符号的条形图

标签 r ggplot2 geom-bar

我想用 ggplot2 从一组数据中使用 SDM 创建条形图(Y 中的 $ ProteinN 和 X 中的 $ method) 并在同一个条形图(重叠)中包含图例中的指标,另一组数据(特定于 $)具有项目符号条形图的形状。 有点像这样(但是垂直条和第一组数据的 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 的 future 版本中将不允许。 如果您希望 y 代表个案计数,请使用 stat="bin"并且不要将变量映射到 y。 如果您希望 y 代表数据中的值,请使用 stat="identity"。 有关示例,请参阅 ?geom_bar。 (已失效;最后一次使用于版本 0.9.2)

错误:将变量映射到 y 并且还使用 stat="bin"。 使用 stat="bin",它将尝试将 y 值设置为每组中的案例数。 这可能会导致意外行为,并且在 ggplot2 的 future 版本中将不允许。 如果您希望 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)

最佳答案

也许尝试这样的事情?

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)

一些注释。

您拼错了“宽度”。

你的两行scale_fill是没有意义的。 ggplot 将仅采用一种填充比例,以最后出现的为准。您不能像这样“修改”填充比例。您应该收到有关它的警告,其中明确指出:

Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.

您收到的错误消息是:

Mapping a variable to y and also using stat="bin"

即您指定了 y = ProteinN,同时还在 geom_bar(默认值)中使用了 stat = "bin"。它接着解释道:

With stat="bin", it will attempt to set the y value to the count of cases in each group.

即它不会绘制 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"。方便的是,错误消息这样说:

If you want y to represent values in the data, use stat="identity".

最后一点是,由于重叠的条形图每个 x 值只有一个值,因此我们确实应该通过以下方式将该部分折叠到所需的最少信息:

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

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

关于r - 使用 ggplot2 创建一个带有条形项目符号的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25319767/

相关文章:

r - 关于 ggplot 注释的更好解决方案?

r - 带有计数的堆叠条形图中 geom_text 的百分比

减少 ggplot2 中条形图组之间的空间

R CMD 安装 --build 包 --> "vignettes missing"

r - 将空条添加到(百分比)条形图(从长数据格式生成)

r - 遍历列的唯一值并创建多个列

r - 向三角形中心弯曲线(ggplot2)

r - ggplot 闪避条形图 : arrange bars by y-value per group

r - 按行填充缺失值(右/左)

R:ggplot 设置带有自定义中断的 ylim