0

I am trying to create a bar plot with 1 set of data, and a line plot overlaid on top with another set of data.

I can create the plots separate no problem, but can't combine them. See bar plot code below:

bar = read.csv("bar.csv", header = TRUE)
bar2 = df %>% 
  gather(desc, value, c(-Station, -Treatment)) %>% 
  separate(desc, c("Stat", "Var")) %>% 
  spread(Stat, value)

xx = ggplot(bar2, aes(x = Var, y = Avg, ymin = Avg-Std, ymax = Avg+Std)) 
+ facet_wrap(~Station) 
+ geom_bar(colour = "black", stat = "identity", position = "dodge", width = 0.65) 
+ geom_errorbar(colour = "black", stat = "identity", position = position_dodge(0.65), width = 0.4) + theme_bw() + scale_y_continuous(expand = c(0,0))

xx

And then the line plot:

line = read.csv("line.csv", header = TRUE)
line2 = line %>% 
      gather(desc, value, c(-Station, -HC, -Treatment)) %>% 
      separate(desc, c("Stat", "Var")) %>% 
      spread(Stat, value)

xx + geom_line(hc2, aes(x=Var, y=Avg), colour = HC) + facet_wrap(~Station)

Where I would like there to be 2 coloured lines showing the different HC variables. When I go to add the line plot I keep getting this error:

Error in list2(na.rm = na.rm, orientation = orientation, ...) : 
  object 'HC' not found

Here is what my data looks like:

bar2 line2

  • 2
    Please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). Please include the code, console output, or data (e.g., `data.frame(...)` or the output from `dput(head(x))`) directly into a [code block]. – r2evans Jul 11 '23 at 19:23
  • 2
    `gather` and `spread` have been superseded (or retired) for well over 4 years, recommending a shift to `pivot_longer` and `pivot_wider`, I think you'll find those newer functions more powerful and perhaps understood by more people these days. I wonder if your `gather` is to blame. But to complicate it all ... you show images of `bar2` and `line2`, we do not see `bar`, and you never define `df`, suggesting your code has patches from different interactive sessions. – r2evans Jul 11 '23 at 19:29
  • 1
    Please make this question more reproducible by including usable sample data (from `dput`, `data.frame`, or `read.table`, preferably), consistent code, and your expected output. It really helps when you have verified the code in a new (fresh!) session to make sure you have not missed objects (such as `df` above). See https://stackoverflow.com/q/5963269 , [mcve], and https://stackoverflow.com/tags/r/info for good discussions on sample data/code/packages, etc. – r2evans Jul 11 '23 at 19:30
  • hi @r2evans thank you for the advice and apologies for my lack of clarity. I did not realize 'gather' and 'spread' were outdated. I am having trouble getting the same dataframe (from bar > bar2) using pivot_longer/pivot_wider.. do you recommend I make a new question for this? –  Jul 11 '23 at 20:51
  • Usable sample data, please. I know nothing about `df` at all, and I'm not going to try to transcribe images. For pivoting/reshaping, I really like these links: **long-to-wide**: https://stackoverflow.com/q/5890584/3358272, https://stackoverflow.com/q/11608167/3358272; **wide-to-long**: https://stackoverflow.com/q/2185252/3358272, https://stackoverflow.com/q/68058000/3358272 – r2evans Jul 11 '23 at 21:15
  • Please consider going back to your [past questions](https://stackoverflow.com/users/20796953/emmlemore?tab=questions) and [accepting](https://stackoverflow.com/help/someone-answers) an answer for each question. While not required, it is "expected" to do so, and not only gives the answerer some rep-points for their volunteer time, it also signals to follow-on readers that your problem is resolved, and which (if multiple) answer worked best for you. Upvotes are also appreciated, and you can choose to upvote zero or more of the answers provided (though at most one can be accepted). Thanks! – r2evans Jul 11 '23 at 21:34
  • hi @r2evans thank you for the advice. I will go back and do so. apologies I am still new to the stack etiquette. I will be deleting this question and and reposting with reproducible examples once I figure out how to do so... thanks. –  Jul 11 '23 at 21:43
  • You don't need to delete, you can [edit] it in place, but over to you. – r2evans Jul 11 '23 at 21:46

0 Answers0