library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.5
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(ggplot2)
df <- data.frame(group=rep(x=LETTERS[1:3],times=c(5,3,7)))
df
## group
## 1 A
## 2 A
## 3 A
## 4 A
## 5 A
## 6 B
## 7 B
## 8 B
## 9 C
## 10 C
## 11 C
## 12 C
## 13 C
## 14 C
## 15 C
ggplot(data=df, mapping=aes(x=group))+geom_bar()

df %>% count(group)
## group n
## 1 A 5
## 2 B 3
## 3 C 7
df %>% count(group) %>% ggplot(aes(group, n)) + geom_bar(stat="identity")

df %>% count(group) %>% ggplot(aes(group, n)) + geom_col()

# Grouped barchart
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value)
ggplot(data, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="dodge", stat="identity")

# Stacked
ggplot(data, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="stack", stat="identity")

# Percent stacked barchart
ggplot(data, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="fill", stat="identity")

ggplot(data, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="fill", stat="identity")+scale_y_continuous(labels= scales::percent)

# Grouped barchart customization
library(viridis)
## Loading required package: viridisLite
library(hrbrthemes)
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
## Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
## if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
ggplot(data,aes(specie, value, fill=condition))+ geom_bar(position="stack",stat="identity")

ggplot(data,aes(specie, value, fill=condition))+ geom_bar(position="stack",stat="identity")+scale_fill_viridis(discrete = T)+ggtitle("Studing 4 species ....")+ theme_ipsum()+xlab("")

# Multiples
ggplot(data,aes(condition, value, fill=condition))+ geom_bar(position="dodge",stat="identity")+scale_fill_viridis(discrete = T, option="E")+ggtitle("Studing 4 species ....")+ facet_wrap(~specie)+ theme_ipsum()+theme(legend.position = "none")+ xlab("")

# ?scale_fill_viridis
# Circular barplot
set.seed(430)
mydata <- data.frame(id=seq(1,60),individual=paste0("Persion ",1:60),value=sample(10:100,60, replace = T))
view(mydata)
p <- ggplot(mydata, aes(as.factor(id),value))+geom_bar(stat="identity",fill=alpha("blue",.3)) + ylim(-100,120) + coord_polar(start=0)
p

p <- p + theme_minimal() + theme(axis.text = element_blank(),axis.title = element_blank(), panel.grid = element_blank(), plot.margin = unit(rep(-2,4),"cm"))
p

# Get the name and the y position of each label
label_data <- mydata
number_of_bar <- nrow(label_data)
angle <- 90 - 360*(label_data$id -0.5)/number_of_bar
label_data$angle <- angle
# 范围 0-90, -0- -270
# 计算label的对齐,如果是绘图的左侧,label -90度
label_data$hjust <- ifelse(angle < -90, 1, 0)
p + geom_text(data=label_data, aes(x=id, y=value+10, label=individual, hjust=0), color="black", fontface="bold", alpha=.6, size=2.5, angle=angle, inherit.aes = F)

# Add a gap in the circle
empty_bar <- 12
to_add <- matrix(NA, empty_bar,ncol(mydata))
colnames(to_add) <- colnames(mydata)
mydata_e <- rbind(mydata, to_add)
mydata_e$id <- seq(1, nrow(mydata_e))
mydata_e$Group <- factor(c(rep("A",10),rep("B",30),rep("C",14),rep("D",6),rep(c("A","B","C","D"),each=3)))
mydata_e <- mydata_e %>% arrange()
mydata_e$id <- seq(1,nrow(mydata_e))
label_data_e <- mydata_e
number_of_bar <- nrow(label_data_e)
angle <- 90-360*(label_data_e$id -.5)/number_of_bar
ggplot(mydata_e, aes(as.factor(id),value))+geom_bar(stat="identity",fill=alpha("blue",.3)) + ylim(-100,120) + coord_polar(start=0) + theme_minimal() + theme(axis.text = element_blank(),axis.title = element_blank(), panel.grid = element_blank(), plot.margin = unit(rep(-2,4),"cm"))+ geom_text(data=label_data_e, aes(x=id, y=value+10, label=individual, hjust=0), color="black", fontface="bold", alpha=.6, size=2.5, angle=angle, inherit.aes = F)
## Warning: Removed 12 rows containing missing values (position_stack).
## Warning: Removed 12 rows containing missing values (geom_text).

ggplot(mydata_e, aes(as.factor(id),value, fill=Group))+geom_bar(stat="identity") + ylim(-100,120) + coord_polar(start=0) + theme_minimal() + theme(axis.text = element_blank(),axis.title = element_blank(), panel.grid = element_blank(), plot.margin = unit(rep(-2,4),"cm"))+ geom_text(data=label_data_e, aes(x=id, y=value+10, label=individual, hjust=0), color="black", fontface="bold", alpha=.6, size=2.5, angle=angle, inherit.aes = F)
## Warning: Removed 12 rows containing missing values (position_stack).
## Removed 12 rows containing missing values (geom_text).

# Circular barchart customization
# library
library(tidyverse)
# Create dataset
data <- data.frame(
individual=paste( "Mister ", seq(1,60), sep=""),
group=c( rep('A', 10), rep('B', 30), rep('C', 14), rep('D', 6)) ,
value=sample( seq(10,100), 60, replace=T)
)
# Set a number of 'empty bar' to add at the end of each group
empty_bar <- 3
to_add <- data.frame( matrix(NA, empty_bar*nlevels(data$group), ncol(data)) )
colnames(to_add) <- colnames(data)
to_add$group <- rep(levels(data$group), each=empty_bar)
data <- rbind(data, to_add)
data <- data %>% arrange(group)
data$id <- seq(1, nrow(data))
# Get the name and the y position of each label
label_data <- data
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) /number_of_bar # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
label_data$hjust <- ifelse( angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)
# prepare a data frame for base lines
base_data <- data %>%
group_by(group) %>%
summarize(start=min(id), end=max(id) - empty_bar) %>%
rowwise() %>%
mutate(title=mean(c(start, end)))
# prepare a data frame for grid (scales)
grid_data <- base_data
grid_data$end <- grid_data$end[ c( nrow(grid_data), 1:nrow(grid_data)-1)] + 1
grid_data$start <- grid_data$start - 1
grid_data <- grid_data[-1,]
# Make the plot
p <- ggplot(data, aes(x=as.factor(id), y=value, fill=group)) + # Note that id is a factor. If x is numeric, there is some space between the first bar
geom_bar(aes(x=as.factor(id), y=value, fill=group), stat="identity", alpha=0.5) +
# Add a val=100/75/50/25 lines. I do it at the beginning to make sur barplots are OVER it.
geom_segment(data=grid_data, aes(x = end, y = 80, xend = start, yend = 80), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 60, xend = start, yend = 60), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 40, xend = start, yend = 40), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 20, xend = start, yend = 20), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
# Add text showing the value of each 100/75/50/25 lines
annotate("text", x = rep(max(data$id),4), y = c(20, 40, 60, 80), label = c("20", "40", "60", "80") , color="grey", size=3 , angle=0, fontface="bold", hjust=1) +
geom_bar(aes(x=as.factor(id), y=value, fill=group), stat="identity", alpha=0.5) +
ylim(-100,120) +
theme_minimal() +
theme(
legend.position = "none",
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1,4), "cm")
) +
coord_polar() +
geom_text(data=label_data, aes(x=id, y=value+10, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) +
# Add base line information
geom_segment(data=base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha=0.8, size=0.6 , inherit.aes = FALSE ) +
geom_text(data=base_data, aes(x = title, y = -18, label=group), hjust=c(1,1,0,0), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE)
p
## Warning: Removed 12 rows containing missing values (position_stack).
## Warning: Removed 12 rows containing missing values (position_stack).
## Warning: Removed 12 rows containing missing values (geom_text).
