rm(list = ls())
###############################input data 
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- dir(dir_path,pattern = "*.csv",full.names = T)
dir_path_name
## [1] "C:\\Users\\liyix\\OneDrive\\Desktop\\data_1.csv"
## [2] "C:\\Users\\liyix\\OneDrive\\Desktop\\data_2.csv"
data_1 <- read.csv(grep("data_1.csv",dir_path_name,value = T),header = T,stringsAsFactors = F)
data_2 <- read.csv(grep("data_2.csv",dir_path_name,value = T),header = T,stringsAsFactors = F)
dim(data_1) #[1] 41  2
## [1] 34  2
dim(data_2) #[1] 16 15
## [1] 16 15
colnames(data_1)[1] <- "NAME"
data_2$NAME %in% data_1$NAME
## logical(0)
colnames(data_2)[1] <- "NAME"
data_1$NAME %in% data_2$NAME
##  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [13]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
sum(data_1$Freq)
## [1] 375
data_1_1 <- data_1[data_1$NAME %in% data_2$NAME,]
dim(data_1_1)
## [1] 16  2
row.names(data_1_1) <- 1:nrow(data_1_1)
data_1_1[17,] <- "Other"
data_1_1[17,2]<- 375-sum(as.numeric(data_1_1$Freq[-17]))
data_1_1$Freq <- as.numeric(data_1_1$Freq)
library(stringr)
# Calling str_to_title() function 
data_1_1$NAME <- str_to_title(data_1_1$NAME) 
###########################################################plot
library(tidyverse)
## -- Attaching packages ---------------------------------------------------------- tidyverse 1.3.0 --
## √ ggplot2 3.3.2     √ purrr   0.3.4
## √ tibble  3.0.3     √ dplyr   1.0.1
## √ tidyr   1.1.2     √ forcats 0.5.0
## √ readr   1.3.1
## -- Conflicts ------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
data_4 <- data_1_1
colnames(data_4) <- c("name","freq")
data_4$name <- LETTERS[1:length(data_4$name)]
data_5 <- data_4 %>%
  mutate(period = factor(name, levels = name)) %>%
  arrange(desc(name)) %>%
  mutate(cumenergy = cumsum(freq),
         centres = cumenergy - freq / 2)
library(yarrr)
## Loading required package: jpeg
## Loading required package: BayesFactor
## Loading required package: coda
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## ************
## Welcome to BayesFactor 0.9.12-4.2. If you have questions, please contact Richard Morey (richarddmorey@gmail.com).
## 
## Type BFManual() to open the manual.
## ************
## Loading required package: circlize
## ========================================
## circlize version 0.4.10
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: https://jokergoo.github.io/circlize_book/book/
## 
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization
##   in R. Bioinformatics 2014.
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(circlize))
## ========================================
## yarrr v0.1.5. Citation info at citation('yarrr'). Package guide at yarrr.guide()
## Email me at Nathaniel.D.Phillips.is@gmail.com
## 
## Attaching package: 'yarrr'
## The following object is masked from 'package:ggplot2':
## 
##     diamonds
library(ggrepel)
data_5
##    name freq period cumenergy centres
## 1     Q   53      Q        53    26.5
## 2     P   10      P        63    58.0
## 3     O   11      O        74    68.5
## 4     N   11      N        85    79.5
## 5     M   13      M        98    91.5
## 6     L   13      L       111   104.5
## 7     K   14      K       125   118.0
## 8     J   14      J       139   132.0
## 9     I   14      I       153   146.0
## 10    H   14      H       167   160.0
## 11    G   14      G       181   174.0
## 12    F   17      F       198   189.5
## 13    E   20      E       218   208.0
## 14    D   30      D       248   233.0
## 15    C   30      C       278   263.0
## 16    B   33      B       311   294.5
## 17    A   64      A       375   343.0
#View(data_5)
leg <- guide_legend(reverse = TRUE)
name_col <- unique(c(as.character(piratepal(palette = "info2")), as.character(piratepal(palette = "up"))))[17:1]
p1 <- ggplot(data_5, aes(x = 1, weight = freq, fill = name)) +
  geom_bar(width = 1, colour = "black") +
  geom_text(x = 1.3, aes(y = centres, label = freq), colour = "black",size = 4) +
  scale_fill_manual(values=name_col,guide = leg) +
  theme_minimal(base_family = "") +
  theme(legend.position = "",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.line =  element_blank()) +
  labs(fill = "", colour = "", 
       caption = "") +
  ggtitle("",
          subtitle = "") +
  coord_polar(theta = "y",start = 0.4) 

p1 + geom_text_repel(aes(x = 1.5, y = centres, label = name), 
                     nudge_x = .2, angle = 0,
                     segment.size = .4, 
                     show.legend = FALSE)

ggsave(filename = paste0(Sys.Date(),"-pie_1_protein.tif"), plot = last_plot(), 
       device = "tiff", path = dir_path,
       scale = 1, width = 15, height = 15, units = "cm",
       dpi = 300, limitsize = TRUE, compression = "lzw")
ggsave(filename = paste0(Sys.Date(),"-pie_1_protein.pdf"), plot = last_plot(), 
       device = "pdf", path = dir_path,
       scale = 1, width = 15, height = 15, units = "cm",
       dpi = 300, limitsize = TRUE)


p2 <- ggplot(data_5, aes(x = 2, weight = freq, fill = name)) +
  geom_bar(width = 1, colour = "gray50") +
  geom_text(x = 1.3 + 1, aes(y = centres, label = freq), colour = "black",size = 4) +
  scale_fill_manual(values=name_col,guide = leg) +
  theme_minimal(base_family = "") +
  theme(legend.position = "",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.line =  element_blank()) +
  labs(fill = "", colour = "", 
       caption = "") +
  ggtitle("",
          subtitle = "") + xlim(1, 2.5) +
  coord_polar(theta = "y",start = 0.4) 
p2

p2 + geom_text_repel(aes(x = 1.5 + 1, y = centres, label = name), 
                     nudge_x = .4, angle = 0,
                     segment.size = .4, 
                     show.legend = FALSE) +
  geom_text(x = 1,y = max(data_5$freq)/2, label = "LETTERS", colour = "black",size = 4) 

ggsave(filename = paste0(Sys.Date(),"-pie_2_protein.tif"), plot = last_plot(), 
       device = "tiff", path = dir_path,
       scale = 1, width = 15, height = 15, units = "cm",
       dpi = 300, limitsize = TRUE, compression = "lzw")
ggsave(filename = paste0(Sys.Date(),"-pie_2_protein.pdf"), plot = last_plot(), 
       device = "pdf", path = dir_path,
       scale = 1, width = 15, height = 15, units = "cm",
       dpi = 300, limitsize = TRUE)

?coord_polar
## starting httpd help server ...
##  done