-->

How to add main title and manipulating axis labels

2020-08-04 04:03发布

问题:

I am new to R and I am using Rstudio.

I have a ggplot2 R script for drawing a bar plot (something same as this) that I have created it by others help but it has two errors that seems super simple but I can not solve them:

1) in the x axis label it must be the Function Class, but it is FunctionClass(without space). and when I add space, it shows this error -> :Error: unexpected symbol in "p <- ggplot(data=dat, aes(x=Function Class"

2) I can not add the Main Title to the plot

I have this line "p + guides (fill = guide_legend(ncol = 1))" for showing the legend in one column (and I need it in my script).

When I insert the script "p + ggtitle ("COG Function Classification of Consensus Sequences")+" before that, there is no main title. when I put this script after that, there is a title but the one column legend transform to an ugly two column legend! and the X axis naming is still exist. (I have even used opt(), theme() and ..., but each of them show another errors, so it is not a duplicated question and it is my case issue).

Help me and if it is possible, please add the appropriate lines in my script or mention the scripts and the position that I have to insert them because it seems that the please of each command has some effect on the whole program.

Thank you in advance and this is my script :

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

回答1:

Try adding xlab and ggtitle:

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