本文介绍了使用ggplot2的R中的Boxplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我是R的新手,一直在试图制作一个boxplot。显示我正在使用的部分数据 h1 h2 h3 h4 h5 h6 h7 h8 h8 h9 h10 1 0.003719430 0.002975544 0.003049933 0.003421876 0.003421876 0.003347487 0.003645042 0.003496264 0.007364472 0.009075410 2 0.003400540 0.002749373 0.003038781 0.003328188 0.003328188 0.003400540 0.003472892 0.003400540 0.007741656 0.009333398 3分配0.003741387 0.002918282 0.003142765 0.003367248 0.003367248 0.003367248 0.003666559 0.003516904 0.008081396 0.008156223 4分配0.003870634 0.002884002 0.003187581 0.003339370 0.003567055 0.003415265 0.003794739 0.003491160 0.008348426 0.007741268 5分配0.003782963 0.002950711 0.003177689 0.003480326 0.003404667 0.003404667 0.003707304 0.003631645 0.008927793 0.007414608 6分配0.003643736 0.002884624 0.003264180 0.003416002 0.003491913 0.003416002 0.003871469 0.003795558 0.009033428 0.007135649 7分配0.003718600 0.003035592 0.00311148 2 0.003339151 0.003566821 0.003566821 0.003642710 0.003870380 0.008120209 0.008044319 8分配0.003819313 0.002979064 0.003284609 0.003360995 0.003590154 0.003437382 0.003895699 0.003590154 0.008326102 0.007791398 9分配0.003899334 0.002981844 0.003211216 0.003364131 0.003669961 0.003440589 0.003746419 0.003669961 0.008410328 0.007569295 10 0.003828488 0.002986220 0.003292499 0.003445639 0.003522209 0.003522209 0.003598778 0.003598778 0.008422673 0.007810115 当我使用默认的 boxplot 命令,那么这就是我得到的 $ p $ boxplot(df) 我一直在尝试使用 ggplot2 生成相同数据的boxplot,但它给出了一个我无法解决的错误。 library(ggplot2) df< - readRDS('data.Rda') ggplot(df)+ geom_boxplot() 错误 不知道如何自动为数据类型的对象选择比例。默认为连续错误:美学必须是长度为1或与dataProblems相同的长度:df [,6:15] 我看到 ggplot2文档 geom_boxplot 并且实现(从这个例子)我需要重新排列我的数据,比如 col1 col2 h1 0.003719430 h1 0.003400540 h1 0.003741387 h1 0.003870634 h1 0.003782963 h1 0.003643736 h2 0.002975544 h2 0.002749373 h2 0.002918282 h2 0.002884002 h2 0.002950711 h2 0.002884624 ... 并使用类似于 ggplot(df,aes(factor(col1 ),col2))+ geom_boxplot() 但这是很多工作。我相信必须有一些方法可以自动执行,而我无法找到它。任何帮助表示赞赏。 解决方案 你是对的 ggplot 被重新塑造,但它不是很难使用包 reshape2 : df ggplot(df,aes(x =变量,y =值))+ geom_boxplot() I'm new to R and have been trying to make a boxplot. A part of the data I'm using is shown h1 h2 h3 h4 h5 h6 h7 h8 h9 h101 0.003719430 0.002975544 0.003049933 0.003421876 0.003421876 0.003347487 0.003645042 0.003496264 0.007364472 0.0090754102 0.003400540 0.002749373 0.003038781 0.003328188 0.003328188 0.003400540 0.003472892 0.003400540 0.007741656 0.0093333983 0.003741387 0.002918282 0.003142765 0.003367248 0.003367248 0.003367248 0.003666559 0.003516904 0.008081396 0.0081562234 0.003870634 0.002884002 0.003187581 0.003339370 0.003567055 0.003415265 0.003794739 0.003491160 0.008348426 0.0077412685 0.003782963 0.002950711 0.003177689 0.003480326 0.003404667 0.003404667 0.003707304 0.003631645 0.008927793 0.0074146086 0.003643736 0.002884624 0.003264180 0.003416002 0.003491913 0.003416002 0.003871469 0.003795558 0.009033428 0.0071356497 0.003718600 0.003035592 0.003111482 0.003339151 0.003566821 0.003566821 0.003642710 0.003870380 0.008120209 0.0080443198 0.003819313 0.002979064 0.003284609 0.003360995 0.003590154 0.003437382 0.003895699 0.003590154 0.008326102 0.0077913989 0.003899334 0.002981844 0.003211216 0.003364131 0.003669961 0.003440589 0.003746419 0.003669961 0.008410328 0.00756929510 0.003828488 0.002986220 0.003292499 0.003445639 0.003522209 0.003522209 0.003598778 0.003598778 0.008422673 0.007810115When I use the default boxplot command then this is what I getboxplot(df)I have been trying to generate the boxplot for same data using ggplot2 but it gives an error which I am unable to resolve. Here's what I tried.library(ggplot2)df <- readRDS('data.Rda')ggplot(df) + geom_boxplot()Here's the errorDon't know how to automatically pick scale for object of type data.frame. Defaulting to continuousError: Aesthetics must either be length one, or the same length as the dataProblems:df[, 6:15]I saw the ggplot2 docs for geom_boxplot and realize (from the example) that I need to rearrange my data likecol1 col2h1 0.003719430h1 0.003400540h1 0.003741387h1 0.003870634h1 0.003782963h1 0.003643736h2 0.002975544h2 0.002749373h2 0.002918282h2 0.002884002h2 0.002950711h2 0.002884624...and use something likeggplot(df, aes(factor(col1), col2)) + geom_boxplot()But that is a lot of work. I believe that there must be some way to do this automatically which I'm not able to find. Any help is appreciated. 解决方案 You are right ggplot requires your data to be reshaped, but it isn't that hard using the package reshape2:library(reshape2)df <- melt(df)ggplot(df, aes(x=variable, y=value)) + geom_boxplot() 这篇关于使用ggplot2的R中的Boxplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 04:27