Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

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

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...