Graphics for Communication

# Creating a scatterplot using mpg dataset

library(ggplot2)

ggplot(mpg,aes(x=displ,y=hwy)) + geom_point(aes(color=class)) + geom_smooth(se=F) + labs(title="Fuel Efficiency decreases with Engine Size")
## `geom_smooth()` using method = 'loess'

ggplot(mpg,aes(x=displ,y=hwy)) + geom_point(aes(color=class)) + geom_smooth(se=F) + labs(title="Fuel Efficiency decreases with Engine Size",subtitle="Two seaters' engine are economically fueled",caption="Massey University")+ theme(plot.title = element_text(hjust = 0.5),plot.subtitle = element_text(hjust = 0.5))
## `geom_smooth()` using method = 'loess'

ggplot(mpg,aes(x=displ,y=hwy)) + geom_point(aes(color=class)) + geom_smooth(se=F) + labs(title="Fuel Efficiency decreases with Engine Size",x="Engine Displacement",y="Fuel Efficiency",color="Car Types")+ theme(plot.title = element_text(hjust = 0.5))
## `geom_smooth()` using method = 'loess'

*Using mathematical quotations notations

library(tibble)

df<-tibble(x=runif(20),y=runif(20))

head(df)
ggplot(df,aes(x=x,y=y)) + geom_point(color="blue") + labs(x = quote(sum(x[i] ^ 2, i == 1, n)),y = quote(alpha + beta + frac(delta, theta)),title="A random scatterplot") + theme(plot.title = element_text(hjust=0.5))

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.4.3
## -- Attaching packages ------------------------------------------------------------- tidyverse 1.2.1 --
## v tidyr   0.7.2     v dplyr   0.7.4
## v readr   1.1.1     v stringr 1.2.0
## v purrr   0.2.4     v forcats 0.2.0
## Warning: package 'tidyr' was built under R version 3.4.3
## Warning: package 'purrr' was built under R version 3.4.3
## Warning: package 'dplyr' was built under R version 3.4.3
## -- Conflicts ---------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
best_in_class <- mpg %>%  group_by(class) %>% filter(row_number(desc(hwy)) == 1)

head(best_in_class)
tail(best_in_class)
ggplot(mpg,aes(displ,y=hwy))+geom_point(aes(color=class)) + geom_smooth(se=F) + geom_text(aes(label=model),data=best_in_class,nudge_y = 2,alpha=0.5) # Using nudge makes thig more beautiful
## `geom_smooth()` using method = 'loess'

ggplot(mpg,aes(displ,y=hwy))+geom_point(aes(color=class)) + geom_smooth(se=F) + geom_label(aes(label=model),data=best_in_class,nudge_y = 2,alpha=0.5)
## `geom_smooth()` using method = 'loess'

library(ggrepel)
## Warning: package 'ggrepel' was built under R version 3.4.2
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  geom_point(size = 3, shape = 1, data = best_in_class) +
  ggrepel::geom_label_repel(aes(label = model), data = best_in_class)

label <- tibble(
  displ = Inf,
  hwy = Inf,
  label = "Increasing engine size is \nrelated to decreasing fuel economy."
) # can be used displ=max(displ), and hwy=max(hwy)

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_text(aes(label = label), data = label, vjust = "top", hjust = "right")

base <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class))

base + theme(legend.position = "left")

base + theme(legend.position = "top")

base + theme(legend.position = "bottom")

base + theme(legend.position = "right") # the default

ggplot(diamonds, aes(carat, price)) +
  geom_bin2d() + 
  scale_x_log10() + 
  scale_y_log10()

ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(color = drv)) +
  scale_colour_brewer(palette = "Set1")

presidential %>%
  mutate(id = 33 + row_number()) %>%
  ggplot(aes(start, id, colour = party)) +
    geom_point() +
    geom_segment(aes(xend = end, yend = id)) +
    scale_colour_manual(values = c(Republican = "red", Democratic = "blue"))

df <- tibble(
  x = rnorm(10000),
  y = rnorm(10000)
)

library(hexbin)
## Warning: package 'hexbin' was built under R version 3.4.2
ggplot(df, aes(x, y)) +
  geom_hex() +
  coord_fixed()

#> Loading required package: methods

ggplot(df, aes(x, y)) +
  geom_hex() +
  viridis::scale_fill_viridis() +
  coord_fixed()