# install.packages(c("tidyverse", "GGally")) # to.install <- c("magittr", "purrr", # "ggplot2", "dplyr", "broom", "GGally") # install.packages(to.install) require(ggplot2) head(mtcars) require(ggplot2) gp <- ggplot(aes(x = wt, y = mpg), data = mtcars) gp + geom_point() gp + geom_point(size = 4) gp + geom_point(aes(size = wt)) gp <- ggplot(aes(x = wt, y = mpg), data = mtcars) + geom_point() gp + scale_y_continuous(limits=c(1, 40)) + scale_x_continuous(limits=c(0, 6)) gp + scale_y_continuous(limits=c(1, 35), breaks=seq(1, 35, 5)) + scale_x_continuous(limits=c(1.5, 5.5), breaks=seq(1.5, 5.5, 1)) gp <- ggplot(aes(x=wt, y=mpg, color=factor(vs)), data=mtcars) gp + geom_point() mtcars$vs <- as.factor(mtcars$vs) gp <-ggplot(aes(x=wt, y=mpg, color=vs), data=mtcars) gp + geom_point() data(Burt, package = 'carData') Burt$class <- as.factor(Burt$class) gp <- ggplot(aes(x = IQbio, y = IQfoster), data = Burt) gp + geom_point(aes(color = class, shape = class)) gp <- ggplot(aes(x = IQbio, y = IQfoster), data = Burt) gp + geom_point(aes(color = class, shape = class)) + geom_smooth(method = "lm", se = FALSE) gp + geom_point(aes(color = class, shape = class)) + geom_smooth(aes(color = class), method = "lm", se = FALSE) gp <- ggplot(aes(x=wt, y=mpg), data=mtcars) gp + geom_point() + geom_density2d() require(dplyr) require(broom) corr <- mtcars %$% cor.test(mpg, wt) %>% tidy %>% mutate_if(is.numeric, round, 4) corr text = paste0('r = ', corr$estimate, ', ', ifelse(corr$p.value <= 0, 'p < 0.05', paste('p = ', corr$p.value)) ) text gp <- ggplot(aes(x = wt, y = mpg), data = mtcars) gp + geom_point() + geom_smooth(method = "lm", se = FALSE) + annotate('text', x = 4.5, y = 35, label=text) require(tidyr) require(purrr) data(Burt, package = 'carData') corr <- Burt %>% group_by(class) %>% nest() %>% mutate(Cor = map(data, ~ cor.test(.$IQbio, .$IQfoster)), p = map_dbl(Cor, 'p.value'), est = map_dbl(Cor, 'estimate') ) %>% mutate_if(is.numeric, round, 4) %>% select(class, p, est, Cor) text <- corr %>% mutate( text = paste0('r = ', est, ', ', ifelse(p <= 0.01, 'p < 0.05', paste('p = ', p)))) Burt$class <- as.factor(Burt$class) gp <- ggplot(aes(x = IQbio, y = IQfoster), data = Burt) corrp <- gp + geom_point(aes(color = class, shape=class)) + geom_smooth(aes(color = class), method = "lm", se = FALSE) + geom_text(aes(x = 120, y = 137, color="high", label=subset(text, class == "high")$text)) + geom_text(aes(x = 118, y = 109, color="medium", label=subset(text, class == "medium")$text)) + geom_text(aes(x = 124, y = 103, color="low", label=subset(text, class == "low")$text)) corrp data(Salaries, package = "carData") Salaries$rank <- as.factor(Salaries$rank) gp <- ggplot(aes(x = salary, y = yrs.since.phd), data = Salaries) + geom_point(aes(color = rank, shape = rank)) + geom_smooth(method = "lm") + scale_y_continuous(limits = c(0, 60)) + scale_x_continuous(limits = c(50000, 240000), breaks = seq(50000, 240000, by = 10000)) gp + theme(axis.text.x = element_text(angle = 90, hjust = 1)) corrp + theme_bw() + scale_colour_grey() + theme(axis.line = element_line(colour = "black") ,plot.background = element_blank() ,panel.grid.major = element_blank() ,panel.grid.minor = element_blank() ,strip.background = element_blank() ,panel.border = element_blank() ,legend.title=element_blank() ,legend.key = element_blank()) require(GGally) cols = c('mpg', 'wt', 'hp', 'qsec') ggpairs(mtcars, columns = cols) data(Salaries, package = "carData") gp <- ggplot(aes(x=yrs.since.phd, y=salary), data=Salaries) + geom_point() + geom_smooth(method = "lm", se = FALSE, colour="gray") + theme_bw() + theme(axis.line = element_line(colour = "black") ,plot.background = element_blank() ,panel.grid.major = element_blank() ,panel.grid.minor = element_blank() ,strip.background = element_blank() ,panel.border = element_blank() ,legend.title=element_blank() ,legend.key = element_blank()) + xlab('Years since Ph.D.') + ylab('Salary') ggsave("salaries_by_year_scatterplot.pdf", device = "pdf", width = 12, height = 8, units = "cm", dpi = 300) ggsave("salaries_by_year_scatterplot.tiff", device = "tiff", width = 12, height = 8, units = "cm", dpi = 300)