Demo dataset to be used for a plot.
# load a built in dataset e.g. iris flower dataset
head(VADeaths, 10)
## 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
x <- VADeaths[1:5, "Rural Male"]
x
## 50-54 55-59 60-64 65-69 70-74
## 11.7 18.1 26.9 41.0 66.0
# Bar plot of one variable
barplot(x)
# Horizontal bar plot
barplot(x, horiz = TRUE)
# remove frame
barplot(x, names.arg = c("A", "B", "C", "D", "E"))
# Change border and fill color using one single color
barplot(x, col = "white", border = "steelblue")
# Change the color of border.
# 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 axis titles
# 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)