ggplot2 aes_string() fails to handle names starting with numbers or containing spaces

前端 未结 4 839
刺人心
刺人心 2020-12-15 08:32

If the column names of a data.frame are started with numbers, or have spaces, aes_string() fails to handle them:

foo=data.frame(\"1         


        
4条回答
  •  北海茫月
    2020-12-15 09:02

    As far as I can tell, this method should work programmatically:

    foo=data.frame("1st Col"=1:5, "2nd Col"=5:1, check.names=F)
    
    #Save the colnames
    bar=colnames(foo)
    
    #change the names to something usable
    names(foo) <- c("col1", "col2")
    
    #Plot with arbitrary labs
    ggplot(foo, aes(x=col1, y=col2)) + geom_point()+
      labs(x=bar[1], y=bar[2])
    

    enter image description here

提交回复
热议问题