在一张图中制作许多条形图
Making many bar plots in a one graph
我想制作一个类似 this
的条形图
我输入了:
> Japan <- 3137
> China <- 542
> Korea <- 499
> VietNam <- 423
> Indonesia <- 261
> Thailand <- 222
> SriLanka <- 60
> Taiwan <- 56
> Taiwan <- 60
> Bangladesh <- 51
> Nepal <- 43
> India <- 37
> Mongolia <- 26
> Myanmar <-21
> Philippines <- 16
> Singapore <- 15
> Cambodia <- 11
> Malaysia <- 10
> Pakistan <- 9
>"Lao People's Democratic Republic" <- 7
> "Brunei Darussalam" <- 3
> Afghanistan <- 10
> Iran <- 2
> Yemen <- 2
> United Arab Emirates <- 2
"United Arab Emirates" <- 2
> Lebanon <- 1
> Israel <- 1
> Kenya <- 9
> Botswana <- 7
> Ethiopia <- 3
> Nigeria <- 2
> Mozambique <- 2
> Uganda <- 2
> Morocco <- 1
> Ghana <- 1
> "South Africa" <- 1
> Zimbabwe <- 1
> America <- 58
> Canada <- 5
> "United Mexican States" <- 5
> Brazil <- 2
> Guyana <- 2
> "Antigua and Barbuda" <- 1
> Cuba <- 1
> Nicaragua <- 1
> Fiji <- 11
> Australia <- 6
> Tonga <- 6
> Samoa <- 2
> "Papua New Guinea" <- 1
> Uzbekistan <- 106
> Norway <- 10
> "Kyrgyz Republic" <- 9
> Germany <- 7
> Fracne <- 6
> Tajikistan <- 6
> Austria <- 5
> Italy <- 5
> UK <- 5
> Belgium <- 4
> Denmark <- 4
> Sweden <- 4
> Finland <- 4
> Estonia <- 3
> Lithuania <- 3
> Russia <- 3
> Georgia <- 1
> Netherlands <- 1
> Portuguese <- 1
> Iceland <- 1
> Kazakhstan <- 1
> Moldova <- 1
> Poland <- 1
> Spain <- 1
> "Swiss Confedeartion" <- 1
> Ukraine <- 1
> plot(Japan, Korea...., col=rainbow)
但不是不行。如何制作条形图?
首先,您需要将变量转换为 data.frame:
df <- read.table(header=F, text=" Japan 3137
China 542
Korea 499
VietNam 423
Indonesia 261
Thailand 222
SriLanka 60
Taiwan 56
Taiwan 60
Bangladesh 51
Nepal 43
India 37
Mongolia 26
Myanmar 21
Philippines 16
Singapore 15
Cambodia 11
Malaysia 10
Pakistan 9
Lao_People_Democratic_Republic 7
Brunei_Darussalam 3
Afghanistan 10
Iran 2
Yemen 2
United_Arab_Emirates 2
Lebanon 1
Israel 1
Kenya 9
Botswana 7
Ethiopia 3
Nigeria 2
Mozambique 2
Uganda 2
Morocco 1
Ghana 1
South_Africa 1
Zimbabwe 1
America 58
Canada 5
UnitedMexicanStates 5
Brazil 2
Guyana 2
AntiguaandBarbuda 1
Cuba 1
Nicaragua 1
Fiji 11
Australia 6
Tonga 6
Samoa 2
PapuaNewGuinea 1
Uzbekistan 106
Norway 10
KyrgyzRepublic 9
Germany 7
Fracne 6
Tajikistan 6
Austria 5
Italy 5
UK 5
Belgium 4
Denmark 4
Sweden 4
Finland 4
Estonia 3
Lithuania 3
Russia 3
Georgia 1
Netherlands 1
Portuguese 1
Iceland 1
Kazakhstan 1
Moldova 1
Poland 1
Spain 1
SwissConfedeartion 1
Ukraine 1")
然后订购(因为我看到你想要订购图表):
df <- df[order(df$V2,decreasing=T), ]
我会用 ggplot2
来绘制它,因为你有很多数据而且更好:
library(ggplot2)
ggplot(df, aes(x=V1,y=V2,fill=V1) ) + #remove fill argument if you don't want colours
geom_bar(stat='identity') +
scale_x_discrete(limits= df$V1 ) +
theme(legend.position="none") +
coord_flip()
我想制作一个类似 this
的条形图我输入了:
> Japan <- 3137
> China <- 542
> Korea <- 499
> VietNam <- 423
> Indonesia <- 261
> Thailand <- 222
> SriLanka <- 60
> Taiwan <- 56
> Taiwan <- 60
> Bangladesh <- 51
> Nepal <- 43
> India <- 37
> Mongolia <- 26
> Myanmar <-21
> Philippines <- 16
> Singapore <- 15
> Cambodia <- 11
> Malaysia <- 10
> Pakistan <- 9
>"Lao People's Democratic Republic" <- 7
> "Brunei Darussalam" <- 3
> Afghanistan <- 10
> Iran <- 2
> Yemen <- 2
> United Arab Emirates <- 2
"United Arab Emirates" <- 2
> Lebanon <- 1
> Israel <- 1
> Kenya <- 9
> Botswana <- 7
> Ethiopia <- 3
> Nigeria <- 2
> Mozambique <- 2
> Uganda <- 2
> Morocco <- 1
> Ghana <- 1
> "South Africa" <- 1
> Zimbabwe <- 1
> America <- 58
> Canada <- 5
> "United Mexican States" <- 5
> Brazil <- 2
> Guyana <- 2
> "Antigua and Barbuda" <- 1
> Cuba <- 1
> Nicaragua <- 1
> Fiji <- 11
> Australia <- 6
> Tonga <- 6
> Samoa <- 2
> "Papua New Guinea" <- 1
> Uzbekistan <- 106
> Norway <- 10
> "Kyrgyz Republic" <- 9
> Germany <- 7
> Fracne <- 6
> Tajikistan <- 6
> Austria <- 5
> Italy <- 5
> UK <- 5
> Belgium <- 4
> Denmark <- 4
> Sweden <- 4
> Finland <- 4
> Estonia <- 3
> Lithuania <- 3
> Russia <- 3
> Georgia <- 1
> Netherlands <- 1
> Portuguese <- 1
> Iceland <- 1
> Kazakhstan <- 1
> Moldova <- 1
> Poland <- 1
> Spain <- 1
> "Swiss Confedeartion" <- 1
> Ukraine <- 1
> plot(Japan, Korea...., col=rainbow)
但不是不行。如何制作条形图?
首先,您需要将变量转换为 data.frame:
df <- read.table(header=F, text=" Japan 3137
China 542
Korea 499
VietNam 423
Indonesia 261
Thailand 222
SriLanka 60
Taiwan 56
Taiwan 60
Bangladesh 51
Nepal 43
India 37
Mongolia 26
Myanmar 21
Philippines 16
Singapore 15
Cambodia 11
Malaysia 10
Pakistan 9
Lao_People_Democratic_Republic 7
Brunei_Darussalam 3
Afghanistan 10
Iran 2
Yemen 2
United_Arab_Emirates 2
Lebanon 1
Israel 1
Kenya 9
Botswana 7
Ethiopia 3
Nigeria 2
Mozambique 2
Uganda 2
Morocco 1
Ghana 1
South_Africa 1
Zimbabwe 1
America 58
Canada 5
UnitedMexicanStates 5
Brazil 2
Guyana 2
AntiguaandBarbuda 1
Cuba 1
Nicaragua 1
Fiji 11
Australia 6
Tonga 6
Samoa 2
PapuaNewGuinea 1
Uzbekistan 106
Norway 10
KyrgyzRepublic 9
Germany 7
Fracne 6
Tajikistan 6
Austria 5
Italy 5
UK 5
Belgium 4
Denmark 4
Sweden 4
Finland 4
Estonia 3
Lithuania 3
Russia 3
Georgia 1
Netherlands 1
Portuguese 1
Iceland 1
Kazakhstan 1
Moldova 1
Poland 1
Spain 1
SwissConfedeartion 1
Ukraine 1")
然后订购(因为我看到你想要订购图表):
df <- df[order(df$V2,decreasing=T), ]
我会用 ggplot2
来绘制它,因为你有很多数据而且更好:
library(ggplot2)
ggplot(df, aes(x=V1,y=V2,fill=V1) ) + #remove fill argument if you don't want colours
geom_bar(stat='identity') +
scale_x_discrete(limits= df$V1 ) +
theme(legend.position="none") +
coord_flip()