计算R中分组条形图中的条数并绘制 [英] Counting the number of bars in a grouped bar chart in R and plotly

查看:690
本文介绍了计算R中分组条形图中的条数并绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在下面运行这个脚本时,它给了我两个分组的条形图。我只想要一个带有每个组中的条数的向量。很明显,附加快照以供参考,矢量将具有两个计数值。

 库(闪亮)
库(shinydashboard)
库(ggplot2)
图书馆(情节)
ui< - dashboardPage(
dashboardHeader(title =Sankey Chart),
dashboardSidebar(
width = 0
),
dashboardBody(
fluidRow(
column(10,
uiOutput('box1'),
tags $ br()),
tags $ br(),
列(10,


box(title =Case Analyses,status =primary,solidHeader =
T,width = 1050,height = 452 ,
plotlyOutput(case_hist))
))


服务器< - 函数(输入,输出)
{
输出$ case_hist< - renderPlotly(
{

iris $ iris_limits< - cut(iris $ Sepal.Length,c(1,3,6,9))$ b $ (iris,aes(x = ID,y = Sepal.Length,fill = iris_limits))+
(1:nrow(iris))
gg < - ggplot geom_bar(stat =identity,position =dodge)+
facet_wrap(〜iris_limits,scales =free_x,labeller = label_both)+
theme_minimal()+ xlab( )+ ylab(Sepal Length)+
主题(axis.text.x = element_blank())
ggplotly(gg)
}

输出$ box1< - renderUI({
tagList(
infoBox(Total Cases,a,icon = icon(fa fa-briefcase))

})

shinyApp(ui,server)

解决方案

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

  gg < -  ggplot(df,aes(alpha,bravo))#然而你让你的情节
temp< - data.frame(ggplot_build(gg)$ data [[1]])
table(temp $ PANEL)

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

When I run this script below, It gives me two grouped bar charts. I simply want a vector with the count of number of bars in each group. Attaching the snapshot for reference, clearly, the vector will have two count values. Please help.

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)

解决方案

What you're effectively trying to do here is extract data from the plot element. In this case, we can do this by extracting the plot data and storing it in a temporary data frame, then making a table of the PANEL variable to see how many elements are in each panel.

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

edit: the plot I was playing with to create this answer had the data applied as the third element, thus I originally had a 3 instead of 1 in line two of the code. Replace that number with whichever layer has your data. If you do a simple plot that only has one element (like yours with geom_bar() first above) then your first element should be the data and the code above should work.

这篇关于计算R中分组条形图中的条数并绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆