如何在 Rstudio 的 ggplot2 中添加主标题和操作轴标签

How to add main title and manipulating axis labels in ggplot2 in Rstudio

我是 R 的新手,我正在使用 Rstudio。

我有一个用于绘制条形图的 ggplot2 R 脚本(something same as this) that I have created it by 但是它有两个看起来非常简单但我无法解决的错误:

1)x轴标签必须是函数Class,但它是 FunctionClass(没有 space)。当我添加 space 时,它显示此错误 -> :Error: unexpected symbol in "p <- ggplot(data=dat, aes(x=Function Class"

2)我无法将主标题添加到剧情中

我有这一行 "p + guides (fill = guide_legend(ncol = 1))" 用于在一列中显示图例(我的脚本中需要它)。

当我在前面插入脚本"p + ggtitle ("COG函数Class共识序列的化”)+”时,没有主标题。当我在那之后放置这个脚本时,有一个标题,但一列图例变成了一个丑陋的两列图例! X 轴命名仍然存在。 (我什至使用过 opt()、theme() 和...,但它们每个都显示另一个错误,所以这不是一个重复的问题,这是我的案例问题)。

帮助我,如果可能的话,在我的脚本中添加适当的行或提及脚本和我必须插入它们的位置,因为看起来请每条命令对整个程序都有一定的影响。

提前谢谢你 这是我的脚本 :

    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)
)

library(ggplot2)

p <- ggplot(data=dat, aes(x=FunctionClass, y=Frequency, fill=legend))+

geom_bar(stat="identity", position=position_dodge(), colour="seashell")

p + guides (fill = guide_legend(ncol = 1))

尝试添加 xlabggtitle:

p <- ggplot(data=dat, aes(x=FunctionClass, y=Frequency, fill=legend))+
geom_bar(stat="identity", position=position_dodge(), colour="seashell")
p + guides (fill = guide_legend(ncol = 1))+
xlab("Factor Class")+
ggtitle("Plant Growth")