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. Any way to do this in R?
Here's an example of what I mean: http://www.pewglobal.org/2014/04/15/global-morality/table/gambling/
You could use 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.
> 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
:
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)
#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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.