【问题标题】:plotting a histogram with rCharts?用 rCharts 绘制直方图?
【发布时间】:2014-09-17 23:26:17
【问题描述】:

我正在尝试使用 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)

我收到此错误:“‘xAxis’不是引用类“Polycharts”的有效字段或方法名称”

我该如何解决?

【问题讨论】:

    标签: r rcharts


    【解决方案1】:

    以下是您在 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
    

    【讨论】:

    • 谢谢!一个问题:这是一个条形图,条形之间有空格。是否可以删除该空间,使其看起来更像直方图?是否可以更改鼠标悬停在栏上时弹出的文本?例如,第一个小节应该是 [10-20]
    • 是的,在我的演示中使用devtools::install_github("timelyportfolio/rCharts@dimple_v2.0.0 设置 barGap 0 是实验性的,但请注意这是实验性的。会考虑在下个月左右投入生产
    【解决方案2】:

    我不愿将其作为答案而不是评论,但这里是我们如何在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
      )
    

    【讨论】:

    • 我有几个问题。 dPlot 和 rPlot 有什么区别?我在哪里可以阅读您提到的格式选项?是否可以制作直方图而不是条形图? (条之间没有空格)谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    相关资源
    最近更新 更多