在一个图表中制作许多条形图

时间:2015-01-08 23:41:04

标签: r plot ggplot2 bar-chart

我想制作像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)

但它不起作用。我怎样才能制作一个条形图?

1 个答案:

答案 0 :(得分:0)

首先,您需要在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()

enter image description here