2014-05-10 77 views
-1

圖表,我有以下數據:如何通過標籤中的R

Country    Quality.of.life.index 
1 Switzerland  206.23 
2 United States 195.55 
3 Germany   192.69 
4 Sweden   180.92 
... 

我正在尋找一種方法,使以「國家」左側標籤和對應於它們的Quality.of棒圖.life.index值在右側。任何方式在R中做到這一點?

這裏是我的意思的例子:http://www.pewglobal.org/2014/04/15/global-morality/table/gambling/

+1

你看着'barplot'? – r2evans

+0

感謝所有的答案。抱歉不熟悉barplot。對於那些與我相同的問題 - 下面的所有答案都是很好的選擇,請看看每個答案。 – SheerSt

回答

2

在闡述@ r2evans的評論,這裏的barplot

par(mar = c(4, 7, 4, 2))    ## This sets the left margin wider 
barplot(mydf$Quality.of.life.index, ## The vector of values you want to plot 
     names.arg=mydf$Country,  ## The labels 
     horiz=TRUE, las = 1)   ## Horizontal bars and labels 

enter image description here

這是假設以下起始數據:

mydf <- data.frame(
    Country = c("Switzerland", "United States", "Germany", "Sweden"), 
    Quality.of.life.index = c(206.23, 195.55, 192.69, 180.92)) 
mydf 
#   Country Quality.of.life.index 
# 1 Switzerland    206.23 
# 2 United States    195.55 
# 3  Germany    192.69 
# 4  Sweden    180.92 
2

你可以使用lattice::barchart。我不得不操縱你的數據,因爲它不會讀取。但是,如果您只撥打data$Country列,data$QofLife列等,它應該沒問題。

> library(lattice) 
> Country <- c('Switzerland', 'USA', 'Germany', 'Sweden') 
> QofLife <- c(206.23, 195.55, 192.69, 180.92) 
> barchart(Country ~ QofLife, xlab = 'Quality of Life') 

enter image description here

2

點陣圖也是這類數據的一個不錯的選擇(使用@Richard準備的數據)

#using base graphics 
dotchart(structure(QofLife, names=Country), xlab = 'Quality of Life',main="graphics") 

#using lattice 
require(lattice) 
dotplot(Country ~ QofLife, xlab = 'Quality of Life', cex=1.2, main="Lattice") 

dotplots