使用rCharts绘制直方图?

时间:2014-09-16 18:55:19

标签: r rcharts

我试图用rCharts创建直方图。这是我的代码:

Age <- structure(list(AGE_FBS001_002 = structure(1:58, .Label = c("14", 
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", 
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", 
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", 
"49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", 
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", 
"71", "76"), class = "factor"), Freq = c(1L, 4L, 8L, 19L, 35L, 
38L, 65L, 103L, 124L, 128L, 125L, 128L, 135L, 102L, 74L, 117L, 
80L, 68L, 76L, 68L, 70L, 47L, 48L, 42L, 52L, 53L, 34L, 42L, 42L, 
41L, 33L, 24L, 19L, 23L, 29L, 23L, 24L, 25L, 28L, 22L, 18L, 18L, 
13L, 10L, 8L, 3L, 6L, 8L, 5L, 8L, 1L, 1L, 2L, 2L, 1L, 4L, 2L, 
1L)), .Names = c("AGE_FBS001_002", "Freq"), row.names = c(NA, 
-58L), class = "data.frame")

rp1 <- rPlot(x="bin(AGE_FBS001_002,10", y="Freq", data=data, type="bar")
rp1$params$width <- 700
rp1$params$height <- 500
rp1$xAxis(axisLabel = "Age")
rp1$yAxis(axisLabel = "Count", width = 50)

我收到此错误:&#34;'xAxis'不是引用类“Polycharts”的有效字段或方法名称&#34;

我该如何解决?

2 个答案:

答案 0 :(得分:2)

以下是如何在Polycharts中执行此操作。

rp1 <- rPlot(x = "bin(AGE_FBS001_002, 10)", y = "Freq", data = Age, type = "bar")
rp1$guides(
  x = list(title = 'Age'),
  y = list(title = 'Count')
)
rp1$set(width = 700, height = 500)
rp1

答案 1 :(得分:1)

我毫不犹豫地将其作为答案而非评论,但在此我们可以使用dimplerCharts中完成这些工作。如果您选择这条路线,有很多格式化选项等。

  library(rCharts)
  library(dplyr)

  Age %>%
    mutate( age = cut(as.numeric(as.character(Age[,1])),seq(0,100,10)) ) %>%
    group_by( age ) %>%
    summarize( Freq = sum(Freq) ) %>%
  # pipe in our transformed data to dimple
  dPlot(
    Freq~age
    ,data = .
    ,type = "bar"
    ,xAxis = list(orderRule = "age")
    ,barGap = 0
  )