R语言 图形中的千位分隔符

xam8gpfp  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(200)

我正在做一个R闪亮的应用程序,我有一个交互式图表在ggplotly。我想作为一千个分隔符空间,我有一个代码,这是不是真的工作。
下面是我的代码:

mrg <- list(l=10, r=10, b=10, t=100, pad=1)
      
        output$sortie3_evo <- renderPlotly({
          g <- ggplot(filtered_data_evo(), aes(x=Année.Scolaire, y= Nombre.élèves, fill=Secteur,label = format(as.numeric(..y..), nsmall=0, big.mark=" ")))+
          stat_summary(fun = sum, geom = "bar", position = position_dodge(0.9))+
            theme(axis.text.x = element_text(angle=25, hjust=1))+
            facet_wrap(~ NiveauLV, ncol = 2) +
            xlab("")

            
g <- ggplotly(g) %>%
              layout(margin = mrg)

          })

它给出了以下结果:

鼠标悬停的最后一行显示了正确的东西,但我不明白为什么它也显示了代码。
下面是我使用dput()时的部分数据:

structure(list(Année.Scolaire = c("2018-2019", "2018-2019", "2018-2019", 
"2018-2019", "2018-2019", "2018-2019", "2018-2019", "2018-2019", 
"2018-2019", "2018-2019"), Secteur = structure(c(2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("PUBLIC", "PRIVE SOUS CONTRAT"
), class = "factor"), Département = c("ARDECHE", "ARDECHE", "HAUTE SAVOIE", 
"SAVOIE", "HAUTE SAVOIE", "HAUTE SAVOIE", "ISERE", "ISERE", "ISERE", 
"SAVOIE"), Cycle = c("Cycle 2", "Cycle 3", "Cycle 2", "Cycle 3", 
"Cycle 1", "Cycle 3", "Cycle 1", "Cycle 2", "Cycle 3", "Cycle 1"
), LV = c("Allemand LV1", "Allemand LV1", "Allemand LV1", "Allemand LV1", 
"Allemand LV1", "Allemand LV1", "Allemand LV1", "Allemand LV1", 
"Allemand LV1", "Allemand LV1"), Nombre.élèves = c(6, 6, 6, 324, 
66, 3, 21, 258, 18, 204), NiveauLV = c("LV1", "LV1", "LV1", "LV1", 
"LV1", "LV1", "LV1", "LV1", "LV1", "LV1"), LV_recode = c("Allemand", 
"Allemand", "Allemand", "Allemand", "Allemand", "Allemand", "Allemand", 
"Allemand", "Allemand", "Allemand")), row.names = c(NA, 10L), class = "data.frame")

先谢谢你。

5sxhfpxr

5sxhfpxr1#

您应该使用text美学,并且可以使用paste创建标签悬停文本,如下所示:

mrg <- list(l=10, r=10, b=10, t=100, pad=1)
library(ggplot2)
library(plotly)
g <- ggplot(filtered_data_evo, aes(x=Année.Scolaire, y= Nombre.élèves, fill=Secteur,text = paste("label:", format(after_stat(y), big.mark=" "))))+
    stat_summary(fun = sum, geom = "bar", position = position_dodge(0.9))+
    theme(axis.text.x = element_text(angle=25, hjust=1))+
    facet_wrap(~ NiveauLV, ncol = 2) +
    xlab("")

g <- ggplotly(g) %>%
    layout(margin = mrg)
g

创建于2023年1月20日,使用reprex v2.0.2

    • 编辑**

要在悬停文本中选择某个aes,您可以使用tooltip,其美学效果如下所示:

g <- ggplotly(g, tooltip = c("x", "fill", "text")) %>%
    layout(margin = mrg) 
g

相关问题