### SOURCE: https://rpubs.com/Koundy/71792
library(grid)
library(ggplot2)
library(ggthemes)
library(gridExtra)
theme_Publication <- function(base_size=14) {
(theme_foundation(base_size=base_size)
+ theme(plot.title = element_text(face = "bold",
size = rel(1.2), hjust = 0.5),
text = element_text(),
panel.background = element_rect(colour = NA),
plot.background = element_rect(colour = NA),
panel.border = element_rect(colour = NA),
axis.title = element_text(face = "bold",size = rel(1)),
axis.title.y = element_text(angle=90,vjust =2),
axis.title.x = element_text(vjust = -0.2),
axis.text = element_text(),
axis.line = element_line(colour="black"),
axis.ticks = element_line(),
panel.grid.major = element_line(colour="#f0f0f0"),
panel.grid.minor = element_blank(),
legend.key = element_rect(colour = NA),
legend.position = "bottom",
legend.direction = "horizontal",
legend.key.size= unit(0.2, "cm"),
legend.margin = unit(0, "cm"),
legend.title = element_text(face="italic"),
plot.margin=unit(c(10,5,5,5),"mm"),
strip.background=element_rect(colour="#f0f0f0",fill="#f0f0f0"),
strip.text = element_text(face="bold")
))
}
scale_fill_Publication <- function(...){
library(scales)
discrete_scale("fill","Publication",manual_pal(values = c("#386cb0","#fdb462",
"#7fc97f","#ef3b2c",
"#662506","#a6cee3",
"#fb9a99","#984ea3","#ffff33")),
...)
}
scale_colour_Publication <- function(...){
library(scales)
discrete_scale("colour","Publication",manual_pal(values = c("#386cb0","#fdb462","#7fc97f",
"#ef3b2c","#662506","#a6cee3",
"#fb9a99","#984ea3","#ffff33")),
...)
}
Bar <- ggplot(mtcars, aes(factor(carb),fill=factor(carb))) +
geom_bar(alpha=0.7) + labs(x="Carb", y="Count")
Bar + scale_fill_Publication() +theme_Publication()

Scatter <- ggplot(mtcars, aes(mpg,disp,color=factor(carb))) +
geom_point(size=3) + labs(x="mpg", y="disp")
Scatter +scale_fill_Publication() +theme_Publication()

Bubble <- ggplot(mtcars, aes(mpg,disp,color=factor(carb),size=hp)) +
geom_point(alpha=0.7) + scale_size_continuous(range = c(3,10))
Bubble +scale_fill_Publication() +theme_Publication()

library(reshape2)
mtcars$Index <- 1:nrow(mtcars)
dat <- melt(mtcars,id.vars = c("Index"),measure.vars = c("drat","wt"))
Line <- ggplot(dat,aes(Index,value,colour=variable))+geom_line(size=1.3)
Line +scale_colour_Publication()+ theme_Publication()

library(tidyverse)
library(cowplot) # for theme_minimal_hgrid()
data_health <- practicalgg::happy %>%
select(age, health) %>%
na.omit() %>%
mutate(health = fct_rev(health)) # revert factor order
data_health
## # A tibble: 38,361 x 2
## age health
## <dbl> <fct>
## 1 23 good
## 2 70 fair
## 3 48 excellent
## 4 27 good
## 5 61 good
## 6 26 good
## 7 28 excellent
## 8 27 good
## 9 21 excellent
## 10 30 fair
## # … with 38,351 more rows
theme_set(theme_bw(base_size=16))
ggplot(data_health, aes(x = age, y = stat(count))) +
geom_density(
data = select(data_health, -health),
aes(fill = "all people surveyed "),
color = NA
) +
geom_density(aes(fill = "highlighted group"), color = NA) +
scale_x_continuous(
name = "age (years)",
limits = c(15, 98),
expand = c(0, 0)
) +
scale_y_continuous(
name = "count",
expand = c(0, 0)
) +
scale_fill_manual(
values = c("#b3b3b3a0", "#2b8cbed0"),
name = NULL,
guide = guide_legend(direction = "horizontal")
) +
facet_wrap(~health, nrow = 1) +
coord_cartesian(clip = "off") +
theme(
axis.line = element_blank(),
strip.text = element_text(size = 12, margin = margin(0, 0, 6, 0, "pt")),
legend.position = "bottom",
legend.justification = "right",
legend.margin = margin(6, 0, 1.5, 0, "pt"),
legend.spacing.x = grid::unit(3, "pt"),
legend.spacing.y = grid::unit(0, "pt"),
legend.box.spacing = grid::unit(0, "pt")
)

library(colorspace) # for darken()
library(ggrepel)
corrupt <- practicalgg::corruption %>%
filter(year == 2015) %>%
na.omit()
corrupt
## # A tibble: 163 x 6
## country region year cpi iso3c hdi
## <chr> <chr> <dbl> <dbl> <chr> <dbl>
## 1 Denmark Europe and Central Asia 2015 91 DNK 0.925
## 2 New Zealand Asia Pacific 2015 91 NZL 0.915
## 3 Finland Europe and Central Asia 2015 90 FIN 0.895
## 4 Sweden Europe and Central Asia 2015 89 SWE 0.913
## 5 Switzerland Europe and Central Asia 2015 86 CHE 0.939
## 6 Norway Europe and Central Asia 2015 88 NOR 0.949
## 7 Singapore Asia Pacific 2015 85 SGP 0.925
## 8 Netherlands Europe and Central Asia 2015 84 NLD 0.924
## 9 Canada Americas 2015 83 CAN 0.92
## 10 Germany Europe and Central Asia 2015 81 DEU 0.926
## # … with 153 more rows
region_cols <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#999999")
country_highlight <- c("Germany", "Norway", "United States",
"Greece", "Singapore", "Rwanda", "Russia",
"Venezuela", "Sudan", "Iraq", "Ghana", "Niger",
"Chad", "Kuwait", "Qatar", "Myanmar", "Nepal",
"Chile", "Argentina", "Japan", "China")
corrupt <- corrupt %>%
mutate(
label = ifelse(country %in% country_highlight, country, "")
)
ggplot(corrupt, aes(cpi, hdi)) +
geom_smooth(
aes(color = "y ~ log(x)", fill = "y ~ log(x)"),
method = 'lm', formula = y~log(x), se = FALSE, fullrange = TRUE
) +
geom_point(
aes(color = region, fill = region),
size = 2.5, alpha = 0.5, shape = 21
) +
geom_text_repel(
aes(label = label),
color = "black",
size = 12/.pt, # font size 9 pt
point.padding = 0.1,
box.padding = .6,
min.segment.length = 0,
seed = 7654
) +
scale_color_manual(
name = NULL,
values = darken(region_cols, 0.3)
) +
scale_fill_manual(
name = NULL,
values = region_cols
) +
scale_x_continuous(
name = "Corruption Perceptions Index, 2015 (100 = least corrupt)",
limits = c(10, 95),
breaks = c(20, 40, 60, 80, 100),
expand = c(0, 0)
) +
scale_y_continuous(
name = "Human Development Index, 2015\n(1.0 = most developed)",
limits = c(0.3, 1.05),
breaks = c(0.2, 0.4, 0.6, 0.8, 1.0),
expand = c(0, 0)
) +
guides(
color = guide_legend(
nrow = 1,
override.aes = list(
linetype = c(rep(0, 5), 1),
shape = c(rep(21, 5), NA)
)
)
) +
theme(
legend.position = "top",
legend.justification = "right",
legend.text = element_text(size = 10),
legend.box.spacing = unit(0, "pt")
)

P <- ggplot(data = mpg,aes(cty, hwy,color=class))+geom_point(size=3) +
facet_wrap(~ manufacturer,scales="free")
P +scale_colour_Publication()+ theme_Publication()
