如何在Rstudio的ggplot2中添加主标题和操作轴标签 [英] How to add main title and manipulating axis labels in ggplot2 in Rstudio

查看:2205
本文介绍了如何在Rstudio的ggplot2中添加主标题和操作轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个ggplot2 R脚本来绘制条形图(

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

解决方案

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

这篇关于如何在Rstudio的ggplot2中添加主标题和操作轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆