ggplot2
Pie chart
Pie chart with other approach this by defining another variable (call pos) in df that calculates the position of text labels.
# https://stackoverflow.com/questions/24803460/r-ggplot2-add-labels-on-facet-pie-chart
library(dplyr)
library(ggplot2)
<- df %>% group_by(year) %>% mutate(pos = cumsum(quantity)- quantity/2)
df
ggplot(data=df, aes(x=factor(1), y=quantity, fill=factor(prod))) +
geom_bar(stat="identity") +
geom_text(aes(x= factor(1), y=pos, label = quantity), size=10) + # note y = pos
facet_grid(facets = .~year, labeller = label_value) +
coord_polar(theta = "y")
Donut chart
<- data.frame(
count.data class = c("1st", "2nd", "3rd", "Crew"),
n = c(325, 285, 706, 885),
prop = c(14.8, 12.9, 32.1, 40.2)
)
count.data
# Add label position
<- count.data %>%
count.data arrange(desc(class)) %>%
mutate(lab.ypos = cumsum(prop) - 0.5*prop)
count.data
<- c("#0073C2FF", "#EFC000FF", "#868686FF", "#CD534CFF")
mycols
ggplot(count.data, aes(x = "", y = prop, fill = class)) +
geom_bar(width = 1, stat = "identity", color = "white") +
coord_polar("y", start = 0)+
geom_text(aes(y = lab.ypos, label = prop), color = "white")+
scale_fill_manual(values = mycols) +
theme_void()
# Donut chart
ggplot(count.data, aes(x = 2, y = prop, fill = class)) +
geom_bar(stat = "identity", color = "white") +
coord_polar(theta = "y", start = 0)+
geom_text(aes(y = lab.ypos, label = prop), color = "white")+
scale_fill_manual(values = mycols) +
theme_void()+
xlim(0.5, 2.5)
gganimate .gif
https://github.com/thomasp85/gganimate
hrbrtheme
- format date
scale_y_continuous(label=scales::comma)+
scale_x_date(date_breaks = "2 month",
date_labels = "%m-%Y")+
theme(legend.title = element_blank(),
legend.position = "bottom",
axis.text.x = element_text(angle = 30))
- histogram
ggplot(aes(x = cnt))+
geom_histogram(bins = 18)+ # bins = 24
labs(title = "Tong thoi gian",
subtitle = "don vi",
x = "So luong",
y = "So quan sat")+
scale_y_comma()+
theme_ipsum_rc()+
facet_wrap(~ bi_card_group, nrow = 2)
- theme
theme(axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 16),
axis.text.y = element_text(size = 14), axis.title.y = element_text(size = 16),
plot.title = element_text(size = 20, face = "bold", color = "darkgreen"))
vietnamese text
plot(1:4,rep(1,4),pch=c("\u0111","\u01B0","\u01A1","\u0103"),cex=4)
Area plot
<- wdata %>%
mu group_by(sex) %>%
summarise(grp.mean = mean(weight))
# Area plot
<- ggplot(economics, aes(x=date)) +
a geom_area(aes(y=psavert), fill = "#999999",
color = "#999999", alpha=0.5) +
geom_area(aes(y=uempmed), fill = "#E69F00",
color = "#E69F00", alpha=0.5) +
theme_minimal()
+ geom_density(aes(color = sex), alpha=0.4)+
a geom_vline(data = mu, aes(xintercept = grp.mean, color=sex),
linetype="dashed")
Density
+ geom_density(aes(color = sex), alpha=0.4)+
a geom_vline(data = mu, aes(xintercept = grp.mean, color=sex),
linetype="dashed")
Histogram
# Basic plot
+ geom_histogram()
a # Change the number of bins
+ geom_histogram(bins = 50)
a # Position adjustment: "identity" (overlaid)
+ geom_histogram(aes(color = sex), fill = "white", alpha = 0.6,
a position="identity")
Histogram with density plot
# Color by groups
+ geom_histogram(aes(y=..density.., color = sex, fill = sex),
a alpha=0.5, position="identity")+
geom_density(aes(color = sex), size = 1)
Box plot with mean points
+ geom_boxplot() +
e stat_summary(fun.y = mean, geom = "point",
shape = 18, size = 4, color = "blue")
Change the default order of items
+ geom_boxplot() +
e scale_x_discrete(limits=c("2", "0.5", "1"))
Add mean points +/- SD
# Use geom = "pointrange" or geom = "crossbar"
+ geom_violin(trim = FALSE) +
e stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="pointrange", color = "red")
# Combine with box plot to add median and quartiles
+ geom_violin(trim = FALSE) +
e geom_boxplot(width = 0.2)
geom_line
# Change line types, point shapes and colors
+ geom_line(aes(linetype=supp, color = supp))+
p geom_point(aes(shape=supp, color = supp))
geom_bar with labels
require(plyr)
# Sort by dose and supp
<- arrange(df2, dose, supp)
df_sorted
# Calculate the cumulative sum of len for each dose
<- ddply(df_sorted, "dose", transform,
df_cumsum label_ypos=cumsum(len))
# Create the bar plot
ggplot(data=df_cumsum, aes(x = dose, y = len, fill = supp)) +
geom_bar(stat = "identity")+
geom_text(aes(y = label_ypos, label = len), vjust=1.6,
color = "white", size = 3.5)
Pie charts
# Basic pie charts
<- data.frame(
df group = c("Male", "Female", "Child"),
value = c(25, 25, 50))
<- ggplot(df, aes(x="", y = value, fill=group)) +
p geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0)
<- theme_minimal()+
blank_theme theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x=element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
plot.title=element_text(size=14, face="bold")
)
require(scales)
+ scale_fill_brewer("Blues") + blank_theme +
p geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]),
label = percent(value/100)), size=5)