2017-04-17 72 views
0

使用以下R代码,我在一个图上生成了多个图。合并多个图的x轴和图例

library(ggplot2) 

Adata=nt.df[(nt.df$Species=='Human' | nt.df$Species=='Arabidopsis')& nt.df$Nucleotide=='A',] 
Cdata=nt.df[(nt.df$Species=='Human' | nt.df$Species=='Arabidopsis')& nt.df$Nucleotide=='C',] 
Gdata=nt.df[(nt.df$Species=='Human' | nt.df$Species=='Arabidopsis')& nt.df$Nucleotide=='G',] 
Udata=nt.df[(nt.df$Species=='Human' | nt.df$Species=='Arabidopsis')& nt.df$Nucleotide=='U',] 
# Grouped 
Aplot <- ggplot(data, aes(fill=Species, y=Percent, x=Position)) + 
    geom_bar(position="dodge", stat="identity") 

Cplot <- ggplot(data, aes(fill=Species, y=Percent, x=Position)) + 
    geom_bar(position="dodge", stat="identity") 

Gplot <- ggplot(data, aes(fill=Species, y=Percent, x=Position)) + 
    geom_bar(position="dodge", stat="identity") 

Uplot <- ggplot(data, aes(fill=Species, y=Percent, x=Position)) + 
    geom_bar(position="dodge", stat="identity") 


grid.arrange(Aplot,Cplot,Gplot,Uplot,ncol=1) 

enter image description here

如何合并x轴标记,y轴的标签,进入1 Species传说为整个数字?

此外,将位置刻度标记标记为整个图形还是每个图形会更有意义吗?

+0

此[R Bloggers Post](https://www.r-bloggers.com/another-rule-of-three-this- one-in-statistics /)有你想要的例子。这不是帖子的要点,但你应该能够遵循代码来看看如何得到这个。 – G5W

+0

您应该考虑将数据合并到单个数据框中并使用'facet_wrap()',它还会合并您的轴标记。 – liborm

回答

0

试试这个:

require(dplyr) 
require(ggplot2) 

nt.df %>% 
    filter(Species %in% c('Human', 'Arabidopsis')) %>% 
    ggplot(aes(fill = Species, y = Percent, x = Position)) + 
    geom_bar(position="dodge", stat="identity") + 
    facet_wrap(. ~ Nucleotide) 

由于数据未张贴我无法测试它,但这应该工作。让我知道你是否有错误。如果你之前从未使用管道(%>%),这是一种使代码更具可读性和简洁性的流行方式。基本上它使左边的功能中的第一个arg位于右侧。在这种情况下,数据是ggplot()中的第一个参数,因此已过滤的数据集将进入ggplot()

+0

我该如何在jupyter中显示剧情? – ahtmatrix

+0

我实际上使用RMarkdown,而不是Jupyter,所以我没有任何建议。这是否回答你的问题? – Zafar

+0

我确实花了一些时间帮助你,所以如果这对你有帮助,我会很高兴答复upvote或者最好的答案 – Zafar