## 图一:常规分组条形图 ggplot(dat, aes(x = reorder(Description, Gene_Number), y = Gene_Number)) + geom_bar(aes(fill = group_type), stat = "identity") + labs(x = '', y = 'Gene Number', fill = '') + coord_flip() + theme(legend.position = 'bottom') + guides(fill = guide_legend(ncol = 1)) ## 图二:当功能名写在条形里 ggplot(dat, aes(x = reorder(Description, Gene_Number), y = Gene_Number)) + geom_bar(aes(fill = group_type), stat = "identity") + labs(y = 'Gene Number', fill = '') + coord_flip() + geom_text(aes(y = 0, label = Description, hjust = as.numeric(Gene_Number < 0))) + # label text based on value th + theme(legend.position = c(0.25, 0.9)) + guides(fill = guide_legend(ncol = 1)) ## 图三:当功能名写在条形外 ggplot(dat, aes(x = reorder(Description, Gene_Number), y = Gene_Number)) + geom_bar(aes(fill = group_type), stat = "identity") + labs(y = 'Gene Number', fill = '') + coord_flip() + geom_text(aes(y = 0, label = Description, hjust = as.numeric(Gene_Number > 0))) + # label text based on value th + theme(legend.position = c(0.25, 0.9)) + guides(fill = guide_legend(ncol = 1)) ## 图四:用条形颜色的深浅和长度同时表达数据 nbreaks = 10 minimum <- floor(min(dat$Gene_Number)/nbreaks)*nbreaks maximum <- ceiling(max(dat$Gene_Number)/nbreaks)*nbreaks
ggplot(dat, aes(x = reorder(Description, Gene_Number), y = Gene_Number)) + geom_bar(aes(fill = Gene_Number), stat = "identity") + labs(y = 'Gene Number', fill = '') + coord_flip() + geom_text(aes(y = 0, label = Description, hjust = as.numeric(Gene_Number > 0))) + # label text based on value th + scale_y_continuous(limits = c(minimum, maximum), breaks = seq(minimum, maximum, nbreaks)) + scale_fill_gradient2(low = 'darkblue', high = 'red', mid = 'white', limits = c(minimum, maximum), breaks = seq(minimum, maximum, nbreaks)) #修改图例名字以及图中颜色 ## 图五:借用ggpubr包绘制条形图 library(ggpubr) dat2 <- dat %>% dplyr::group_by(group_type) %>% dplyr::arrange(Gene_Number)
ggbarplot(dat2, x = "Description", y = "Gene_Number", fill = "group_type", # change fill color by mpg_level color = "white", # Set bar border colors to white palette = "jco", # jco journal color palett. see ?ggpar # sort.val = "desc", # Sort the value in descending order sort.by.groups = F, # Don't sort inside each group # x.text.angle = 90, # Rotate vertically x axis texts ylab = "Gene Number", legend.title = "", rotate = TRUE, ggtheme = theme_classic()) + theme(legend.position = 'bottom') + guides(fill = guide_legend(ncol = 1))