簡體   English   中英

ggplot2 R完整和丟失數據的條形圖

[英]ggplot2 R barplot of complete and missing data

我使用以下代碼為每個變量創建了一個缺失計數圖:

ui =  fluidPage(plotOutput("missing"))

server = function(input, output, session){

data <- reactive({
      var.missing<- sapply(readData(),function(x)sum(is.na(x)))
      var.missing<- var.missing[order(var.missing)]
      missing.df<- data.frame(variable=names(var.missing),missing=var.missing,stringsAsFactors=FALSE)
      #missing.df$variable<- factor(missing.df$variable,levels=missing.df$variable,ordered=FALSE)

    })


    output$missing <- renderPlot({
      ggplot(data=as.data.frame(data()),aes(x=(factor(variable,levels=variable,ordered=FALSE)),y=missing)) +
      geom_bar(stat="identity") + labs(x="Variables",y="Number of Missing Values") + 
      theme(axis.text.x=element_text(angle=45, hjust=1))
      })
}

缺少計數圖

我的要求還是創建一個包含總值和缺失值的分布式條形圖,但是我無法實現。您能幫我以下代碼行中缺失的內容嗎?

var.missing<- sapply(readData(),function(x)(sum!(is.na(x))-sum(is.na(x))))

測試數據

str(airquality)
Output
'data.frame': 153 obs. of 6 variables:
 $ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
 $ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
 $ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
 $ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
 $ Month : int 5 5 5 5 5 5 5 5 5 5 ...
 $ Day : int 1 2 3 4 5 6 7 8 9 10 ...
> head(airquality)
Output
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6

謝謝,

我猜您想做的是創建一個合並圖,顯示同一圖中每個變量的缺失值和非缺失值的數量。

這是使用空氣質量數據的解決方案:

library(shiny)

ui =  fluidPage(plotOutput("missing"))

server = function(input, output, session) {

    data <- reactive({
    # Difference of not missing and missing data - Note that
    # ! is inside the sum function
    var.diff<- sapply(airquality, function(x) (sum(!is.na(x)) - sum(is.na(x))))
    # Missing data
    var.missing <- sapply(airquality, function(x) sum(is.na(x)))

    # Repetitions - used in coloring bars
    type <-c(rep("(var.missing", length(var.missing)), rep("var.diff", 
             length(var.diff)))

    # Used for plotting both missing and diff together
    values <- c(var.missing, var.diff)

    new.df <- data.frame(variable = names(var.missing), values)

  })

  output$missing <- renderPlot({
    ggplot(new.df, aes(variable, values)) + geom_bar(stat = "identity", aes(fill = type), 
      position = "dodge") +
      labs(x = "Variables", y = "Values") + 
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
  })

} 

shinyApp(ui,server)
data <- reactive({
  #Complete data
  var.nonmissing<- sapply(readData(), function(x)sum(!is.na(x)))
  # Missing data
  var.missing <- sapply(readData(), function(x)sum(is.na(x)))
  # Repetitions - used in coloring bars
  Category <-c(rep("Missing", length(var.missing)), rep("Complete", 
                                                        length(var.nonmissing)))
  # Used for plotting both missing and diff together
  values <- c(var.missing, var.nonmissing)
  new.df <- data.frame(variable = names(var.missing), values,Category)

})


output$miss <- renderPlot({

  ggplot(data(), aes(variable, values)) + geom_bar(stat = "identity", aes(fill = Category), 
                                                   position = "stack") +
    ggtitle("Missing and Non-Missing Distribution")+
    geom_text(aes(label=values),angle = 90,check_overlap = TRUE,colour="blue",fontface = "oblique",family = "Impact",position = position_stack(vjust = 0.5)) +
    labs(x = "Variables", y = "Count") + 
    theme(plot.title = element_text(family = "Helvetica", face = "bold", size = (18)), 
          legend.title = element_text(colour = "navy",  face = "bold.italic", family = "Helvetica"), 
          legend.text = element_text(face = "italic", colour="navy",family = "Helvetica"), 
          axis.title = element_text(family = "Helvetica", size = (10), colour = "navy"),
          axis.text.x = element_text(family = "Helvetica", colour = "#993333", size = (8),angle = 55, hjust = 1),
          axis.text.y = element_text(family = "Helvetica", colour = "#993333", size = (10)))

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM