[英]How to graph by label in R
I have the following data: 我有以下数据:
Country Quality.of.life.index
1 Switzerland 206.23
2 United States 195.55
3 Germany 192.69
4 Sweden 180.92
...
I am looking for a way to make a graph with 'Country' labels on the left and a bar corresponding to their Quality.of.life.index values on the right. 我正在寻找一种制作图的方法,该图的左侧带有“国家/地区”标签,而右侧则是与它们的Quality.of.life.index值相对应的条形图。 Any way to do this in R?
有什么办法可以在R中做到这一点吗?
Here's an example of what I mean: http://www.pewglobal.org/2014/04/15/global-morality/table/gambling/ 这是我的意思的示例: http : //www.pewglobal.org/2014/04/15/global-morality/table/gambling/
You could use lattice::barchart
. 您可以使用
lattice::barchart
。 I had to manipulate your data a bit because it wouldn't read. 我不得不操纵您的数据,因为它不会读取。 But if you call only the
data$Country
column, data$QofLife
column, etc. it should be fine. 但是,如果仅调用
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')
Elaborating on @r2evans's comment, here's barplot
: 详细介绍@ 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
This is assuming the following starting data: 假设以下起始数据:
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
A dotplot is also a nice choice for this kind of data (using the data @Richard prepared) 对于这种数据,圆点图也是不错的选择(使用@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.