2014-05-10 76 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