[英]How to graph by label in 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/
您可以使用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')
详细介绍@ 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
假设以下起始数据:
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
对于这种数据,圆点图也是不错的选择(使用@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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.