ggplot2与rbind获取错误:rep(值[[k]],length.out = n)

时间:2018-01-10 22:36:52

标签: r ggplot2

我正在尝试制作一个ggplot。这是我的代码: -

ggplot(Barfig4, aes(treatment, backtransformed))+ 
         geom_bar(stat="identity", position="dodge", fill=I(col), width=0.5,size=5) +
  facet_grid(treatment~., scales="free_y") +
    geom_errorbar(aes(ymin=lower, ymax=upper),width=0,size=0.5)+ 
  theme_few(base_size=14) + # adjust font size as desired
    theme(legend.position="none") + 
  theme( axis.line = element_line(colour = "black", size = 1, linetype = "solid"), 
         axis.text = element_text(size = 12, colour="black"), # adjust size if you like
         panel.border = element_rect(colour = "black", fill=NA, size=1),
         axis.ticks = element_line(size = 1, colour="black")) +
  theme(axis.text.x=element_text(angle=30,hjust=1))

这给出错误: -

  

rep(value [[k]],length.out = n)出错:         尝试复制'closure'类型的对象

我需要这样的东西: -

enter image description here 这就是我的ggplot2应该是这样的。 我已经工作了一个星期,所以请帮助:(

这是Barfig4的代码: -

Barfig4<-rbind(mating,courting)
Barfig4
Barfig4$treatment=factor(Barfig4$treatment,levels=c("control","9-tricosene","tricosane","7-tricosene","5-tricosene","2-methyloctacosane","heneicosane")) # specify desired order here
Barfig4$treatment=relevel(Barfig4$treatment,ref="control")

    upper = Barfig4$backtransformed+Barfig4$backtransformed_upper_limit # the upper limits
    upper
    lower = Barfig4$backtransformed-Barfig4$backtransformed_lower_limit # the lower limits
    lower

这是Barfig4的结果: [在此处输入图像说明] [2]

请帮助:(

> dput(droplevels(head(Barfig4, 14)))

structure(list(treatment = structure(c(NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_), .Label = character(0), class = "factor"), 
    estimate = c(-1.5971447802041, 1.08631915643811, -1.53834934761002, 
    -0.348765368851216, -0.800750492594197, -0.0122931322300098, 
    0.70984158520319, 0.177651783720163, 0.450956875702216, -0.522492270011893, 
    -0.522492270011882, -0.311183176344688, -0.0976090760466224, 
    0.333173840045831), std = c(0.240595524143666, 0.429965502362798, 
    0.896062660341878, 0.574451956012899, 0.668951762288697, 
    0.521700987658334, 0.449381607642202, 0.150284056397679, 
    0.296537640980168, 0.442231469222928, 0.442231438194413, 
    0.403261029769007, 0.368360980392784, 0.31000521266891), 
    statistic = c(-6.63829797286835, 2.52652631541004, -1.71678769319892, 
    -0.607127132566304, -1.19702277164885, -0.0235635594350468, 
    1.57959643459277, 1.18210665840736, 1.52074075389429, -1.18149047811997, 
    -1.18149056101748, -0.77166686928052, -0.264982126886897, 
    1.07473625097287), `p-value` = c(3.17325888779118e-11, 0.0115196757815383, 
    0.0860179703815371, 0.543766545174063, 0.231297679141505, 
    0.981200739433062, 0.114199316297104, 0.238906330511932, 
    0.130286415267008, 0.239150183209052, 0.239150150390535, 
    0.441442894297231, 0.791362181433913, 0.284100853393404), 
    padj = c(2.22128122145383e-10, 0.0806377304707678, 0.60212579267076, 
    1, 1, 1, 0.799395214079727, 1, 0.912004906869059, 1, 1, 1, 
    1, 1), backtransformed = c(16.8381049598515, 74.7687961273842, 
    17.6775359193695, 41.3681847902961, 30.9865004122157, 49.6926755645072, 
    67.0366154984819, 0.544296506773912, 0.61086671562056, 0.372269642519627, 
    0.372269642519629, 0.422825965246518, 0.475617086996406, 
    0.582531420029282), backtransformed_upper_limit = c(20.4801710999514, 
    81.9990726021826, 34.4729813692433, 55.6183376604594, 46.7097931866074, 
    62.4667651702418, 76.1191536281209, 0.581257050349088, 0.678632521994339, 
    0.479945564196047, 0.479945556451399, 0.523003213247331, 
    0.567277487065846, 0.655471735842067), backtransformed_lower_limit = c(13.7318762045887, 
    65.8440812384191, 8.05859678049629, 28.4302795983261, 18.6987873977282, 
    36.9585805747054, 56.4749361112238, 0.506841504816302, 0.538528279224913, 
    0.2759334211022, 0.275933427301519, 0.328617577822115, 0.385570519640422, 
    0.505791897762753), panel = structure(c(1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("a", "b"), class = "factor"), 
    col = c("grey", "red", "red", "red", "blue", "blue", "blue", 
    "grey", "red", "red", "red", "red", "blue", "blue")), .Names = c("treatment", 
"estimate", "std", "statistic", "p-value", "padj", "backtransformed", 
"backtransformed_upper_limit", "backtransformed_lower_limit", 
"panel", "col"), row.names = c(NA, 14L), class = "data.frame")



Barfig4 <- data.frame(treatment = c("control","9-tricosene","tricosane","7-tricosene","5-tricosene","2-methyloctacosane","heneicosane"),
                       backtransformed = c(16.8, 74.7, 17.7,41.3,30.9, 49.6, 67.0, 0.5, 0.6,0.3,0.3,0.4,0.4,0.5),
                       lower = c(3,8,9,12,12,12,10,0,0,0,0,0,0,0),
                       upper = c(37, 156, 52, 96,77,112,143,1,1,0,0,0,1,1),
                       col = c("grey", "red", "red", "red","blue","blue","blue","grey", "red", "red", "red","red","blue","blue"))

enter image description here 这就是我得到的,它不像我想要的那样。 上限和下限可能有问题吗?

对于颜色,我给出了这些代码: -

grey = rgb(190,190,190, maxColorValue=255)
blue = rgb(27,67,131, maxColorValue=255)
red = rgb(170,12,42, maxColorValue=255)

1 个答案:

答案 0 :(得分:2)

如果移动fill = col语句,则不会发生错误。 我创建了一个虚拟数据框来尝试复制问题

dummydat <- data.frame(treatment = c("intercept", "treatment1", "treatment2"),
                   backtransformed = c(16.8, 74.7, 17.7),
                   lower = c(15, 70, 15),
                   upper = c(18, 77, 20),
                   col = c("grey", "red", "red"))

以下代码因您之前看到的错误而失败:

ggplot(dummydat, aes(x=treatment, y=backtransformed)) + 
geom_bar(stat="identity", position="dodge", fill=col, width=0.5,size=5)

错误是:

Error in rep(value[[k]], length.out = n) : 
attempt to replicate an object of type 'closure'

但这样运行没有错误:

ggplot(dummydat, aes(treatment, backtransformed, fill=col))+
geom_bar(stat="identity", position="dodge", width=0.5,size=5) 

我使用scale_fill_manual语句来设置颜色。

<强>更新 这是使用您自己的Barfig4数据的代码

    ggplot(Barfig4, aes(treatment, backtransformed))+
    geom_bar(stat="identity", position="dodge", width=0.5,size=5, aes(fill=I(col))) +
    facet_grid(treatment~., scales="free_y") +
    geom_errorbar(aes(ymin=lower, ymax=upper),width=0,size=0.5)+ 
   theme(legend.position="none") + 
  theme( axis.line = element_line(colour = "black", size = 1, linetype = "solid"), 
         axis.text = element_text(size = 12, colour="black"), # adjust size if you like
         panel.border = element_rect(colour = "black", fill=NA, size=1),
         axis.ticks = element_line(size = 1, colour="black")) +
  theme(axis.text.x=element_text(angle=30,hjust=1))