0

I am trying to make a ggplot. This is my code:-

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))

This gives Error :-

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

I need something like this:-

enter image description here This is how my ggplot2 should look like. I have been working for one week on this so please help :(

This is the code for 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

This is the outcome of Barfig4: [enter image description here][2]

Please help :(

> 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 This is what I get and it doesn't resemble what I wanted. Is there maybe problem with upper and lower limit?

For colors I give these codes:-

grey = rgb(190,190,190, maxColorValue=255)
blue = rgb(27,67,131, maxColorValue=255)
red = rgb(170,12,42, maxColorValue=255)
sm925
  • 2,648
  • 1
  • 16
  • 28
Risha
  • 9
  • 1
  • 3
  • Can you tell where the error comes from? Like, if you delete all your `theme` options, do you still get the error? What if you delete the facet, or the errorbar, or the bar geom? – Gregor Thomas Jan 10 '18 at 22:44
  • To really help you, we'll need you to share data reproducibly. Pasting the output of `dput(Barfig4)` into your question will let us copy/paste your data structure into R. – Gregor Thomas Jan 10 '18 at 22:44
  • Do I have to send the whole data with which I worked? – Risha Jan 10 '18 at 23:08
  • You should post a subset large enough to demonstrate your issue. – Gregor Thomas Jan 10 '18 at 23:23
  • Yes I still get error when I delete theme options:Error in rep(value[[k]], length.out = n) : attempt to replicate an object of type 'closure' – Risha Jan 10 '18 at 23:24
  • Gregor, can I send you to your e-mail address my whole project so you can run it and check for the possible error? – Risha Jan 10 '18 at 23:26
  • Nope, you definitely cannot just send to my email. If you wish to hire me as a consultant, you can contact me through linked in. Just type `dput(droplevels(head(Barfig4, 14)))` into your R console and paste the result into your question. Providing data to demonstrate the problem is standard practice for asking a good question. [For a full tutorial on how to make a good reproducible example in R, look here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Gregor Thomas Jan 10 '18 at 23:35
  • This is the result from R console. – Risha Jan 10 '18 at 23:50

1 Answers1

2

The error does not occur if you move the fill=col statement. I created a dummy dataframe to try to replicate the problem

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"))

The following code fails with the error you saw before:

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

The error is:

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

But this runs without error:

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

I'd use a scale_fill_manual statement to set the colors though.

UPDATE Here's the code using your own Barfig4 data

    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))
adts
  • 71
  • 5
  • Good eye! `fill = col` definitely needs to be inside `aes()`. – Gregor Thomas Jan 10 '18 at 23:51
  • Thank you :) I will now try to make this ggplot2. :) – Risha Jan 11 '18 at 00:03
  • Gregor, I did make the ggplot2 but it doesn't resemble what I expected. You used some very different values for upper and lower level. From where did you get those. – Risha Jan 11 '18 at 00:21
  • @Risha I didn't make this answer, adts did. They posted it before you posted your data and explained at the top *"I created a dummy dataframe to try to replicate the problem"*. I think they correctly found your issue, which is that `fill = col` needs to be inside `aes()`. If you make that correction in your code I think you find that it works. – Gregor Thomas Jan 11 '18 at 00:43
  • Gregor or adts: I posted the code and ggplot2, but it does not resemble what I want to make. Can someone help to make the ggplot2? – Risha Jan 11 '18 at 07:15
  • @Risha **put your `fill = col` (or `fill = I(col)`) inside `aes()`!!!** – Gregor Thomas Jan 11 '18 at 18:25
  • Hi Gregor, I did use fill=col inside aes and my ggplot2 doesn't resemble what I expected. – Risha Jan 11 '18 at 21:55