如何在ggplot数据标签中显示%闪亮

时间:2014-08-13 08:14:29

标签: r ggplot2 shiny

如何使用中的使用geom_text或注释显示数据标签中的%符号,我相信可以使用获取%符号,但我不知道如何在shinyapp中获得%符号。

数据

 head(sum1)
  TicketType hrs_0to1 hrs_1to6 hrs_6to12 hrs_12to24 hrs_24to48 hrs_48to96 hrs_above96
1    Dummy-1     4.04     3.08      3.06       2.50       1.32       0.76        0.00
2    Dummy-2    21.39    32.03     23.68      18.07       7.41       3.31        1.97
3    Dummy-3     0.14     0.18      0.28       0.09       0.11       0.24        0.00
4    Dummy-4    24.95     9.30     19.50       6.02       1.43       0.27        0.00
5    Dummy-5     0.00     0.00      0.09       0.05       0.08       0.00        0.00
6    Dummy-6     0.00     0.06      0.09       0.32       0.64       2.25        3.95

数据结构

str(sum1)
'data.frame':   37 obs. of  9 variables:
 $ TicketType : Factor w/ 37 levels "Address change",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Ttype      : Factor w/ 4 levels "Complaint","Others",..: 4 4 4 2 1 1 1 1 3 4 ...
 $ hrs_0to1   : num  0.04 0.21 0 0.25 0 0 0 0 0 0 ...
 $ hrs_1to6   : num  0.03 0.32 0 0.09 0 0 0 0 0 0 ...
 $ hrs_6to12  : num  0.03 0.24 0 0.19 0 0 0 0 0 0 ...
 $ hrs_12to24 : num  0.03 0.18 0 0.06 0 0 0 0 0 0.02 ...
 $ hrs_24to48 : num  0.01 0.07 0 0.01 0 0.01 0 0.02 0 0.05 ...
 $ hrs_48to96 : num  0.01 0.03 0 0 0 0.02 0 0.01 0 0.05 ...
 $ hrs_above96: num  0 0.02 0 0 0 0.04 0.01 0.03 0 0.03 ...

这是一个有效的简化版本,然而,在server.R和ui.R中提到的代码中闪亮地执行此操作存在挑战

ggplot(data=sum1,aes_string("TicketType","hrs_0to1",fill="TicketType"))+geom_bar(stat="identity")+geom_text(aes(label= paste(sprintf("%0.2f",hrs_0to1*100),"%")))

server.R

library(shiny)
library(ggplot2)
library(scales)
library(dplyr)


a<-theme(panel.grid.minor.y=element_blank(),panel.grid.major.y=element_blank(),
         panel.background=element_rect(fill=NA,colour=NA),
         axis.ticks=element_blank(),axis.text.x = element_text(angle = 80, vjust = 1, hjust=1),
         legend.position="none")


#cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#sum1<-read.csv("E:/R/shiny/FirstTryShiny-Version-3/data/sum1.csv", header= TRUE,sep=",")
#sum1<-sum1[,-9]

shinyServer(function(input,output){

        output$text1<- renderText({
        paste("Template 1 is:",input$var)})

        output$stack<-renderPlot({

        sum1$TicketType<- reorder(sum1$TicketType, sum1[,input$var])

        p<-ggplot(data = sum1,aes_string("TicketType",input$var,fill="TicketType"))+
        geom_bar(stat="identity") +
        scale_colour_continuous(low="#56B4E9",high ="#009E73") +
        geom_text(aes_string(label=input$var),size=3.5,vjust=1,colour="black")+
        scale_y_continuous(labels=percent)+
        coord_flip()

        print(p+a)})

    output$table1<-renderDataTable({sum1})

    })

ui.R

library(shiny)
library(ggplot2)


shinyUI(fluidPage(theme="Yeti.css",

    column(12,offset=5,
    titlePanel("Distribution of Templates by Hours")),
    br(),
    h6(textOutput("text1")),

    fluidRow(

        column(4,offset=0,
               wellPanel(
                   selectInput("var","Hours",
                               choices = colnames(sum1[2:8]),selected ="hrs_0to1")))),

        column(12,offset=0,
                plotOutput("stack", height=650,width=1000)
            ),

        column(12,
               dataTableOutput("table1")
               )

    ))

0 个答案:

没有答案