rm(list = ls())
################# ##############input data
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- list.files(pattern = ".*xlsx",dir_path,full.names = T, recursive = F)
dir_path_name
## [1] "C:\\Users\\liyix\\OneDrive\\Desktop\\AB.xlsx"
library(openxlsx)
getSheetNames(grep("AB.xlsx",dir_path_name,value = T))
## [1] "Sheet1"
data_1 <- read.xlsx(grep("AB.xlsx",dir_path_name,value = T),sheet = 1)
###############################input data
dim(data_1)
## [1] 5 4
data_1$X4 <- NULL
data_2 <- data_1[1:4, ]
data_2$X1 <- c("A","B","C","D")
colnames(data_2) <- c("aa","bb","cc")
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.0.6 v dplyr 1.0.4
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## Warning: package 'tidyr' was built under R version 4.0.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
data_3 <- gather(data_2, key = cate,value = value,-1)
data_3
## aa cate value
## 1 A bb 0.06377806
## 2 B bb 0.09356806
## 3 C bb 0.04549719
## 4 D bb 0.08911820
## 5 A cc 0.23320660
## 6 B cc 0.67526880
## 7 C cc 0.08917197
## 8 D cc 0.43724140
#data_3$cate <- gsub("\\.", " ", data_3$cate)
#data_3$value <- data_3$value * 100
data_3$cate <- factor(data_3$cate, levels = unique(data_3$cate))
data_3$aa <- factor(data_3$aa, levels = unique(data_3$aa)[c(1,2,3,4)])
######################################
library(ggplot2)
draw_key_polygon3 <- function(data, params, size) {
lwd <- min(data$size, min(size) / 4)
grid::rectGrob(
width = grid::unit(0.6, "npc"),
height = grid::unit(0.6, "npc"),
gp = grid::gpar(
col = data$colour,
fill = alpha(data$fill, data$alpha),
lty = data$linetype,
lwd = lwd * .pt,
linejoin = "mitre"
))
}
#GeomBar$draw_key = draw_key_polygon3
ggplot(data_3, aes(x=aa, y=value, color = cate)) +
#geom_bar(position=position_dodge(0.9), stat="identity",
#colour="red", # Use black outlines
# fill=NA,
# size=0.5) + # Thinner lines
geom_col(width = 0.55, position = position_dodge(0.65),fill= NA,size = 0.6,key_glyph = "polygon3")+
xlab("") +
ylab("Hit rate (%)") +
ggtitle("") +
scale_color_manual(name=NULL, # Legend label, use darker colors
values=c("darkblue","darkred")) +
scale_y_continuous(expand = c(0,0),breaks = seq(0,.7,.20),limits = c(0,.70)) +
theme(legend.position = c(0.82,.95),
#legend.direction = "vertical",
#legend.spacing.x = unit(1, 'cm'),
#legend.spacing.y = unit(0.1, 'cm'),
#legend.title.align = 0.1,
legend.key.size = unit(.9, "cm"),
legend.text = element_text(colour="black", size=12,
face="plain"),
# legend.title = element_text(colour="red", size=10,
# face="bold"),
#legend.background = element_rect(fill="lightblue",
# size=2, linetype="solid",
# colour ="blue"),
#legend.key.height=unit(1.2,"line"),
#legend.key.width=unit(1.2,"line"),
#legend.margin=margin(5,5,5,5),
#legend.justification = c(0,1),
#legend.box.margin=margin(0,0,0,0),
legend.key = element_rect(colour = NA, fill = NA),
panel.background = element_blank(),
panel.border = element_rect(colour = NA, fill= NA, size=1),
panel.grid = element_blank(),
axis.text.x = element_text(size= 12, color = "black",family = "sans",hjust = 1,vjust = 1,angle = 45),
axis.text.y = element_text(size= 12, color = "black",family = "sans",vjust = 0.5,hjust = 0.5),
axis.title = element_text(size=12, color = "black",family = "sans"),
axis.ticks = element_line(size= 0.5),
axis.ticks.length = unit(3, "pt"),
#axis.title=element_text(size=14,face="bold"),
axis.line = element_line(size = 0.5, colour = "black"))

ggsave(paste0(Sys.Date(),"-hit_rate_spacing_legend.tiff"),
plot = last_plot(), device = NULL, path = dir_path,
scale = 1, width = 13, height = 14, units ="cm",dpi = 300,
limitsize = TRUE,compression = "lzw")
library(eoffice)
## Warning: package 'eoffice' was built under R version 4.0.5
## Bioconductor version '3.11' is out-of-date; the current release version '3.13'
## is available with R version '4.1'; see https://bioconductor.org/install
topptx(filename ="mtcars_1.pptx")
#ref https://stackoverflow.com/questions/11366964/is-there-a-way-to-change-the-spacing-between-legend-items-in-ggplot2