ggplot(data = <DATA>) +
<GEOM_FUNCTION>(
mapping = aes(<MAPPINGS>),
stat = <STAT>,
position = <POSITION>
) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION>
library(ggplot2)
ggplot2::mpg # dataset
## # A tibble: 234 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compact
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compact
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
## 7 audi a4 3.1 2008 6 auto(av) f 18 27 p compact
## 8 audi a4 quattro 1.8 1999 4 manual(m5) 4 18 26 p compact
## 9 audi a4 quattro 1.8 1999 4 auto(l5) 4 16 25 p compact
## 10 audi a4 quattro 2 2008 4 manual(m6) 4 20 28 p compact
## # ... with 224 more rows
variables
- displ
: engine size [L]
- hwy
: fuel efficiency on highway [miles per gallon (mpg)]
- drv
: the type of drive train (f
= front-wheel drive, r
= rear wheel drive, 4
= 4wd)
- cyl
: number of cylinders
geom_point()
normal <-
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
red <-
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = hwy)) +
scale_color_gradient(low = "pink", high = "red")
## Plot
library(gridExtra)
grid.arrange(normal, red, ncol = 2)
color <-
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
size <-
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, size = class))
alpha <-
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
shape <-
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class))
## Plot
library(gridExtra)
grid.arrange(color, size, alpha, shape, ncol = 2)
facet by class
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)
facet by other variables
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)
geom_smooth
point <-
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))
smooth <-
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy), se = FALSE)
group <-
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, group = drv))
color <-
ggplot(data = mpg) +
geom_smooth(
mapping = aes(x = displ, y = hwy, color = drv),
show.legend = FALSE
)
## Plot
library(gridExtra)
grid.arrange(point, smooth, group, color, ncol = 2)
inefficient <-
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))
efficient <-
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()
color <-
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth()
color2 <-
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class), show.legend = FALSE) +
geom_smooth()
## Plot
library(gridExtra)
grid.arrange(inefficient, efficient, color, color2, ncol = 2)
geom_bar()
count <-
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
proportion <-
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = stat(prop), group = 1))
## Plot
library(gridExtra)
grid.arrange(count, proportion, ncol = 2)
color <-
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, colour = cut))
fill <-
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut))
color2 <-
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, color = clarity))
fill2 <-
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity))
alpha <-
ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) +
geom_bar(alpha = 1/5, position = "identity")
nonFill <-
ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) +
geom_bar(fill = NA, position = "identity")
## Plot
library(gridExtra)
grid.arrange(color, fill, color2, fill2, alpha, nonFill, ncol = 2)
posiFill<-
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")
posiDodge <-
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
## Plot
library(gridExtra)
grid.arrange(posiFill, posiDodge, ncol = 2)
geom_boxplot()
x <-
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot()
y <-
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
## Plot
library(gridExtra)
grid.arrange(x, y, ncol = 2)
geom_polygon()
nz <- map_data("nz") # Polygon of Newzy Land
wrong <-
ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black")
correct <-
ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black") +
coord_quickmap()
## Plot
library(gridExtra)
grid.arrange(wrong, correct, ncol = 2)
coord_xx()
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar <- bar
flip <- bar + coord_flip()
polar <- bar + coord_polar()
## Plot
library(gridExtra)
grid.arrange(bar, flip, polar, ncol = 3)
library(stargazer)
stargazer(
<m1>, <m2>,
type = "text",
column.labels = c("<t1>", "<t2>"),
dep.var.caption="Dependent variable",
dep.var.labels.include = FALSE,
model.numbers = FALSE,
model.names = FALSE
)