Load the necessary package

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.2
## -- Attaching packages --------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.1     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 4.0.2
## Warning: package 'readr' was built under R version 4.0.2
## Warning: package 'forcats' was built under R version 4.0.2
## -- Conflicts ------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()



Load the dataset

college_data <- read_csv("http://672258.youcanlearnit.net/college.csv")
## Parsed with column specification:
## cols(
##   id = col_double(),
##   name = col_character(),
##   city = col_character(),
##   state = col_character(),
##   region = col_character(),
##   highest_degree = col_character(),
##   control = col_character(),
##   gender = col_character(),
##   admission_rate = col_double(),
##   sat_avg = col_double(),
##   undergrads = col_double(),
##   tuition = col_double(),
##   faculty_salary_avg = col_double(),
##   loan_default_rate = col_character(),
##   median_debt = col_double(),
##   lon = col_double(),
##   lat = col_double()
## )





Creating a simple Bar graph

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))





Bar graph with background color

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))+
  theme(plot.background = element_rect(fill = "green") )





Bar graph with panel color

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))+
  theme(panel.background = element_rect(fill = "yellow") )





Bar graph with no plot and panel background

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))+
  theme(plot.background = element_blank() )+
  theme(panel.background = element_blank() )





Bar graph with no plot and panel background but with “Black” gridline

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))+
  theme(plot.background = element_blank() )+
  theme(panel.background = element_blank() )+
  theme(panel.grid.major = element_line(color = "Black"))





if I only want the gridlines along Y-axis

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))+
  theme(plot.background = element_blank() )+
  theme(panel.background = element_blank() )+
  theme(panel.grid.major.y = element_line(color = "Black"))





Renaming the axis titles

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))+
  theme(plot.background = element_blank() )+
  theme(panel.background = element_blank() )+
  theme(panel.grid.major.y = element_line(color = "Black"))+
  ylab("Number of Schools")





Resetting the limit of axis





Renaming and Resetting the limit of axis with Scale

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))+
  theme(plot.background = element_blank() )+
  theme(panel.background = element_blank() )+
  theme(panel.grid.major.y = element_line(color = "Black"))+
  scale_x_discrete(name = "Region")+
  scale_y_continuous(name = "Number of Schools", limits = c(0,500))





Changing the fill colors

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))+
  theme(plot.background = element_blank() )+
  theme(panel.background = element_blank() )+
  theme(panel.grid.major.y = element_line(color = "Black"))+
  scale_fill_manual(values = c("Green","Yellow"))





Renaming the legend name

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))+
  theme(plot.background = element_blank() )+
  theme(panel.background = element_blank() )+
  theme(panel.grid.major.y = element_line(color = "Black"))+
  scale_fill_manual(values = c("Green","Yellow"), guide=guide_legend(title = "Ins. Type"))





Working with legend

title in “Guide_Legend” - change the title of the legend

nrow - Number of row of legengs

label.position - Position of the name of the legends

keywidth - Size of the legends

legend.position - Position of the legends

ggplot(data = college_data)+
  geom_bar(mapping = aes(x= region, fill = control))+
  theme(plot.background = element_blank() )+
  theme(panel.background = element_blank() )+
  theme(panel.grid.major.y = element_line(color = "Black"))+
  scale_fill_manual(values = c("Green","Yellow"),
                    guide=guide_legend(title = "Ins. Type",
                    nrow = 1,
                    label.position = "top",
                    keywidth = 3))+
  theme(legend.position = "top")





Creating a normal Scatterplot

ggplot(data = college_data)+
  geom_point(mapping = aes(x= tuition, y= sat_avg, color= control, size= undergrads),
             alpha= 1/2)





Annotating

ggplot(data = college_data)+
  geom_point(mapping = aes(x= tuition, y= sat_avg, color= control, size= undergrads),
             alpha= 1/2)+
  annotate("text",label = "Elite", x= 48000, y = 1500)





A horizontal line indicating the mean Sat_Avg

ggplot(data = college_data)+
  geom_point(mapping = aes(x= tuition, y= sat_avg, color= control, size= undergrads),
             alpha= 1/2)+
  annotate("text",label = "Elite", x= 48000, y = 1500)+
  geom_hline(yintercept = mean(college_data$sat_avg))+
  annotate("text",label = "Mean", x= 48000, y = mean(college_data$sat_avg)+22)





A vertical line indicating the max tuiton

ggplot(data = college_data)+
  geom_point(mapping = aes(x= tuition, y= sat_avg, color= control, size= undergrads),
             alpha= 1/2)+
  annotate("text",label = "Elite", x= 48000, y = 1500)+
  geom_vline(xintercept = max(college_data$tuition))+
  annotate("text",label = "Max", y= 1000, x = max(college_data$tuition)-2000)+
   theme(plot.background = element_blank() )+
  theme(panel.background = element_blank() )





Adding graph title

ggplot(data = college_data)+
  geom_point(mapping = aes(x= tuition, y= sat_avg, color= control, size= undergrads),
             alpha= 1/2)+
  annotate("text",label = "Elite", x= 48000, y = 1500)+
  geom_vline(xintercept = max(college_data$tuition))+
  annotate("text",label = "Max", y= 1000, x = max(college_data$tuition)-2000)+
   theme(plot.background = element_blank() )+
  theme(panel.background = element_blank() )+
  ggtitle("sat_avg vs tuition",
          subtitle= "U.S. Dept. of Education")





Playing with themes(bw)

ggplot(data = college_data)+
  geom_point(mapping = aes(x= tuition, y= sat_avg, color= control, size= undergrads),
             alpha= 1/2)+
  annotate("text",label = "Elite", x= 48000, y = 1500)+
  theme_bw()





Playing with themes(void)

ggplot(data = college_data)+
  geom_point(mapping = aes(x= tuition, y= sat_avg, color= control, size= undergrads),
             alpha= 1/2)+
  annotate("text",label = "Elite", x= 48000, y = 1500)+
  theme_void()