0

I've searched both S.O. & Google for this. There are lots of answers, but I can't really seem to find one that's helping me pin down the actual problem I'm having.

Here's my code:

library(RNeo4j)
    library(tidyverse)
    library(stringr)
    library(MASS)

### uncomment the next 2 lines to reconnect to server & re-query the database
# setwd("~/Desktop/Dashrock/Neo4j")
# source("N4j_connect_query.R")

df <- naics_jll %>%
    group_by(m.mkt,c1.name) %>%
    summarize(n1_4_pct =sum(n.n1_4)/sum(n.est),
              n5_9_pct =sum(n.n5_9)/sum(n.est),
              n10_19_pct =sum(n.n10_19)/sum(n.est),
              n20_49_pct =sum(n.n20_49)/sum(n.est),
              n50_99_pct =sum(n.n50_99)/sum(n.est),
              n100_249_pct =sum(n.n100_249)/sum(n.est),
              n250_499_pct =sum(n.n250_499)/sum(n.est),
              n500_999_pct =sum(n.n500_999)/sum(n.est),
              n1000_pct =sum(n.n1000)/sum(n.est),
              n1000_1_pct =sum(n.n1000_1)/sum(n.est),
              n1000_2_pct =sum(n.n1000_2)/sum(n.est),
              n1000_3_pct =sum(n.n1000_3)/sum(n.est),
              n1000_4_pct =sum(n.n1000_4)/sum(n.est),
              ap = sum(n.ap),
              emp = sum(n.emp),
              num_firms = sum(n.est))

g <- ggplot(df)

  g + 
    geom_point(aes(x=c1.name, y=n100_249_pct, color = factor(m.mkt)), na.rm = TRUE) +
    facet_grid(. ~ m.mkt) +
    labs(x = "Industry Code (NAICS)", y = "Btwn 100-249 Employees (as % of All Companies)", title = "Company Profiles") + 
    theme(axis.text.x  = element_text(angle=90, hjust = .5, vjust=.5, size=5))

This works just fine. Here's the output:

ggplot output

The problem is, when I change from geom_point to geom_bar (which is what I really want), I get different versions of this error:

Error in factor(n100_249_pct) : object 'n100_249_pct' not found

What's wrong?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Steve
  • 575
  • 4
  • 18
  • 1
    Can you please include data and/or code that will provide us with a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) ? Also ... just a guess ... have you tried `geom_bar(stat="identity")` ... ? – Ben Bolker Sep 30 '16 at 19:43
  • Thank you! stat="identity" fixed it. – Steve Sep 30 '16 at 19:51

1 Answers1

1

Use stat = "identity" as parameter to geom_bar() per Ben's suggestion.

Edit (2017-01-29): Version 2.2.0 of ggplot has a new function geom_col() which is short-hand for geom_bar(stat = "identity").

Uwe
  • 41,420
  • 11
  • 90
  • 134
Steve
  • 575
  • 4
  • 18
  • it might (??) be helpful to cut this answer down to just "use `geom_bar(stat="identity")`" - it'd be easier to read and I don't see what you'd miss otherwise ... – Ben Bolker Sep 30 '16 at 19:51