R ggplot - Error stat_bin requires continuous x variable

后端 未结 4 604
南方客
南方客 2020-12-28 18:50

My table is data.combined with following structure:

\'data.frame\':   1309 obs. of  12 variables:
 $ Survived: Factor w/ 3 levels \"0\",\"1\",\"None\": 1 2 2         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-28 19:31

    extractTitle <- function(Name) {     
    Name <- as.character(Name) 
    
      if (length(grep("Miss.", Name)) > 0) { 
        return ("Miss.")
      } else if (length(grep("Master.", Name)) > 0) { 
        return ("Master.") 
      } else if (length(grep("Mrs.", Name)) > 0) { 
        return ("Mrs.") 
      } else if (length(grep("Mr.", Name)) > 0) { 
        return ("Mr.") 
     } else { 
        return ("Other") 
      } 
    }
    
    titles <- NULL 
    
    for (i in 1:nrow(data.combined)){
      titles <- c(titles, extractTitle(data.combined[i, "Name"]))
    }
    
    data.combined$title <- as.factor(titles)
    
    ggplot(data.combined[1:892,], aes(x = title, fill = Survived))+
           geom_bar(width = 0.5) +
            facet_wrap("Pclass")+
             xlab("Pclass")+
             ylab("total count")+
             labs(fill = "Survived")  
    

提交回复
热议问题