0

I'm trying to make one plot of two datasets to compare their values.

ggplot()
ggplot(pos_plot, aes(x=WORD, y=FREQ)) +
  geom_bar(position="dodge", colour="blue", stat = "identity") +
ggplot(neg_plot, aes(x=WORD, y=FREQ)) +
  geom_bar(position="dodge", colour="red", stat = "identity")

But when I run this code I get the error:

Error: Don't know how to add o to a plot

Does anyone know what I'm doing wrong?

RHA
  • 3,677
  • 4
  • 25
  • 48
Rob
  • 1
  • 1
  • 2
  • 1
    This will be easier to do if you merge the two data sets into one and create a factor identifying the source data set. Then you can use `facet.grid` to make a diptych, or use `fill = [factor id'ing source]` to compare them in a single plot. – ulfelder May 08 '17 at 12:04
  • How do you want to combine the two plots? Two plots side by side, or one plot with bars side by side? – Jeremy Voisey May 08 '17 at 14:29

1 Answers1

0

You clearly need to study the grammar of graphics a bit more. The + sign in ggplot can't be used to just add another ggplot to your plot. You could use facet_grid as mentioned by @ulfelder in comments, or grid.arrange from the gridExtra package:

require(ggplot2)
library(gridExtra)
library(grid)

dfpos <- data.frame(x=1,y=1)
dfneg <- data.frame(x=-1,y=-1)

ppos <- ggplot(dfpos, aes(x,y)) + geom_point()
pneg <- ggplot(dfneg, aes(x,y)) + geom_point()

grid.arrange(ppos,pneg)

Or, rewrite your own code to:

ppos <- ggplot(pos_plot, aes(x=WORD, y=FREQ)) +
        geom_bar(position="dodge", colour="blue", stat = "identity")
pneg <- ggplot(neg_plot, aes(x=WORD, y=FREQ)) +
        geom_bar(position="dodge", colour="red", stat = "identity")
grid.arrange(ppos,pneg)
RHA
  • 3,677
  • 4
  • 25
  • 48