R 使用相同的x轴在xyplot下方打印条形图?

R 使用相同的x轴在xyplot下方打印条形图?,r,plot,ggplot2,R,Plot,Ggplot2,我试图在彼此下方绘制不同类型的图(线形图和条形图),它们都具有相同的轴: c1 <- ggplot(data, aes(date, TotalMutObs)) + stat_smooth(se = FALSE) + geom_point() + opts(axis.title.x = theme_blank()) + ylab("Cumulative number of new mutations") c2

我试图在彼此下方绘制不同类型的图(线形图和条形图),它们都具有相同的轴:

c1 <- ggplot(data, aes(date, TotalMutObs)) + stat_smooth(se = FALSE) + 
            geom_point() + 
            opts(axis.title.x = theme_blank()) +
            ylab("Cumulative number of new mutations")   
c2 <- ggplot(data, aes(date, distance)) + stat_smooth(se = FALSE) + 
            geom_point() + 
            opts(axis.title.x = theme_blank()) + 
            ylab("Cumulative mean pairwise distance")   
c3 <- ggplot(data, aes(x = date, y = NbOfHorses)) + 
            geom_bar(stat = "identity") + 
            opts(axis.title.x = theme_blank()) + 
            ylab("Number of horses sampled")

grid.arrange(c1, c2,c3)

c1解决此问题的方法是在ggplot2内工作,创造性地堆叠数据副本,然后将子集发送到所需的每个几何体

#A version of your data cleaned up
dat <- data.frame(date = as.Date(date),NbOfHorses = as.numeric(NbOfHorses),
                TotalMutObs = as.numeric(TotalMutObs),distance = as.numeric(distance))

#Create three copies, one for each panel
# Use informative titles for grp to be panel titles    
fullDat <- rbind(dat,dat,dat)
fullDat$grp <- rep(c('Cumulative number of new mutations',
                        'Cumulative mean pairwise distance',
                        'Number of horses sampled'),each = nrow(dat))

ggplot(fullDat,aes(x = date)) + 
    facet_wrap(~grp,nrow = 3,scale = "free_y") + 
    geom_point(data = subset(fullDat,grp == 'Cumulative number of new mutations'),
        aes(y = TotalMutObs)) + 
    stat_smooth(data = subset(fullDat,grp == 'Cumulative number of new mutations'),
        aes(y = TotalMutObs),se = FALSE) + 
    geom_point(data = subset(fullDat,grp == 'Cumulative mean pairwise distance'),
        aes(y = distance)) + 
    stat_smooth(data = subset(fullDat,grp == 'Cumulative mean pairwise distance'),
        aes(y = distance),se = FALSE) + 
    geom_bar(data = subset(fullDat,grp == 'Number of horses sampled'),
        aes(y = NbOfHorses),stat = "identity") + 
    labs(x = NULL,y = NULL)
#已清理数据的版本

dat ggplot2中没有
xlim()
函数吗?类似于
c3的东西我认为这不是问题,因为所有三个x轴的跨度完全相同。提出一些数据,我们将测试它。你是正确的。问题在于y轴标签的宽度。范围实际上都是一样的。提供的数据会抛出与该代码相关的错误,因为cbind创建了一个矩阵。最好用dput(data)的输出替换该代码。应该将其放在as.data.frame中。现在应该可以了。必须有固定x轴原点的方法来对齐所有绘图。有什么想法吗?
#A version of your data cleaned up
dat <- data.frame(date = as.Date(date),NbOfHorses = as.numeric(NbOfHorses),
                TotalMutObs = as.numeric(TotalMutObs),distance = as.numeric(distance))

#Create three copies, one for each panel
# Use informative titles for grp to be panel titles    
fullDat <- rbind(dat,dat,dat)
fullDat$grp <- rep(c('Cumulative number of new mutations',
                        'Cumulative mean pairwise distance',
                        'Number of horses sampled'),each = nrow(dat))

ggplot(fullDat,aes(x = date)) + 
    facet_wrap(~grp,nrow = 3,scale = "free_y") + 
    geom_point(data = subset(fullDat,grp == 'Cumulative number of new mutations'),
        aes(y = TotalMutObs)) + 
    stat_smooth(data = subset(fullDat,grp == 'Cumulative number of new mutations'),
        aes(y = TotalMutObs),se = FALSE) + 
    geom_point(data = subset(fullDat,grp == 'Cumulative mean pairwise distance'),
        aes(y = distance)) + 
    stat_smooth(data = subset(fullDat,grp == 'Cumulative mean pairwise distance'),
        aes(y = distance),se = FALSE) + 
    geom_bar(data = subset(fullDat,grp == 'Number of horses sampled'),
        aes(y = NbOfHorses),stat = "identity") + 
    labs(x = NULL,y = NULL)