首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要使用R中的Vegan包为地块创建图例的帮助

需要使用R中的Vegan包为地块创建图例的帮助
EN

Stack Overflow用户
提问于 2016-08-04 05:19:25
回答 1查看 1.8K关注 0票数 2

我目前正在尝试使用素食包在我的PCO图上绘制图例,但是图例上的pch和绘图上的pch是不同的。有人知道如何解决这个问题吗?我有附加的代码和相应的复制数据。非常感谢

代码语言:javascript
复制
library(vegan)
pco2 <- capscale(vec2 ~ 1, add = TRUE)
pco2
ordiplot(pco2, display = 'sites', type = 'n', cex=.75, main="Biotechnology Dissimilarity")
points (pco2, col = MyMeta$Group0, pch=MyMeta$Group0)
legend(x="bottomleft", legend=levels(MyMeta$Group), col=MyMeta$Group0, pch=MyMeta$Group0) 

下面是复制数据

代码语言:javascript
复制
vec2 <- structure(c(5.6e-05, 0.000264854, 0.031144847, 1.36e-05, 0.0077926, 
9.61e-06, 0.018901651, 0.005618188, 0.025462273, 0.078385688, 
0.004847819, 0.017850528, 0.000467735, 5.4e-05, 0.001046951, 
0.006335814, 0.03979001, 6.73e-05, 0.143216374, 0.000402185, 
5.99e-06, 0.008637892, 0.000906473, 0.030147803, 0.000696942, 
0.06038404, 0.000625457, 0.035261238, 0.042930657, 0.01282187, 
0.030221599, 0.000118513, 0.003552078, 0.002547753, 0.005850736, 
0.0001176, 0.001012019, 0.022458292, 0.023196107, 0.061736236, 
0.010543594, 0.032114583, 1.02e-05, 0.013650841, 0.000419571, 
0.000358173, 0.003873999, 0.000165441, 0.027002225, 0.026133375, 
0.028335404, 0.027503738, 0.000136987, 0.001289475, 0.048530267, 
0.000208896, 0.002173612, 0.023953018, 2.3e-05, 0.019027806, 
0.000495739, 0.011051853, 0.021856285, 0.006643201, 0.011511987, 
1.1e-05, 0.002010242, 0.003117657, 0.117475573, 0.024946637, 
0.058560863, 0.102647711, 0.000379362, 0.000603669, 1.97e-05, 
0.020976103, 0.01459725, 0.000289262, 0.003064517, 0.008196972, 
0.000302017, 0.020562333, 0.093245647, 0.008974985, 0.015369811, 
0.013341021, 0.000146334, 0.045070436, 0.002364084, 0.001069053, 
0.000525468, 0.024699631, 0.000210154, 0.013661305, 0.014193045, 
9.36e-05, 0.008298908, 0.001575832, 0.002778748, 0.026456846, 
0.05169889, 0.023670177, 0.000370742, 0.211737239, 0.011322947, 
0.022878397, 1.95e-06, 0.055530173, 2.34e-05, 8.81e-06, 0.000189184, 
0.000270935, 7.4e-05, 0.000178542, 0.000530574, 0.005837658, 
0.024640248, 0.006536165, 0.011476716, 0.003567421, 0.000424627, 
0.000295164, 6.09e-05, 1.02e-05, 0.043852798, 0.033416026, 0.011645167, 
0.000166027, 6.94e-05, 0.042856405, 0.000429471, 0.00051019, 
0.000530227, 0.006190825, 1.75e-05, 0.023080027, 4.07e-05, 0.001322897, 
0.041780286, 0.000358371, 6.89e-05, 3.47e-05, 0.00132758, 0.001339749, 
0.012596551, 0.003345775, 2.54e-05, 0.000660134, 0.031165069, 
0.000149653, 1.09e-05, 0.009427228, 6.33e-05, 0.030851467, 0.009840297, 
0.008769023, 0.006244515, 6.09e-05, 0.001459698, 0.010502741, 
0.00054124, 0.000238582, 0.000303381, 0.019191567, 5.91e-05), class = "dist", Labels = c("Abruzzo07", 
"Abruzzo14", "Andalucia07", "Andalucia14", "Aquitanie07", "Aquitanie14", 
"Aragon07", "Aragon14", "Asturias07", "Asturias14", "Austria07", 
"Austria14", "Auvergne07", "Auvergne14", "Baden-Wu07", "Baden-Wu14", 
"Baleares07", "Baleares14", "Basilicata07", "Basilicata14", "Basse Normandie07", 
"Basse Normandie14", "Bavaria07", "Bavaria14", "Berlin07", "Berlin14", 
"Bolzano07", "Bolzano14", "Bourgogne07", "Bourgogne14", "Bradenburgh07", 
"Bradenburgh14", "Bremen07", "Bremen14", "Bretagne07", "Bretagne14", 
"Burgenland07", "Burgenland14", "Campania07", "Campania14", "Canarias07", 
"Canarias14", "Cantabria07", "Cantabria14", "Carinthia07", "Carinthia14", 
"CastillaLaMancha07", "CastillaLaMancha14", "CastillayLeon07", 
"CastillayLeon14", "Catalonia07", "Catalonia14", "Centre07", 
"Centre14", "Champagne-Ardenne07", "Champagne-Ardenne14", "Corsica07", 
"Corsica14", "EastWales07", "EastWales14", "Emilia Romagna07", 
"Emilia Romagna14", "England07", "England14", "Franche-Comte07", 
"Franche-Comte14", "Friuli Venezia Giulia07", "Friuli Venezia Giulia14", 
"Hamburg07", "Hamburg14", "Haute Normandie07", "Haute Normandie14", 
"Hessen07", "Hessen14", "Italy07", "Italy14", "La Rioja07", "Lazio07", 
"Lazio14", "Liguria07", "Liguria14", "Limousine07", "Limousine14", 
"Loire07", "Loire14", "Lombardy07", "Lombardy14", "Lorraine07", 
"Lorraine14", "Lower Austria07", "Lower Austria14", "Lower Saxony07", 
"Lower Saxony14", "Madrid07", "Madrid14", "Malta07", "Malta14", 
"Marche07", "Marche14", "Mecklenburg-Vorpommern07", "Mecklenburg-Vorpommern14", 
"Midi-Pyrenees07", "Midi-Pyrenees14", "Molise07", "Molise14", 
"Murcia07", "Murcia14", "Calais07", "Calais14", "NorthernIreland07", 
"NorthernIreland14", "Picardie07", "Picardie14", "Piemonte07", 
"Piemonte14", "Poitou-Charentes07", "Poitou-Charentes14", "Puglia07", 
"Puglia14", "Rheinland07", "Rheinland14", "Rhone-Alpes07", "Rhone-Alpes14", 
"Saarland07", "Saarland14", "Salzburg07", "Salzburg14", "Sardinia07", 
"Sardinia14", "Saxony-Anhalt07", "Saxony-Anhalt14", "Saxony07", 
"Saxony14", "Schleswig-Holstein07", "Schleswig-Holstein14", "Scotland07", 
"Scotland14", "Sicily07", "Sicily14", "Steiermark07", "Steiermark14", 
"Thuringia07", "Thuringia14", "Trento07", "Trento14", "Tuscany07", 
"Tuscany14", "Tyrol07", "Tyrol14", "Umbria07", "Umbria14", "Upper Austria07", 
"Upper Austria14", "Valencia07", "Valencia14", "Valle d'Aosta07", 
"Valle d'Aosta14", "Veneto07", "Veneto14", "Vienna07", "Vienna14", 
"Vorarlberg014", "Vorarlberg07", "West Wales07", "West Wales14"
), Size = 165, Diag = FALSE, Upper = FALSE)

这是第二个data.frame

代码语言:javascript
复制
MyMeta<- structure(list(Group = structure(c(4L, 4L, 6L, 6L, 2L, 2L, 6L, 
6L, 6L, 6L, 1L, 2L, 2L, 3L, 3L, 6L, 6L, 4L, 4L, 2L, 2L, 3L, 3L, 
3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 1L, 4L, 4L, 
6L, 6L, 6L, 6L, 1L, 1L, 6L, 6L, 6L, 6L, 6L, 6L, 2L, 2L, 2L, 2L, 
2L, 2L, 7L, 7L, 4L, 4L, 7L, 7L, 2L, 2L, 4L, 4L, 3L, 3L, 2L, 2L, 
3L, 3L, 6L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L, 2L, 2L, 1L, 
1L, 3L, 3L, 6L, 6L, 5L, 5L, 4L, 4L, 3L, 3L, 2L, 2L, 4L, 4L, 6L, 
6L, 1L, 4L, 4L, 2L, 2L, 7L, 7L, 2L, 2L, 4L, 4L, 2L, 2L, 4L, 4L, 
3L, 3L, 2L, 2L, 3L, 3L, 1L, 1L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 
7L, 7L, 4L, 4L, 1L, 1L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 4L, 4L, 
1L, 1L, 6L, 6L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 7L, 7L), .Label = c("Austria", 
"France", "Germany", "Italy", "Malta", "Spain", "UK"), class = "factor"), 
Group0 = c(4, 4, 6, 6, 2, 2, 6, 6, 6, 6, 1, 2, 2, 3, 3, 6, 
6, 4, 4, 2, 2, 3, 3, 3, 3, 4, 4, 2, 2, 3, 3, 3, 3, 2, 2, 
1, 1, 4, 4, 6, 6, 6, 6, 1, 1, 6, 6, 6, 6, 6, 6, 2, 2, 2, 
2, 2, 2, 7, 7, 4, 4, 7, 7, 2, 2, 4, 4, 3, 3, 2, 2, 3, 3, 
6, 4, 4, 4, 4, 2, 2, 2, 2, 4, 4, 2, 2, 1, 1, 3, 3, 6, 6, 
5, 5, 4, 4, 3, 3, 2, 2, 4, 4, 6, 6, 1, 4, 4, 2, 2, 7, 7, 
2, 2, 4, 4, 2, 2, 4, 4, 3, 3, 2, 2, 3, 3, 1, 1, 4, 4, 3, 
3, 3, 3, 3, 3, 7, 7, 4, 4, 1, 1, 3, 3, 4, 4, 4, 4, 1, 1, 
4, 4, 1, 1, 6, 6, 4, 4, 4, 4, 1, 1, 1, 1, 7, 7)), .Names = c("Group", 
"Group0"), row.names = c("Abruzzo07", "Abruzzo14", "Andalucia07", 
"Andalucia14", "Aquitanie07", "Aquitanie14", "Aragon07", "Aragon14", 
"Asturias07", "Asturias14", "Austria07", "Austria14", "Auvergne07", 
"Auvergne14", "Baden-Wu07", "Baden-Wu14", "Baleares07", "Baleares14", 
"Basilicata07", "Basilicata14", "Basse Normandie07", "Basse Normandie14", 
"Bavaria07", "Bavaria14", "Berlin07", "Berlin14", "Bolzano07", 
"Bolzano14", "Bourgogne07", "Bourgogne14", "Bradenburgh07", "Bradenburgh14", 
"Bremen07", "Bremen14", "Bretagne07", "Bretagne14", "Burgenland07", 
"Burgenland14", "Campania07", "Campania14", "Canarias07", "Canarias14", 
"Cantabria07", "Cantabria14", "Carinthia07", "Carinthia14", "CastillaLaMancha07", 
"CastillaLaMancha14", "CastillayLeon07", "CastillayLeon14", "Catalonia07", 
"Catalonia14", "Centre07", "Centre14", "Champagne-Ardenne07", 
"Champagne-Ardenne14", "Corsica07", "Corsica14", "EastWales07", 
"EastWales14", "Emilia Romagna07", "Emilia Romagna14", "England07", 
"England14", "Franche-Comte07", "Franche-Comte14", "Friuli Venezia Giulia07", 
"Friuli Venezia Giulia14", "Hamburg07", "Hamburg14", "Haute Normandie07", 
"Haute Normandie14", "Hessen07", "Hessen14", "Italy07", "Italy14","La Rioja07", "Lazio07", "Lazio14", "Liguria07", "Liguria14", 
"Limousine07", "Limousine14", "Loire07", "Loire14", "Lombardy07", 
"Lombardy14", "Lorraine07", "Lorraine14", "Lower Austria07", 
"Lower Austria14", "Lower Saxony07", "Lower Saxony14", "Madrid07", 
"Madrid14", "Malta07", "Malta14", "Marche07", "Marche14", "Mecklenburg-Vorpommern07", 
"Mecklenburg-Vorpommern14", "Midi-Pyrenees07", "Midi-Pyrenees14", 
"Molise07", "Molise14", "Murcia07", "Murcia14", "Calais07", "Calais14", 
"NorthernIreland07", "NorthernIreland14", "Picardie07", "Picardie14", 
"Piemonte07", "Piemonte14", "Poitou-Charentes07", "Poitou-Charentes14", 
"Puglia07", "Puglia14", "Rheinland07", "Rheinland14", "Rhone-Alpes07", 
"Rhone-Alpes14", "Saarland07", "Saarland14", "Salzburg07", "Salzburg14", 
"Sardinia07", "Sardinia14", "Saxony-Anhalt07", "Saxony-Anhalt14", 
"Saxony07", "Saxony14", "Schleswig-Holstein07", "Schleswig-Holstein14", 
"Scotland07", "Scotland14", "Sicily07", "Sicily14", "Steiermark07", 
"Steiermark14", "Thuringia07", "Thuringia14", "Trento07", "Trento14", 
"Tuscany07", "Tuscany14", "Tyrol07", "Tyrol14", "Umbria07", "Umbria14", 
"Upper Austria07", "Upper Austria14", "Valencia07", "Valencia14", 
"Valle d'Aosta07", "Valle d'Aosta14", "Veneto07", "Veneto14", 
"Vienna07", "Vienna14", "Vorarlberg014", "Vorarlberg07", "West Wales07", 
"West Wales14"), class = "data.frame")
EN

回答 1

Stack Overflow用户

发布于 2016-08-04 10:24:21

在您的legend代码中,levels(MyMeta$Group)表示[1] "Austria" "France" "Germany" "Italy" "Malta" "Spain" "UK"。但是MyMeta$Group0的意思是[1] 4 4 6 6 2 2 6 6 6 6 1 2 2 3 ...pchcol没有被授权给legend。我提出了一些解决这个问题的方法。

代码语言:javascript
复制
## the most easy method (if your Group0 isn't alphabetical order, this method can't be used.)
legend(x="bottomleft", legend=levels(MyMeta$Group), col=1:7, pch=1:7)
                                 #  you can use seq.int(levels(MyMeta$Group)) instead of 1:7

## a versatile method (this method doesn't need the condition of Group0)
legend(x="top", legend=unique(MyMeta$Group), col=unique(MyMeta$Group0), pch=unique(MyMeta$Group0))

  # if you want alphabetical order,
  library(dplyr)
  MyMeta %>% distinct(Group, Group0) %>% arrange(Group) %>% 
    with(legend(x="bottomright", legend=Group, col=Group0, pch=Group0))

Gavin Simpson给出了一个更聪明的方法

有关更多信息,请参阅注释。

代码语言:javascript
复制
library(vegan)
pco2 <- capscale(vec2 ~ 1, add = TRUE)

pchv <- 1:7
colv <- 1:7

ordiplot(pco2, display = 'sites', type = 'n', cex=.75, main="Biotechnology Dissimilarity")
with(MyMeta, points(pco2, col=colv[Group], pch=pchv[Group]))
with(MyMeta, legend(x="bottomleft", legend=levels(Group), col=colv, pch=pchv))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38753832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档