为机场站代码创建频率表和堆积条形图

时间:2017-07-24 21:33:44

标签: r ggplot2

我的目标是获取记录机场站代码的数据日志的子集。

我试图根据输入电台代码的次数绘制频率表,然后我想使用“填充”功能构建一个堆积条形图。另外,我试图将这些基数划分为4个偶数组。

数据子集如下所示:

 OPSLOG2016$Base <- c("yyc", "yyc", "ylw", "yvr", "lax", "hnl", "yvr", "yow", "yyz","yyz", "lga", "yyz", "yyz", "YYZ", "yow", "YYC", "YYZ", NA, "hux","yvr", ... <truncated>

某些基地的某些频率:

#List of 49
$ bos: num 134   
$ cun: num 205
$ fll: num 114
$ hnl: num 95
$ las: num 288
$ lax: num 218
$ lga: num 456
$ lgw: num 169
$ mbj: num 71
$ mco: num 223
$ ogg: num 99

我的代码到目前为止:

corpus = Corpus(VectorSource(OPSLOG2016$Base))
corpus = tm_map(corpus, PlainTextDocument)
basefreq = DocumentTermMatrix(corpus)
sparseBase = removeSparseTerms(basefreq, 0.999)
dfBase = as.data.frame(as.matrix(sparseBase))
qplot(dfBase, y = scale(dfBase,center = TRUE, scale = frequency())
      **#Error: ggplot2 doesn't know how to deal with data of class list Error during wrapup: cannot open the connection**


dfVecSum = lapply(dfBase, sum)
   plot(dfVecSum)
  **#Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y Error during wrapup: cannot open the connection**
ggplot(dfVecSum, aes(x = dfVecSum, y = Frequency, fill = fill)) + 
  geom_bar(position = "fill")
  **#Error: ggplot2 doesn't know how to deal with data of class list Error during wrapup: cannot open the connection**

很明显,我是新手,并且犯了许多错误。但是我希望能够朝着正确的方向前进,因为我似乎无法自己动手。

1 个答案:

答案 0 :(得分:0)

看起来你正在开始列表。也许这可以帮助你朝着正确的方向发展:

library(ggplot2)

yourlist = list(bos= 134,cun=205,fll= 114,hnl =  95)
df = as.data.frame(do.call(rbind,yourlist))
df$name = rownames(df)
colnames(df)[colnames(df)=="V1"] = "total"

ggplot(df, aes(x = name, y = total))+ geom_bar(stat="identity")

enter image description here