找不到对象错误

时间:2016-09-30 19:35:42

标签: r ggplot2

我搜索了S.O. &安培;谷歌为此。有很多答案,但我似乎找不到能帮助我确定的实际问题。

这是我的代码:

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

这很好用。这是输出:

ggplot output

问题是,当我从geom_point更改为geom_bar(这是我真正想要的)时,我会得到此错误的不同版本:

  

因子(n100_249_pct)出错:找不到对象'n100_249_pct'

怎么了?

1 个答案:

答案 0 :(得分:1)

根据Ben的建议,使用stat = "identity"作为geom_bar()的参数。

编辑(2017-01-29)ggplot版本2.2.0有一个新功能geom_col(),这是geom_bar(stat = "identity")的简写。