如何在R中按标签绘图

时间:2014-05-10 04:17:58

标签: r graph plot statistics

我有以下数据:

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

我正在寻找一种方法来制作一个图表,左边是'Country'标签,右边是一个与他们的Quality.of.life.index值相对应的栏。在R中有什么方法吗?

以下是我的意思:http://www.pewglobal.org/2014/04/15/global-morality/table/gambling/

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 :(得分: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