我想用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)