【问题标题】:Counting the number of bars in a grouped bar chart in R and plotly计算R中分组条形图中的条数并绘制
【发布时间】:2018-04-12 18:00:45
【问题描述】:

当我在下面运行这个脚本时,它给了我两个分组的条形图。我只是想要一个向量,其中包含每组中的条数。附上快照以供参考,很明显,向量将有两个计数值。请帮忙。

library(shiny)
library(shinydashboard)
library(ggplot2)
library(plotly)
ui <- dashboardPage(
dashboardHeader(title = "Sankey Chart"),
dashboardSidebar(
width = 0
),
dashboardBody(
fluidRow(
column(10,
     uiOutput('box1'),
     tags$br()),
tags$br(),
column(10,


     box(title = "Case Analyses",status = "primary",solidHeader = 
T,width = 1050,height = 452,
         plotlyOutput("case_hist"))
))
)
)
server <- function(input, output) 
{ 
output$case_hist <- renderPlotly(
{

iris$iris_limits <- cut(iris$Sepal.Length, c(1,3,6,9))
iris$ID <- factor(1:nrow(iris))
gg <- ggplot(iris, aes(x=ID, y=Sepal.Length, fill=iris_limits)) + 
geom_bar(stat="identity", position="dodge") +
facet_wrap(~iris_limits, scales="free_x", labeller=label_both) +
theme_minimal() + xlab("") + ylab("Sepal Length") +
theme(axis.text.x=element_blank())
ggplotly(gg)
}
)
output$box1 <- renderUI({
tagList(
infoBox("Total Cases", "a" , icon = icon("fa fa-briefcase"))
)
})
}
shinyApp(ui, server)

【问题讨论】:

    标签: r ggplot2 plotly ggplotly


    【解决方案1】:

    您在这里有效地尝试做的是从绘图元素中提取数据。在这种情况下,我们可以通过提取绘图数据并将其存储在一个临时数据框中来做到这一点,然后制作一个 PANEL 变量表来查看每个面板中有多少元素。

    gg<- ggplot(df, aes(alpha, bravo))  # however you made your plot
    temp <- data.frame(ggplot_build(gg)$data[[1]]) 
    table(temp$PANEL)
    

    编辑:我用来创建这个答案的情节将数据应用为第三个元素,因此我最初在代码的第二行中有一个 3 而不是 1。将该数字替换为具有您的数据的任何层。如果你做一个只有一个元素的简单绘图(比如上面的 geom_bar() 第一个元素),那么你的第一个元素应该是数据,上面的代码应该可以工作。

    【讨论】:

    • 感谢您的帮助,我已经编辑了这篇文章,请帮助我解决 R shiny 中的类似要求。
    • 如果我点击分组条形图中的任何条形“红色”或“绿色”,我应该得到上方信息框中显示的属于该分组条形图的条形总数
    • @AshminKaul 这是这里的标准程序,不编辑帖子以寻求额外支持,而是创建一个新帖子。反正我对闪亮不太熟悉,所以我不能帮你。
    • @AshminKaul,您应该发布一个新问题,而不是编辑您已经接受答案的问题。
    猜你喜欢
    • 2012-10-23
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多