######Initiate packages #If you don't have ggplot2 installed then use: install.packages(c("ggplot2", "plyr")) #Initiate ggplot2 library(ggplot2) library(reshape) library(plyr) #--------Quick Tutorial---------- facebookData <- read.delim("FacebookNarcissism.dat", header = TRUE) graph <- ggplot(facebookData, aes(NPQC_R_Total, Rating)) graph + geom_point() + opts(title = "geom_point()") saveInImageDirectory("04 Tutorial Point.png") graph + geom_point(shape = 17) + opts(title = "geom_point(shape = 17)") saveInImageDirectory("04 Tutorial Triangle.png") graph + geom_point(size = 6) + opts(title = "geom_point(size = 6)") saveInImageDirectory("04 Tutorial Size.png") graph + geom_point(aes(colour = Rating_Type)) + opts(title = "geom_point(aes(colour = Rating_Type))") saveInImageDirectory("04 Tutorial Colour Point.png") graph + geom_point(aes(colour = Rating_Type), position = "jitter") + opts(title = "geom_point(aes(colour = Rating_Type), position = jitter)") saveInImageDirectory("04 Tutorial Jitter.png") graph + geom_point(aes(shape = Rating_Type), position = "jitter") + opts(title = "geom_point(aes(shape = Rating_Type), position = jitter)") saveInImageDirectory("04 Tutorial Jitter2.png") #--------Scatterplots---------- examData <- read.delim("Exam Anxiety.dat", header = TRUE) names(examData) #Simple scatter scatter <- ggplot(examData, aes(Anxiety, Exam)) scatter + geom_point() + labs(x = "Exam Anxiety", y = "Exam Performance %") saveInImageDirectory("04 Exam Scatter.png") #Simple scatter with smooth scatter <- ggplot(examData, aes(Anxiety, Exam)) scatter + geom_point() + geom_smooth() + labs(x = "Exam Anxiety", y = "Exam Performance %") saveInImageDirectory("04 Exam Smooth.png") #Simple scatter with regression line scatter <- ggplot(examData, aes(Anxiety, Exam)) scatter + geom_point() + geom_smooth(method = "lm", colour = "Red", se = F) + labs(x = "Exam Anxiety", y = "Exam Performance %") saveInImageDirectory("04 Exam Scatter w. Line.png") #Simple scatter with regression line + CI scatter <- ggplot(examData, aes(Anxiety, Exam)) scatter + geom_point() + geom_smooth(method = "lm", colour = "Red")+ labs(x = "Exam Anxiety", y = "Exam Performance %") saveInImageDirectory("04 Exam Scatter w Line & CI.png") #Simple scatter with regression line + coloured CI scatter <- ggplot(examData, aes(Anxiety, Exam)) scatter + geom_point() + geom_smooth(method = "lm", colour = "Red", alpha = 0.1, fill = "Red") + labs(x = "Exam Anxiety", y = "Exam Performance %") saveInImageDirectory("04 Exam Scatter w. Line & red CI.png") #Grouped scatter with regression line + CI scatter <- ggplot(examData, aes(Anxiety, Exam, colour = Gender)) scatter + geom_point() + geom_smooth(method = "lm", aes(fill = Gender), alpha = 0.1) + labs(x = "Exam Anxiety", y = "Exam Performance %", colour = "Gender") saveInImageDirectory("04 Exam Grouped Scatter w Line & CI.png")