r - 带有 Shiny 和 rCharts 的 x 和 y 标签

标签 r shiny rcharts

我即将完成我想要的 Shiny 应用程序的创建。

如何向图表的 x 轴和 y 轴添加标签?

这就是我现在拥有的:

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;')
    )
  ),
  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
      })
    }
  )

  )
)

谢谢!

最佳答案

nPlot 对象有一个 xAxisyAxis 方法,它们采用一个选项 axisLabel。您可能需要调整 y 轴的宽度。

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

  )
)

enter image description here

关于r - 带有 Shiny 和 rCharts 的 x 和 y 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24105587/

相关文章:

r - 如何使用 golem 在 Flexdashboard 包装器中制作 dockerized Shiny 应用程序?

r - 使用散列对 R Shiny 应用程序的一部分进行简单的密码保护

CSS rCharts 调整与 R renderChart{} 中的 staggerLabel 函数重叠

R Markdown,chunk选项结果="asis"时输出测试结果(htest)

r - 有没有办法自动为 R Markdown 中的图像添加标识符?

r - 使用 ggplot2 的多个依赖级别旭日图/圆环图

R Shiny iframe 更新后不再显示 .pdf 源代码?

r - 当某些行包含逗号作为千位分隔符和“标志并且没有小数的行没有标志时,如何读取R中的数据

r - 更改 R 中 rCharts sankey 图中节点的颜色

rCharts-如何向 NVD3 图表添加轴标签和标题