# Load libraries --------------------------------------------- #install.packages("tidyverse") library(tidyverse) # Load data -------------------------------------------------- # data<- read_csv('https://raw.githubusercontent.com/idc9/stor390/master/data/movies.csv') # # saveRDS(data,file = "ggplotDatav2.RDS") data <- readRDS("ggplotDatav2.RDS") # inspect data str(data) # The basic plot------------------------------------------------ basic.plot <- ggplot(data=data) + geom_point(aes(x=imdb_rating, y=critics_score)) windows() plot(basic.plot) # Labels # add labs() basic.labels.plot <- ggplot(data=data) + geom_point(aes(x=imdb_rating, y=critics_score)) + labs(x='IMDB', # x- axis label y='Rotten Tomatoes (critics)', # y- axis label title='IMDB scores are predictive of Rotten Tomatoes') # main title windows() plot(basic.labels.plot) ## logarithmic axis basic.log.plot <- ggplot(data=data) + geom_point(aes(x=log10(imdb_rating), y=log10(critics_score)))# the log process on the fly windows() plot(basic.log.plot) #Annotation-------------------------------------- # data frame for annotation bad_movie <- data %>% filter(critics_score== min(critics_score)) # add text label bad.movie.plot <- ggplot(data=data) + geom_point(aes(x=imdb_rating, y=critics_score)) + geom_text(data=bad_movie, aes(x=imdb_rating, y=critics_score, label=title), color='red')#add text windows() plot(bad.movie.plot) # nudge the text bad.movie.plot <- ggplot(data=data) + geom_point(aes(x=imdb_rating, y=critics_score)) + geom_text(data=bad_movie, aes(x=imdb_rating, y=critics_score, label=title),color='red', nudge_x= .3, nudge_y= 2) #nudge is moving the labels a small distance from what tey are labelling windows() plot(bad.movie.plot) # axis ------------------------------------------ ##limits axis.lims.plot <- ggplot(data=data) + geom_point(aes(x=imdb_rating, y=critics_score)) + lims(x=c(-5, 15), y=c(-10, 100)) windows() plot(axis.lims.plot) ## axis scale axis.scale.plot <- ggplot(data=data) + geom_point(aes(x=imdb_rating, y=critics_score)) + scale_y_continuous(breaks=seq(from=0, to=100, by=10)) windows() plot(axis.scale.plot) ### x axis scaling axis.scale.plot <- ggplot(data=data) + geom_point(aes(x=imdb_rating, y=critics_score)) + scale_y_continuous(breaks=seq(from=0, to=100, by=10))+ scale_x_continuous(breaks=seq(from=0, to=10, by=.5)) windows() plot(axis.scale.plot) # theme--------- ## Theme controls the plot background and the legend theme.plot <- ggplot(data=data) + geom_point(aes(x=imdb_rating, y=critics_score)) + theme(panel.background = element_blank()) windows() plot(theme.plot) ## green panel green.theme.plot <- ggplot(data=data) + geom_point(aes(x=imdb_rating, y=critics_score)) + theme(panel.background = element_rect(fill = "green")) windows() plot(green.theme.plot) ## green panel- red background green.red.theme.plot <- green.theme.plot+ theme(plot.background = element_rect(fill = "red")) windows() plot(green.red.theme.plot) ## change axis line axis.change.plot <- basic.plot + theme(axis.line = element_line(size = 3, colour = "grey80")) windows() plot(axis.change.plot) ## change axis labels color axis.color.change.plot <- basic.plot + theme(axis.text = element_text(colour = "blue")) windows() plot(axis.color.change.plot) ## axis ticks modifications axis.ticks.change.plot <- basic.plot + theme(axis.ticks = element_line(size = 2)) windows() plot(axis.ticks.change.plot) ## axis ticks lenght axis.ticks.lenght.change.plot <- basic.plot + theme(axis.ticks.length = unit(.25, "cm")) windows() plot(axis.ticks.lenght.change.plot) ## axis title modification axis.ytext.plot <- basic.plot + theme(axis.title.y = element_text(size = rel(1.5), angle = 90)) windows() plot(axis.ytext.plot) # Legend---------------------------------------------- ## add legend p2 <- ggplot(data, aes(imdb_rating, critics_score)) + geom_point(aes(colour = factor(dvd_rel_year), shape = factor(genre))) + scale_shape_manual(values=0:11) + labs( x = "imdb rating", y = "Critics Score", colour = "DVD year", shape = "Genre" ) windows() plot(p2) ## remove legend p3 <- p2 + theme(legend.position = "none") windows() plot(p3) ## legend bottom p4 <- p2 + theme(legend.position = "bottom") # alternative "top" windows() plot(p4) ## legend position p5 <- p2 + theme( legend.position = c(.95, .95), legend.justification = c("right", "top"), legend.box.just = "right", legend.margin = margin(1, 1, 1, 1)#the legend margins size ) windows() plot(p5) # Their values should be between 0 and 1. c(0,0) corresponds to the “bottom left” and c(1,1) # corresponds to the “top right” position. # facets-------------------- facet1.plot <- basic.plot+ facet_wrap(~ genre) windows() plot(facet1.plot) ## change the facet labels facet2.plot <- facet1.plot+ theme(strip.background = element_rect(colour = "black", fill = "white")) windows() plot(facet2.plot) ## change the facet labels letters to bold facet3.plot <- facet1.plot+ theme(strip.background = element_rect(colour = "black", fill = "white"), strip.text.x = element_text(colour = "black", face = "bold")) windows() plot(facet3.plot) # save plots -------------------- dev.off() ggsave(plot=facet1.plot,"facet1.pdf",device="pdf",width = 24,height = 20,units="cm",dpi = 300) ggsave(plot=facet1.plot,"facet1.jpg",width = 24,height = 20,units="cm",dpi = 1000) ggsave(plot=facet1.plot,"facet2.jpg",scale = 2,dpi = 1000) # links---------------------------------------------------- # https://idc9.github.io/stor390/notes/custom_viz/custom_viz.html # http://ggplot2.tidyverse.org/reference/index.html