Aesthetic - Description x -> X-axis position y -> Y-axis position shape -> Shape of each point color -> Color of each point size -> Size of each point alpha -> Transparency of each point
ggplot(data = college) +
geom_point(mapping = aes(x=tuition, y=sat_avg))
ggplot(data = college) +
geom_point(mapping = aes(x=tuition, y=sat_avg, shape = control))
ggplot(data = college) +
geom_point(mapping = aes(x=tuition, y=sat_avg, color = control,
size = undergrads), alpha = 1/2)
ggplot(data = college) +
geom_line(mapping = aes(x=tuition, y=sat_avg, color = control)) +
geom_point(mapping = aes(x=tuition, y=sat_avg, color = control))
ggplot(data = college) +
geom_smooth(mapping = aes(x=tuition, y=sat_avg, color = control), se = FALSE) +
geom_point(mapping = aes(x=tuition, y=sat_avg, color = control), alpha = 1/5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control))
college %>%
group_by(region) %>%
summarise(average_tuition = mean(tuition)) %>%
ggplot() +
geom_col(mapping = aes(x = region, y = average_tuition))
ggplot(data = college) +
geom_histogram(mapping = aes(x=undergrads), bins = 10, origin = 0)
## Warning: `origin` is deprecated. Please use `boundary` instead.
ggplot(data = college) +
geom_boxplot(mapping = aes(x=control, y = tuition))
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control)) +
theme(plot.background = element_rect(fill="purple"))
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control)) +
theme(panel.background = element_rect(fill="purple"))
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control)) +
theme(panel.background = element_blank()) +
theme(plot.background = element_blank())
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control)) +
theme(panel.background = element_blank()) +
theme(plot.background = element_blank()) +
theme(panel.grid.major.y = element_line(color = "grey"))
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control)) +
theme(panel.background = element_blank()) +
theme(plot.background = element_blank()) +
theme(panel.grid.major.y = element_line(color = "grey")) +
ylab("Number of schools") +
xlab("Region") +
ylim(0,500)
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control)) +
theme(panel.background = element_blank()) +
theme(plot.background = element_blank()) +
theme(panel.grid.major.y = element_line(color = "grey")) +
ylab("Number of schools") +
xlab("Region") +
ylim(0,500)
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control)) +
theme(panel.background = element_blank()) +
theme(plot.background = element_blank()) +
theme(panel.grid.major.y = element_line(color = "grey")) +
scale_x_discrete(name = "Region") +
scale_y_continuous(name = "Number of schools", limits = c(0,500)) +
scale_fill_manual(values = c("orange", "blue"))
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control)) +
theme(panel.background = element_blank()) +
theme(plot.background = element_blank()) +
theme(panel.grid.major.y = element_line(color = "grey")) +
scale_x_discrete(name = "Region") +
scale_y_continuous(name = "Number of schools", limits = c(0,500)) +
scale_fill_manual(values = c("orange", "blue"),
guide = guide_legend(title = "Institution Type",
nrow = 1,
label.position = "bottom",
keywidth = 2)) +
theme(legend.position = "top")
ggplot(data = college) +
geom_point(mapping = aes(x=tuition, y=sat_avg, color = control,
size = undergrads), alpha = 1/2) +
annotate("text", label = "Elite Privates", x = 45000, y = 1450) +
geom_hline(yintercept = mean(college$sat_avg)) +
annotate("text", label = "Mean SAT", x = 47500, y = mean(college$sat_avg)-15) +
geom_vline(xintercept = mean(college$tuition)) +
annotate("text", label = "Mean Tuition", x = mean(college$tuition)-7500, y = 700) +
theme(panel.background = element_blank(), legend.key = element_blank()) +
scale_color_discrete(name = "Institution Type") +
scale_size_continuous(name = "Undergrads") +
scale_x_continuous(name = "Tuition") +
scale_y_continuous(name = "SAT Score")
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control)) +
theme(panel.background = element_blank()) +
theme(plot.background = element_blank()) +
theme(panel.grid.major.y = element_line(color = "grey")) +
scale_x_discrete(name = "Region") +
scale_y_continuous(name = "Number of schools", limits = c(0,500)) +
scale_fill_manual(values = c("orange", "blue")) +
ggtitle("More colleges are in the Southern US than any other Region",
subtitle = "Source: U.S. Dept. of Education")
library(ggthemes)
ggplot(data = college) +
geom_bar(mapping = aes(x=region, fill = control)) +
# theme_bw()
# theme_minimal()
#theme_void()
#theme_dark()
theme_solarized()
#theme_excel
#theme_wsj()
#theme_economist()
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
#Previuosly,
#library(ggplot2)
#library(ggmap)
#register_google(key = "")
#QuickMap - Plot the Map.
# qmap("New York, NY", zoom = 18)
#Retrieve the Map
nyc_map <- get_map("New York, NY", zoom = 18)
ggmap(nyc_map)
nyc <- geocode("New York, NY")
linda <- geocode("Lynda.com")
whitehouse <- geocode("White House")
ggmap(get_map(linda))
ggmap(get_map(nyc, maptype = "roadmap"))
#Others maptype: terrain, roadmap, terrain-labels, terrain-lines, sattelite, hybrid, toner, toner-lite, toner-blackground, watercolor.
usa <- geocode("United States")
nyc <- geocode("New York, NY")
ggmap(get_map(usa, zoom = 4)) +
geom_point(mapping = aes(x=lon, y=lat), color = "red", data=nyc)
library(tibble)
placesnames <- c("New York, NY", "Lynda.com", "White House", "Mt. Rushmore",
"The alamo")
locations <- geocode(placesnames)
places <- tibble(name = placesnames, lat = locations$lat, lon = locations$lon)
ggmap(get_map(usa, zoom = 4, maptype = "watercolor")) +
geom_point(mapping = aes(x=lon, y=lat), color = "red", data=places) +
geom_text(mapping = aes(x=lon, y=lat, label= name), color = "red", data = places, nudge_y = 1)
states <- map_data("state")
ggplot(states, aes(long, lat, group = group))+
geom_polygon() +
coord_map() +
theme(axis.title = element_blank(),
axis.text = element_blank(),
panel.background = element_blank())
library(dplyr)
library(stringr)
college <- read.csv('http://672258.youcanlearnit.net/college.csv')
college <- college %>%
mutate(state = as.factor(state), region = as.factor(region),
highest_degree = as.factor(highest_degree),
control = as.factor(control), gender = as.factor(gender),
loan_default_rate = as.numeric(loan_default_rate))
states <- map_data("state")
college_summary <- college %>%
group_by(state) %>%
summarize(schools=n())
college_summary <- college_summary %>%
mutate(region=as.character(setNames(str_to_lower(state.name),
state.abb)[as.character(state)]))
college_summary <- college_summary %>%
mutate(region=ifelse(as.character(state)== "DC", "disctrict of columbia", region))
map_data <- merge(states, college_summary, by= "region")
ggplot(map_data) +
geom_polygon(mapping = aes(x = long, y= lat, group=group, fill= schools)) +
coord_map() +
theme(plot.background = element_blank(),
panel.background = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank()) +
scale_fill_gradient(low= "beige", high="red")