R-将图例拆分为两列

R-将图例拆分为两列,r,ggplot2,legend,move,R,Ggplot2,Legend,Move,我有下面的R图 library(tidyverse) dat <- data.frame( FunctionClass = factor(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z"), levels=c("A", "B", "C", "D", "E", "F", "G",

我有下面的R图

library(tidyverse)

dat <- data.frame(
  FunctionClass = factor(c("A", "B", "C", "D", "E", "F", "G", "H", "I",     "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z"), levels=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z")),
  legend = c("A: RNA processing and modification", "B: Chromatin structure and dynamics", "C: Energy production and conversion", "D: Cell cycle control, cell division, chromosome partitioning", "E: Amino acid transport and metabolism", "F: Nucleotide transport and metabolism", "G: Carbohydrate transport and metabolism", "H: Coenzyme transport and metabolism", "I: Lipid transport and metabolism", "J: Translation, ribosomal structure and biogenesis", "K: Transcription", "L: Replication, recombination and repair", "M: Cell wall/membrane/envelope biogenesis", "N: Cell motility", "O: Posttranslational modification, protein turnover, chaperones", "P: Inorganic ion transport and metabolism", "Q: Secondary metabolites biosynthesis, transport and catabolism", "R: General function prediction only", "S: Function unknown", "T: Signal transduction mechanisms", "U: Intracellular trafficking, secretion, and vesicular transport", "V: Defense mechanisms", "W: Extracellular structures", "Y: Nuclear structure", "Z: Cytoskeleton"),
  Frequency=c(360,391,897,1558,1168,448,1030,536,732,1292,2221,2098,789,117,1744,732,437,5162,1251,2191,603,216,2,14,739)
)

dat <- cbind(
  dat
  , Frequency2=c(523,900,400,155,168,428,1050,516,742,129,221,2698,7829,1147,144,7132,4437,562,1551,2691,103,516,22,12,939))

p <- dat %>%
  gather("variable", "value", -FunctionClass, -legend) %>%
  ggplot(aes(FunctionClass, value, fill = legend, group = variable)) +
  geom_bar(stat="identity", position=position_dodge(), colour="seashell") +
  guides (fill = guide_legend(ncol = 1)) +
  xlab("COG Class")

p + theme(legend.position="bottom")
库(tidyverse)
dat从post这里有一个获得多列图例的解决方案:

p <- dat %>%
  gather("variable", "value", -FunctionClass, -legend) %>%
  ggplot(aes(FunctionClass, value, fill = legend, group = variable)) +
  geom_bar(stat="identity", position=position_dodge(), colour="seashell") +
  xlab("COG Class") +
  guides(fill=guide_legend(ncol=2)) # adjust the number of columns with ncol = xxx and be sure to use the right aes (fill, color, etc)

p + theme(legend.position="bottom")
p%
聚集(“变量”、“值”、-FunctionClass、-legend)%>%
ggplot(aes(函数类、值、填充=图例、组=变量))+
几何图形栏(stat=“identity”,position=position\u dodge(),color=“seashell”)+
xlab(“齿轮类”)+
辅助线(fill=guide_图例(ncol=2))#调整ncol=xxx的列数,确保使用正确的aes(fill、color等)
p+主题(图例位置=“底部”)

您看到了吗?这对我很管用。啊,当然!谢谢我认为
ncol=1
应该是
ncol=2
这能回答你的问题吗?