天际计算与ggplot2& rPref:美学错误

时间:2016-01-22 11:13:40

标签: r ggplot2

我正在尝试计算数据集的天际线,并使用rPref包和ggplot2在结果图中突出显示它,但是当我输入以下代码来绘制它并突出显示天际线时:

library(rPref)
library(ggplot2)

dfsmall <- df[sample(nrow(df), 10000), ]
subm <- dfsmall$R
cit <- dfsmall$C
sky3 <- psel(dfsmall, low(subm) * high(cit))
ggplot(dfsmall, aes(cit, subm)) + geom_point(shape = 21) + geom_point(data = sky3, size = 3)

我收到以下错误:

Error: Aesthetics must be either length 1 or the same as the data (9): x, y

我尝试过使用该软件包网站上给出的相同示例,并且绘制得很好。我最初使用的数据集有多达360万个条目,这就是我尝试先使用小样本的原因。我尝试使用整个样本,但得到了同样的错误。

这是未突出天际线的采样数据图: plot

以下是数据集的示例:

Journal|C|R
Community Ecology|3.1520957042244797|1.1328168254666038
Community Ecology|3.152093636630696|1.132817463161266
Community Ecology|3.152093630174369|1.132817463785396
Community Ecology|2.8492035704599306|1.0418903023831696
Community Ecology|2.8491927435707853|1.041893282606882
Community Ecology|3.2152403526503557|1.072928508197194
Community Ecology|3.2152403526503557|1.072928508197194
Community Ecology|3.2043813702700636|1.067889752202103
Community Ecology|3.292976984658889|1.0805480894094015
Community Ecology|3.2929769846588894|1.0805480894094015
Community Ecology|3.292968394110873|1.080553371763456
Community Ecology|3.2929684104602917|1.080553375230888
Community Ecology|3.297486496322724|1.0805838373798877
Proceedings of the National Academy of Sciences USA|5.492578948402146|1.1232782448162892
Proceedings of the National Academy of Sciences USA|5.492578948154322|1.1232782442984
Proceedings of the National Academy of Sciences USA|5.4923822992218385|1.123268132762782
Proceedings of the National Academy of Sciences USA|5.492382345441327|1.123268107954781
Proceedings of the National Academy of Sciences USA|19.52012153924722|1.568480417634541
Proceedings of the National Academy of Sciences USA|19.5504286947169|1.5660562041325001
Proceedings of the National Academy of Sciences USA|19.55023863424425|1.5661010273152816
Proceedings of the National Academy of Sciences USA|19.550234273504078|1.5661015512961107
Proceedings of the National Academy of Sciences USA|19.55023427354027|1.5661015512974155
Proceedings of the National Academy of Sciences USA|19.55023427425949|1.5661015541914598
Proceedings of the National Academy of Sciences USA|19.366373694833726|1.5325085337940505
Proceedings of the National Academy of Sciences USA|19.55023427425949|1.5661015541914598
Proceedings of the National Academy of Sciences USA|19.55023418539019|1.566101654690083
Proceedings of the National Academy of Sciences USA|20.20619146067687|1.5564061442955064
Proceedings of the National Academy of Sciences USA|20.20619146020598|1.5564061447061452
Proceedings of the National Academy of Sciences USA|24.330346939445825|1.5855625617064044
Proceedings of the National Academy of Sciences USA|24.32796209135422|1.586084138236795
Proceedings of the National Academy of Sciences USA|24.324974707463298|1.5859368964111258
Proceedings of the National Academy of Sciences USA|24.320932825399186|1.5898833974860256
Proceedings of the National Academy of Sciences USA|24.320932825629683|1.589883396830437
Proceedings of the National Academy of Sciences USA|24.32199772077184|1.590389367315143
Proceedings of the National Academy of Sciences USA|24.321996213258043|1.5903893303395582
Proceedings of the National Academy of Sciences USA|24.322276840047028|1.590790509355881
Proceedings of the National Academy of Sciences USA|24.322275635565717|1.590789589521394
Proceedings of the National Academy of Sciences USA|24.322275650939638|1.5907895961484653
Proceedings of the National Academy of Sciences USA|24.26729643814093|1.5773525076139876
Proceedings of the National Academy of Sciences USA|24.263382706510537|1.5766546748542565
Proceedings of the National Academy of Sciences USA|24.269034760069243|1.5774257022984333
Proceedings of the National Academy of Sciences USA|24.266333939069302|1.579844362870521
Proceedings of the National Academy of Sciences USA|24.26612828632516|1.5797397498768895
Proceedings of the National Academy of Sciences USA|24.261214163494014|1.5789216112362954
Proceedings of the National Academy of Sciences USA|24.26121414055694|1.5789216119032887
Proceedings of the National Academy of Sciences USA|24.261214140575703|1.5789216118424365
Proceedings of the National Academy of Sciences USA|24.25712119587152|1.5785021041094625
Proceedings of the National Academy of Sciences USA|24.256397836120925|1.5795320150755814
Proceedings of the National Academy of Sciences USA|24.256397482593627|1.5795319168545505
Proceedings of the National Academy of Sciences USA|24.260178541404912|1.5761494844739465
Proceedings of the National Academy of Sciences USA|24.260178541506633|1.5761494845123885
Proceedings of the National Academy of Sciences USA|14.335716420107927|1.6854220559754898
Journal of Animal Ecology|16.099904196469833|1.6345032588963908
Journal of Animal Ecology|16.102442348742738|1.634700557296763
Journal of Animal Ecology|18.034389329767645|2.1570774733559017
Journal of Animal Ecology|18.034389329767645|2.1570774733559017
Journal of Animal Ecology|18.034388552493283|2.157077902450765
Journal of Animal Ecology|18.034388552489773|2.1570779024468623
Journal of Animal Ecology|18.034388554816502|2.157077905399307
Journal of Animal Ecology|18.04311563259971|2.134537616887214
Journal of Animal Ecology|18.057234708641843|2.149842770489911
Journal of Animal Ecology|18.033200658271994|2.1395765498409927
Journal of Animal Ecology|18.03933023880154|2.13940588881513
Journal of Animal Ecology|18.03933023880154|2.13940588881513
Journal of Animal Ecology|18.039069515495246|2.1394914641672864
Journal of Animal Ecology|18.039069515495246|2.1394914641672864
Journal of Animal Ecology|18.039069515495278|2.139491464167187
Journal of Animal Ecology|18.02389564721894|2.1522740818511172
Journal of Animal Ecology|18.02389169637643|2.1522761073718693
Journal of Animal Ecology|18.023891694314234|2.1522761070217933
Journal of Animal Ecology|18.023891694322828|2.1522761070230847
Journal of Animal Ecology|18.02400867028083|2.1523007166587336
Journal of Animal Ecology|18.024009503532504|2.152300330398323
Journal of Animal Ecology|18.024009507297173|2.1523003312275417
Journal of Animal Ecology|18.02400950727997|2.1523003312222544
Journal of Animal Ecology|18.024009507118695|2.152300331396728
Journal of Animal Ecology|18.02400950699615|2.1523003317192204
Journal of Animal Ecology|18.024301138614387|2.1521156173698652
Journal of Animal Ecology|18.024301133493587|2.152115617216169
Journal of Animal Ecology|13.512450926521746|1.8344544913920997
Journal of Animal Ecology|13.512450926535502|1.834454491374112
Journal of Animal Ecology|16.795519881820873|1.8587957613719486
Journal of Animal Ecology|16.79551988180648|1.8587957613692905
Journal of Animal Ecology|16.795519881801074|1.8587957613717006
Journal of Animal Ecology|16.792019761006873|1.85959488428296
Journal of Animal Ecology|16.792019704664124|1.8595948635461816
Journal of Animal Ecology|16.79389019055074|1.8596237137598375
Journal of Animal Ecology|16.793889570394562|1.8596236581975052
Journal of Animal Ecology|16.793889602680576|1.8596236750142792
Journal of Animal Ecology|16.79388960268032|1.8596236750146238
Journal of Animal Ecology|16.79388960268032|1.8596236750146238
Journal of Animal Ecology|16.793889600183306|1.859623668816706
Journal of Animal Ecology|16.793889600181853|1.8596236688175931
Journal of Animal Ecology|16.795491013809205|1.8596206991429924
Journal of Animal Ecology|16.795750768604005|1.859562598287657
Journal of Animal Ecology|16.79860512178667|1.8599365870913362
Journal of Animal Ecology|16.80115886257487|1.8597922763373367
Journal of Animal Ecology|16.801158862574685|1.8597922763373838
Journal of Animal Ecology|16.80386878605096|1.8598523370420816
Journal of Animal Ecology|16.813753416102138|1.8555393341100082
Journal of Animal Ecology|16.813887539751132|1.855544676922069
Journal of Animal Ecology|4.002516605756929|1.2523055533884906
Journal of Animal Ecology|16.922494275839764|1.8594179993986442
Journal of Animal Ecology|16.922494275894664|1.8594179995071516
Journal of Animal Ecology|16.922494230729445|1.8594180121038164
Journal of Animal Ecology|16.91929225094357|1.8590218181635865
Journal of Animal Ecology|16.91835712721651|1.8583429059964651
Journal of Animal Ecology|16.920261746467|1.8588892075899766
Journal of Animal Ecology|16.92021230614336|1.8588993898554431
Journal of Animal Ecology|16.919849127434876|1.8588876802891745
Journal of Animal Ecology|16.919849127435768|1.858887680288736
Journal of Animal Ecology|16.91984912932506|1.8588876807472192
Journal of Animal Ecology|16.919846686680515|1.858836353416164
Journal of Ecology|19.03652568307928|1.8463478065176067
Journal of Ecology|19.03652583648677|1.8463478808897091
Journal of Ecology|19.036525836475974|1.8463478808918388
Journal of Ecology|19.036525836476482|1.84634788089207
Journal of Ecology|19.099528406976958|1.8514710377655381
Journal of Ecology|19.099528406977278|1.8514710377655252
Journal of Ecology|19.099411987643677|1.8511021018112128
Journal of Ecology|24.903142208590992|2.133427242833482
Journal of Ecology|24.902662665319266|2.1331102530697557
Journal of Ecology|24.904816739928762|2.134354838867883
Journal of Ecology|24.900445232052768|2.1330482680351786
Journal of Ecology|24.900445232005442|2.133048269682403
Journal of Ecology|24.9004482218183|2.133048299210671
Journal of Ecology|24.900448221818028|2.133048299216169
Journal of Ecology|24.87881545377532|2.137001285156332
Journal of Ecology|24.87884358983232|2.1369825265984406
Journal of Ecology|24.878843589808756|2.1369825265918676
Journal of Ecology|24.87882549737359|2.136998877889485
Journal of Ecology|24.87882549737359|2.136998877889485
Journal of Ecology|24.880573871057027|2.136899606338112
Journal of Ecology|24.772113317717007|2.163533494024218
Journal of Ecology|24.772112832754427|2.1635333517845172
Journal of Ecology|24.77090164556158|2.1635522542737147
Journal of Ecology|24.770901615853482|2.1635523058730866
Journal of Ecology|24.770901614408164|2.1635523057711707
Journal of Ecology|24.770901604663926|2.163552323541336
Journal of Ecology|23.15297378450799|2.0445537516892855
Journal of Ecology|23.15297378450799|2.0445537516892855
Journal of Ecology|23.153544306529263|2.0445448910393638
Journal of Ecology|23.153184915614442|2.0445017662739042
Journal of Ecology|23.150571616402516|2.044746643302016
Journal of Ecology|22.149996100444458|1.9636030669492381
Journal of Ecology|22.149996100444476|1.9636030669492819
Journal of Ecology|22.150131368114607|1.9636363583694165
Journal of Ecology|22.13079818825254|1.9649485044479993
Journal of Ecology|22.13079818825254|1.964948504447999
Journal of Ecology|21.584070904284193|1.9101963988050434
Journal of Ecology|21.58407090131441|1.9101962853493293
Journal of Ecology|21.584070903294883|1.9101962857562031
Journal of Ecology|21.584070899289852|1.9101963751969402
Journal of Ecology|21.58406998073396|1.9101958638813608
Journal of Ecology|18.723241955070534|1.8916968388995765
Journal of Ecology|18.723081633256225|1.8917360620082861
Journal of Ecology|11.665251930052849|1.7437714040341359
Journal of Ecology|18.723081633256225|1.8917360620082861
Journal of Ecology|18.723081522317084|1.8917360080036894
Paleobiology|16.99874529178958|1.7083090323185406
Community Ecology|15.574198779545585|1.7011411837763915
Community Ecology|15.574198779545585|1.7011411837763917
Community Ecology|15.574198779545585|1.7011411837763917
Community Ecology|15.574198779545629|1.701141183776386
Community Ecology|15.574186299539985|1.7011363661432308
Community Ecology|13.813877415649849|1.690947157084035
Community Ecology|13.813877415649843|1.6909471570840613
Community Ecology|14.937290149500017|1.7805017658245699
Community Ecology|14.93729014949984|1.7805017658245716

1 个答案:

答案 0 :(得分:1)

如果我运行您的代码,sky3看起来像这样:

#                                   Journal        C        R
#4                        Community Ecology 2.849204 1.041890
#6                        Community Ecology 3.215240 1.072929
#7                        Community Ecology 3.215240 1.072929
#8                        Community Ecology 3.204381 1.067890
#10                       Community Ecology 3.292977 1.080548
#13                       Community Ecology 3.297486 1.080584 
#...

请注意列名称。然后,您将citsubm分别映射到ggplot中的x和y,这将继承到所有geoms,包括指定不同data.frame的第二个geom_pointsky3)。除了ggplot之外,geoms显然无法处理传递给data参数的data.frame之外的映射变量(无论如何你都不会想要这样)。您的代码很容易修复:

sky3 <- psel(dfsmall, low(R) * high(C))
ggplot(dfsmall, aes(x = R, y = C)) + 
  geom_point(shape = 21) + 
  geom_point(data = sky3, size = 3)