We will use the Dataset VADeaths. This is Death rates per 1000 in Virginia in 1940. They are cross classifed by the age groups(rows) and population group (Columns). Its a matrix with 5 rows and 4 columns
head(VADeaths)
## Rural Male Rural Female Urban Male Urban Female
## 50-54 11.7 8.7 15.4 8.4
## 55-59 18.1 11.7 24.3 13.6
## 60-64 26.9 20.3 37.0 19.3
## 65-69 41.0 30.9 54.6 35.1
## 70-74 66.0 54.3 71.1 50.0
# Subset
x <- VADeaths[1:3, "Rural Male"]
x
## 50-54 55-59 60-64
## 11.7 18.1 26.9
# Bar plot of one variable
barplot(x)
# Horizontal bar plot
barplot(x, horiz = TRUE)
barplot(x, names.arg = c("A", "B", "C"))
# Change border and fill color using one single color
barplot(x, col = "white", border = "steelblue")
# Use different colors for each group
barplot(x, col = "white",
border = c("#999999", "#E69F00", "#56B4E9"))
# Change fill color : single color
barplot(x, col = "steelblue")
# Change fill color: multiple colors
barplot(x, col = c("#999999", "#E69F00", "#56B4E9"))
# Change color (col = "gray") and remove frame
barplot(x, main = "Death Rates in Virginia",
xlab = "Age", ylab = "Rate")
barplot(VADeaths,
col = c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk"),
legend = rownames(VADeaths))
barplot(VADeaths,
col = c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk"),
legend = rownames(VADeaths), beside = TRUE)
# Define a set of colors
my_colors <- c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk")
# Bar plot
barplot(VADeaths, col = my_colors, beside = TRUE)
# Add Legend
legend("topleft", legend = rownames(VADeaths),
fill = my_colors, box.lty = 0, cex = 0.8)