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$rating ~ dat$complaints) with(dat, plot(rating ~ complaints)) t.test(dat$rating ~ dat$sex, var.equal = TRUE) with(dat, t.test(rating ~ sex, var.equal = TRUE)) plot(rating ~ complaints, data = dat, xlab = "Handling of Employee Complaints", ylab = "Overall Rating", main = "Employee Survey") abline(lm(rating ~ complaints, data = dat)) #### Points plot(rating ~ complaints, type = "n", data = dat, xlab = "Handling of Employee Complaints", ylab = "Overall Rating", main = "Employee Survey") with(dat, points(complaints, rating, col = "red")) ### Points separated by sex plot(rating ~ complaints, type = "n", data = dat, xlab = "Handling of Employee Complaints", ylab = "Overall Rating", main = "Employee Survey") with(dat[dat$sex == "M", ], points(complaints, rating, col = "blue")) with(dat[dat$sex == "F", ], points(complaints, rating, col = "red")) legend("topleft", c("Male", "Female"), pch = 1, col = c("blue", "red")) ## Text plot(rating ~ complaints, type = "n", data = dat, xlab = "Handling of Employee Complaints", ylab = "Overall Rating", main = "Employee Survey") with(dat, text(complaints, rating, label = sex)) ## Axes plot(rating ~ complaints, type = "n", data = dat, xlab = "Handling of Employee Complaints", ylab = "Overall Rating", main = "Employee Survey", axes = FALSE, 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, label = sex)) # Make an arrow pointing to the centriod mx <- mean(dat$complaints) my <- mean(dat$rating) points(mx, my, pch = 16, cex = 2, col = "red") abline(v = mx, lty = 2, col = "pink") abline(h = my, lty = 2, col = "pink") arrows(mx - 10, my + 10, mx - 1, my + 1, col = "blue", lwd = 2) text(mx - 10, my + 10, pos = 2, col = "blue", label = "The Mean Coordinate") ########## Boxplot boxplot(dat$rating, ylab = "Overall Rating") boxplot(rating ~ sex, data = dat, horizontal = TRUE, xlab = "Overall Rating", boxwex = 0.5, col = c("blue", "red")) ######## Barplot barplot(50, ylim = c(0, 60), col = "pink", xlab = "Overall Rating") aggregate(rating ~ sex, data = dat, FUN = mean) aggregate(rating ~ sex, data = dat, FUN = sd) tapply(dat$rating, dat$sex, mean) sexm <- with(dat, tapply(rating, sex, mean)) sexsd <- with(dat, tapply(rating, sex, sd)) barout <- barplot(sexm, ylim = c(0, 80), col = c("pink", "skyblue")) arrows(barout, sexm + sexsd, barout, sexm - sexsd, angle = 90, code = 3) abline(h = 0) ##### Barplot with two factors 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), legend.text = TRUE, col = c("pink", "darkorange"), args.legend = list(x = 2, y = 110, legend = c("Male", "Female"))) arrows(barout, sem + sesd, barout, sem - sesd, angle = 90, code = 3) abline(h = 0) #### Line plot visualizing interaction 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 = 1, col = "red") points(cbind(1:2, sem[,2]), pch = 2, col = "blue") lines(cbind(1:2, sem[,2]), lty = 2, col = "blue") legend("bottomleft", c("Non-Whites", "Whites"), lty = 1:2, pch = 1:2, col = c("red", "blue")) ### Histogram x <- rnorm(1000, 0, 1) hist(x) hist(x, breaks = 20) hist(x, breaks = 20, prob = TRUE) lines(density(x), lty = 2, col = "red") ### Saving a figure tiff(file = "x.tiff", width = 1600, height = 1600, pointsize = 60) hist(x, breaks = 20, prob = TRUE) lines(density(x), lty = 2, col = "red") text(-3, 0.3, "Histogram") dev.off() pdf(file = "x.pdf", width = 8, height = 8) hist(x, breaks = 20, prob = TRUE) lines(density(x), lty = 2, col = "red") text(-3, 0.3, "Histogram") dev.off() ### New windows windows() plot(rnorm(100), rnorm(100)) quartz() X11() ### Multiple graphs par(mfrow = c(2, 2)) hist(rnorm(100)) hist(rt(100, df = 3)) hist(rchisq(100, df = 3)) hist(rf(100, df1 = 2, df2 = 7)) dev.off() par(mfcol = c(2, 2)) hist(rnorm(100)) hist(rt(100, df = 3)) hist(rchisq(100, df = 3)) hist(rf(100, df1 = 2, df2 = 7)) dev.off() par(mfrow = c(1, 3)) hist(rnorm(100)) hist(rt(100, df = 3)) hist(rchisq(100, df = 3)) #### Special characters in figures 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) hist(x, breaks = 20) 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)