# Plotting # November 5, 2015 # Sunthud Pornprasertmanit dat <- attitude dat$sex <- as.factor(rep(c("M", "F"), each = 15)) dat$ethnicity <- as.factor(rep(c("W", "N"), 15)) plot(rating ~ complaints, data = dat) plot(dat$complaints, dat$rating) plot(rating ~ complaints, data = dat, xlab = "Handling of Employee Complaints", ylab = "Overall Rating", main = "Employee Survey") abline(lm(rating ~ complaints, data = dat)) plot(rating ~ complaints, data = dat, type = "n", xlab = "Handling of Employee Complaints", ylab = "Overall Rating", main = "Employee Survey") with(dat[dat$sex == "M",], points(complaints, rating, pch = 2, cex = 1.5, col = "blue")) with(dat[dat$sex == "F",], points(complaints, rating, pch = 2, cex = 1.5, col = "red")) legend("topleft", c("Male", "Female"), pch = 2, col = c("blue", "red"), cex = 1.5) plot(rating ~ complaints, data = dat, type = "n", xlab = "Handling of Employee Complaints", ylab = "Overall Rating", main = "Employee Survey") with(dat, text(complaints, rating, labels = sex)) #Start with 0:100, manipulate numbers on the axes plot(rating ~ complaints, data = dat, type = "n", axes = FALSE, xlab = "Handling of Employee Complaints", ylab = "Overall Rating", main = "Employee Survey", xlim = c(0, 100), ylim = c(0, 100)) axis(1, at = c(25, 50, 75, 100), labels = c("Very Low", "Low", "High", "Very High")) axis(2, at = c(25, 50, 75, 100), labels = c("Very Low", "Low", "High", "Very High")) box() with(dat, text(complaints, rating, labels = dat$sex)) mx <- mean(dat$complaints) my <- mean(dat$rating) points(mx, my, pch = 16, cex = 2, col = "red") arrows(0.93 * mx, 1.2 * my, 0.99 * mx, 1.01 * my, code = 2, length = 0.1, col = "red") text(0.93 * mx, 1.2 * my, pos = 2, col = "red", label = "The Mean Coordinate") boxplot(dat$rating, ylab = "Overall Rating") boxplot(rating ~ sex, data = dat, xlab = "Overall Rating", horizontal = TRUE, boxwex = 0.5, col = c("green", "yellow")) barplot(50, ylim = c(0, 60), col = "pink", xlab = "Overall Rating") sexm <- tapply(dat$rating, dat$sex, mean) sexsd <- tapply(dat$rating, dat$sex, sd) barout <- barplot(sexm, ylim = c(0, 80), col = c("pink", "skyblue")) arrows(barout, sexm + sexsd, barout, sexm - sexsd, angle = 90, code = 3, length = 0.5) abline(h = 0) sem <- tapply(dat$rating, list(dat$sex, dat$ethnicity), mean) sesd <- tapply(dat$rating, list(dat$sex, dat$ethnicity), sd) barout <- barplot(sem, beside = TRUE, ylim = c(0, 100), col = c("pink", "skyblue"), legend.text = TRUE, args.legend = list(x = 3.5, y = 100, legend = c("Male", "Female"))) arrows(barout, sem + sesd, barout, sem - sesd, angle = 90, code = 3, length = 0.2) abline(h = 0) plot(c(0, 0), type = "n", axes = FALSE, xlab = "Sex", ylab = "Overall Rating", main = "Employee Survey", xlim = c(0.5, 2.5), ylim = c(0, 80)) axis(1, at = c(1, 2), labels = c("Female", "Male")) axis(2) box() points(cbind(1:2, sem[,1]), pch = 1, col = "red") lines(cbind(1:2, sem[,1]), lty = 2, col = "red") points(cbind(1:2, sem[,2]), pch = 2, col = "blue") lines(cbind(1:2, sem[,2]), lty = 1, col = "blue") legend("bottomleft", c("Non-Whites", "Whites"), pch = 1:2, lty = c(2, 1), col = c("red", "blue")) set.seed(123321) x <- rt(1000, df = 8) hist(x) hist(x, breaks = 20) hist(x, breaks = 20, prob = TRUE) lines(density(x), lty = 2, col = "red") # pdf, png, win.metafile tiff(file = "x.tiff", width = 1600, height = 1600, pointsize = 60) hist(x, breaks = 20) mx <- round(mean(x), 2) dev.off() pdf("x.pdf", width = 8, height = 8, pointsize = 20) hist(x, breaks = 20) mx <- round(mean(x), 2) text(2, 150, label = bquote(italic(M) ~ "=" ~ .(round(mean(x), 2))), pos = 4) text(2, 140, label = bquote(italic(SD) == .(round(sd(x), 2))), pos = 4) dev.off() # For windows, the better way is to take a snapshot from PDF windows() # to view several graphs; X11 for Linux; quartz for Macs par(mfrow = c(2, 2)) # Fill row first par(mfcol = c(2, 2)) # Fill column first par(mfrow = c(2, 2)) # Fill row first hist(rnorm(100, 0, 1)) hist(rchisq(100, 2)) plot(matrix(c(0, 0, 1, 1), 2), type = "l") barplot(c(20, 40)) dev.off() set.seed(123321) x <- rnorm(100, 0, 1) hist(x, main = "mu = round(mean(x), 2)") hist(x, main = paste("mu =", round(mean(x), 2))) hist(x, main = bquote(mu == round(mean(x), 2))) hist(x, main = bquote(mu == .(round(mean(x), 2)))) hist(x, main = bquote(italic(M) == .(round(mean(x), 2)))) hist(x, main = bquote("Histogram of a sample from" ~ italic(N)(0, 1))) text(-3, 35, label = bquote(italic(M) == .(round(mean(x), 2))), pos = 4) text(-3, 30, label = bquote(italic(SD) == .(round(sd(x), 2))), pos = 4)