1. 程式人生 > >R多個箱線圖並標註離群點

R多個箱線圖並標註離群點

在學探索性資料分析的時候,老師讓我們做多個箱線圖,並且找出其離群點並標註出來,我是R語言的小菜鳥,鼓搗了一天寫出來了一個小程式,希望需要這個的人有所幫助。微笑

Sweden=c(7.87,4.22,2.49,0.94,0.89,0.87,0.81,0.78,0.71,0.69)
Netherlans=c(8.68,7.31,6.02,2.64,1.75,1.72,1.51,1.42,1.31,1.29)
Canada=c(11.91,6.72,3.84,2.81,2.73,2.68,2.65,2.49,1.71,1.69)
France=c(28.11,7.83,5.35,3.30,2.94,2.54,2.46,2.33,2.03,1.99)
Mexico=c(31.18,10.12,8.06,3.79,3.46,2.91,2.71,2.17,2.06,1.86)
Argentina=c(29.66,7.61,6.35,4.10,3.8,2.75,2.7,2.69,2.51,2.44)
Spain=c(25.99,16.96,5.01,4.74,3.57,3.34,3.12,2.64,2.14,1.69)
England=c(79.86,11.02,7.22,6.38,5.09,4.88,4.30,3.30,3.10,2.99)
Italy=c(23.59,15.80,11.82,11.14,7.84,5.90,4.54,4.44,3.61,3.36)
WestGermany=c(21.92,18.56,11.42,8.27,7.28,7.02,6.94,6.53,5.84,5.66)
Brazil=c(49.81,38.57,9.68,9.52,8.08,8.03,6.99,5.02,4.95,2.78)
SovietUnion=c(63.34,36.36,13.32,11.37,10.90,10.84,10.70,10.27,9.5,9.17)
Japan=c(110.21,32.14,18.88,16.39,13.37,11.95,10.70,7.89,7.71,7.04)
UnitedStates=c(77.81,35.50,24.79,20.02,16.70,9.39,9.38,8.76,7.63,7.50)
India=c(45.37,30.03,22.98,20.62,17.25,16.11,11.49,9.47,9.07,7.21)
China=c(69,40.10,36.92,32.20,24.11,21.46,21.21,16.50,15.00,11.13)

name=c('Sweden','Netherlans','Canada','France','Mexico','Argentina',

       'Spain','England','Italy','WestGermany','Brazil','SovietUnion',
       'Japan','UnitedStates','India','China')
LQ=boxplot(Sweden,Netherlans,Canada,France,Mexico,Argentina,
           Spain,England,Italy,WestGermany,Brazil,SovietUnion,
           Japan,UnitedStates,India,China,
           names=name,pch="*",cex=1.5,horizontal = TRUE)
lq=LQ$out


foo<-read.table(file= "D:\\Data\\Rdatas\\shujufenxi1.txt",head=T,stringsAsFactors = FALSE)
foo
countryname1=rep('a',length(lq))
row=rep(0,length(lq))
l=rep(0,length(lq))
for (i in 1:length(lq))
{ for (j in 1:160)
  if(lq[i]==foo[j,1])
  {print (foo[j,3])
    countryname1[i]=foo[j,3]
    row[i]=j
    l[i]=ceiling(j/10.0)}
}
names(LQ$out)=countryname1
countryname1
l

text(x=LQ$out,y=l,labels=countryname1,cex=1,col="blue")

#以下是txt格式的文件

population nation country
7.87 SwedenStockholm
4.22 SwedenGoteborg
2.49 SwedenMalomo
0.94 SwedenNorrkoping
0.89 SwedenVasteras
0.87 SwedenUppsala
0.81 SwedenOrebro
0.78 SwedenHalsingborg
0.71 SwedenLinkoping
0.69 SwedenBoras
8.68 NetherlansAmsterdarm
7.31 NetherlansRotterdam
6.02 NetherlansTheHangue
2.64 NetherlansUtrecht
1.75 NetherlansEindhoven
1.72 NetherlansHaarlem
1.51 NetherlansGroningen
1.42 NetherlansTilburg
1.31 NetherlansEnschede
1.29 NetherlansArnhem
11.91 CanadaMontreal
6.72 CanadaToronto
3.84 CanadaVaocover
2.81 CanadaEdmonton
2.73 CanadaHamilton
2.68 CanadaOttawa
2.65 CanadaWinnipeg
2.49 CanadaCalgary
1.71 CanadaQuebec
1.69 CanadaLondon
28.11 FranceParis
7.83 FranceMarscilles
5.35 FranceLyon
3.30 FranceToulouse
2.94 FranceNice
2.54 FranceBordcaux
2.46 FranceNantes
2.33 FranceStrasbourg
2.03 FranceSt.Etienne
1.99 FranceLille
31.18 MexicoMexicoCity
10.12 MexicoGuadalajara
8.06 MexicoMonterrey
3.79 MexicoJuarez
3.46 MexicoPuebla
2.91 MexicoMexicali
2.71 MexicoLeon
2.17 MexicoTorreon
2.06 MexicoChibuahua
1.86 MexicoSanLuisPotosi
29.66 ArgentinaBuenosAires
7.61 ArgentinaRosario
6.35 ArgentinaCordoba
4.10 ArgentinaLaPlata
3.80 ArgentinaAvellaneda
2.75 ArgentinaSantaFe
2.70 ArgentinaMardelPalata
2.69 ArgentinaGeneraSanMartin
2.51 ArgentinaTucuman
2.44 ArgentinaLanus
25.99 SpainMadmd
16.96 SpainBarcelona
5.01 SpainValencia
4.74 SpainSeville
3.57 SpainZaragoza
3.34 SpainBilboa
3.12 SpainMalaga
2.64 SpainMurcia
2.14 SpainCordoba
1.69 SpainPalma
79.86 EnglandLondon
11.02 EnglandBirmingham
7.22 EnglandLivecpool
6.38 EnglandManchester
5.09 EnglandLeeds
4.88 EnglandSheffied
4.30 EnglandBristol
3.30 EnglandCoventry
3.10 EnglandNottingham
2.99 EnglandKingston-upon-Hull
23.59 ItalyRome
15.80 ItalyMilan
11.82 ItalyNaples
11.14 ItalyTurin
7.84 ItalyGenos
5.90 ItalyPalermo
4.54 ItalyFlorence
4.44 ItalyBologna
3.61 ItalyCatamia
3.36 ItalyVenice
21.92 WestGermanyWestBerlin
18.56 WestGermanyHamburg
11.42 WestGermanyMunich
8.27 WestGermanyCologne
7.28 WestGermanyEssen
7.02 WestGermanyDusseldorf
6.94 WestGermanyFrankfurt
6.53 WestGermanyDortmund
5.84 WestGermanyBremen
5.66 WestGermanyHannover
49.81 BrazilSaoPaulo
38.57 BrazilRiodeJaneiro
9.68 BrazilRecife
9.52 BrazilBeioHorizonte
8.08 BrazilSalvador
8.03 BrazilPortoAlegre
6.99 BrazilFortaleza
5.02 BrazilCuritiba
4.95 BrazilBelem
2.78 BrazilNiterol
63.34 SovietUnionMoscow
36.36 SovietUnionLeningrad
13.32 SovietUnionKiev
11.37 SovietUnionBaku
10.90 SovietUnionTashkent
10.84 SovietUnionGorky
10.70 SovietUnionKharkov
10.27 SovietUnionNovosibirsk
9.50 SovietUnionKuibyshev
9.17 SovietUnionSverdlovsk
110.21 JapanTokyo
32.14 JapanOsaka
18.88 JapanNagoya
16.39 JapanYokohama
13.37 JapanKyoto
11.95 JapanKobe
10.70 JapanKitaKyushu
7.89 JapanKawasaki
7.71 JapanFukuoka
7.04 JapanSaporo
77.81 UnitedStatesNewYorkCity
35.50 UnitedStatesChicago
24.79 UnitedStatesLosAngeles
20.02 UnitedStatesPhiladelphia
16.70 UnitedStatesDetroit
9.39 UnitedStatesBeltimore
9.38 UnitedStatesHouston
8.76 UnitedStatesCleveland
7.63 UnitedStatesWashongtonDC
7.50 UnitedStatesSt.Louis
45.37 IndiaBombay
30.03 IndiaCalcutta
22.98 IndiaDelhi
20.62 IndiaHyderabad
17.25 IndiaMadra
16.11 IndiaHowrah
11.49 IndiaAhmedabad
9.47 IndiaKangalore
9.07 IndiaBengalore
7.21 IndiaPoona
69.00 ChinaShanghai
40.10 ChinaBeijing
36.92 ChinaHongKpng
32.20 ChinaTianjin
24.11 ChinaShenzhen
21.46 ChinaWuhan
21.21 ChinaChongqing
16.50 ChinaCanton
15.00 ChinaXian
11.13 ChinaNanjing