生成两个具有相同x轴断裂的图

时间:2018-12-13 20:59:14

标签: r ggplot2

我有两个图,我希望x轴以相同的方式断开。 这是情节1的代码:

m <- read.csv('Finalfor1lowergreaterthan1.csv', header=T, row.names=1)
m <- m[m$SVM.Count >= 40,]
boxOdds = m$Odd

df <- data.frame(
  yAxis = length(boxOdds):1,
  boxnucleotide = m$Position,
  boxCILow = m$lower,
  boxCIHigh = m$upper,
  Mutation = m$Resistance)

ticksy <- c(seq(0,0.3,by=.1), seq(0, 1, by =.5), seq(0, 20, by =5), seq(0, 150, by =50))
ticksx <- c(seq(0,300,by=25))
p <- ggplot(df, 
            aes(x = boxnucleotide, y = boxOdds, colour=Mutation, label=rownames(m)))
p1 <- p + 
  geom_errorbar(aes(ymax = boxCIHigh, ymin = boxCILow), size = .5, height = .01) +
  geom_point(size = 1) +
  theme_bw() +
  theme(panel.grid.minor = element_blank()) +
  scale_y_continuous(breaks=ticksy, labels = ticksy) +
  scale_x_continuous(breaks=ticksx, labels = ticksx) +
  coord_trans(y = "log10") +
  ylab("Odds ratio (log scale)") +
  scale_color_manual(values=c("#00BFC4","#F8766D","#619CFF")) +
  xlab("Integrase nucleotide position") + 
  geom_text(size=2,hjust=0, vjust=0)

plot1

然后我有另一个情节:

m <- read.csv('Finalfor20lowergreaterthan1.csv', header=T, row.names=1)
#m <- m[m$SVM.Count >= 40, ]
boxOdds = m$Odd

df <- data.frame(
  yAxis = length(boxOdds):1,
  boxnucleotide = m$Position,
  boxCILow = m$lower,
  boxCIHigh = m$upper,
  Mutation = m$Resistance)

ticksy <- c(seq(0,0.3,by=.1), seq(0, 1, by =.5), seq(0, 20, by =5), seq(0, 150, by =50))
ticksx <- c(seq(0,300,by=25))
p <- ggplot(df, 
            aes(x = boxnucleotide, y = boxOdds, colour=Mutation, label=rownames(m)))
p1 <- p + 
  geom_errorbar(aes(ymax = boxCIHigh, ymin = boxCILow), size = .5, height = .01) +
  geom_point(size = 1) +
  theme_bw() +
  theme(panel.grid.minor = element_blank()) +
  scale_y_continuous(breaks=ticksy, labels = ticksy) +
  scale_x_continuous(breaks=ticksx, labels = ticksx) +
  coord_trans(y = "log10") +
  ylab("Odds ratio (log scale)") +
  scale_color_manual(values=c("#00BFC4","#F8766D","#619CFF")) +
  xlab("Integrase nucleotide position") + 
  geom_text(size=2,hjust=0, vjust=0)

plot2

为什么图1从x轴上的75开始,图2从100开始...为什么图2也从75上开始,并像图1那样缩放。

这两个代码具有相同的部分:ticksx <- c(seq(0, 300, by=25))

1 个答案:

答案 0 :(得分:1)

一种在不同图形上对齐轴范围的好方法是使用expand_limits

您可以简单地使用p1 + expand_limits(x=c(0, 300))。这样可以确保所有绘图中的x轴至少包含0和300。您还可以使用y参数来控制y轴范围。

来自?expand_limits

  

有时,您可能需要确保所有面板或所有图的限制都包含一个值。该函数是geom_blank()的一个薄包装,可以轻松添加此类值。