闪亮和Rcharts y轴标记?

时间:2014-06-08 14:05:22

标签: r shiny rcharts

我有以下代码:

library(rCharts)
library(shiny)
X <- data.frame(Var1 = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L,3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L),
                Var2 = structure(c(1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("control","treatment1", "treatment2"), class = "factor"),
                Freq = c(0L,0L, 3L, 2L, 6L, 9L, 13L, 36L, 50L, 497L, 0L, 2L, 1L, 3L, 6L, 4L, 11L, 29L, 50L, 499L, 1L, 2L, 0L, 2L, 5L, 6L, 12L, 22L, 63L,490L)
)
runApp(
  list(ui = fluidPage(
    titlePanel("Quiz 3 grades distribution"),

    fluidRow(
      column(3,
             #helpText("Select grade in Quiz 1 before the treatment:"),    
             selectInput("select", label = h3("Grade Quiz 1 before the treatment:"), 
                         choices = list("All" = 0, "Not Perfect" = 1, "Perfect" = 2), 
                         selected = 0)
      ),

      column(9, div(showOutput("histogram","nvd3")), style = 'align:center;')
      , tags$head(tags$style(HTML(".nv-axislabel {font: 22px Arial;}"))) # to style labels
    )
  ),
  server = shinyServer(
    function(input, output, session) {
      output$histogram <- renderChart2({
        n2 <- nPlot(Freq ~ Var1, group = 'Var2', data = X, type = 'multiBarChart')
        n2$params$width <- 500
        n2$params$height <- 400
        n2$xAxis(axisLabel = "my x axis label")
        n2$yAxis(axisLabel = "my y axis label", width = 50)
        n2
      })
    }
  )

  )
)

如何更改y轴上的标记,例如,每25个而不是每50个计数?

非常感谢!

1 个答案:

答案 0 :(得分:2)

使用tickValues作为选项传递给yAxis

library(rCharts)
library(shiny)
X <- data.frame(Var1 = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L,3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L),
                Var2 = structure(c(1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("control","treatment1", "treatment2"), class = "factor"),
                Freq = c(0L,0L, 3L, 2L, 6L, 9L, 13L, 36L, 50L, 497L, 0L, 2L, 1L, 3L, 6L, 4L, 11L, 29L, 50L, 499L, 1L, 2L, 0L, 2L, 5L, 6L, 12L, 22L, 63L,490L)
)
runApp(
  list(ui = fluidPage(
    titlePanel("Quiz 3 grades distribution"),

    fluidRow(
      column(3,
             #helpText("Select grade in Quiz 1 before the treatment:"),    
             selectInput("select", label = h3("Grade Quiz 1 before the treatment:"), 
                         choices = list("All" = 0, "Not Perfect" = 1, "Perfect" = 2), 
                         selected = 0)
      ),

      column(9, div(showOutput("histogram","nvd3")), style = 'align:center;')
      ,tags$head(tags$style(HTML(".nv-axislabel {font: 22px Arial;}")))
    )
  ),
  server = shinyServer(
    function(input, output, session) {
      output$histogram <- renderChart2({
        n2 <- nPlot(Freq ~ Var1, group = 'Var2', data = X, type = 'multiBarChart')
        n2$params$width <- 500
        n2$params$height <- 400
        n2$xAxis(axisLabel = "my x axis label")
        n2$yAxis(axisLabel = "my y axis label", width = 50)
        n2$yAxis(tickValues = do.call(seq, c(as.list(range(X$Freq)), 25)))
        n2
      })
    }
  )

  )
)

enter image description here