在ggplot2中手动为x轴和y轴添加断点

时间:2016-08-26 02:34:46

标签: r ggplot2 axis scatter-plot

我通常使用scale_y_continuous(或scale_x_continuous)更改ggplot2中连续变量的缩放中断。我通常也会使用coord_cartesian(xlim=)(或ylim=为y轴)设置轴限制。一切正常如下:

#Some example data
set.seed(100)
b<-sample(1:10, 10)
a<-sample(1:10, 10)

df<-data.frame(a,b)

#Graph without scales set (just default)
library(ggplot2)
ggplot(data=df, aes(a,b))+
  theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                     panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), 
                     axis.line = element_line(colour = "black"))+
  geom_text(aes(label=rownames(df)), color="black")

使用默认比例生成以下图表:

enter image description here

如果我想将x轴调整为1-13(即13个刻度线从1开始到13结束),我会做以下事情:

ggplot(data=df, aes(a,b))+
  theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                     panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), 
                     axis.line = element_line(colour = "black"))+
  geom_text(aes(label=rownames(df)), color="black")+
  scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+
  coord_cartesian(xlim=c(0, 13))

enter image description here

...和y轴......

ggplot(data=df, aes(a,b))+
  theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                     panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), 
                     axis.line = element_line(colour = "black"))+
  geom_text(aes(label=rownames(df)), color="black")+
  scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+
  coord_cartesian(ylim=c(0, 13))

enter image description here

然而,当我尝试同时完成两个轴的相同比例调整时,输出不会产生我期望的结果(在x和y轴上都是1-13)。

ggplot(data=df, aes(a,b))+
  theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                     panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), 
                     axis.line = element_line(colour = "black"))+
  geom_text(aes(label=rownames(df)), color="black")+
  scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+
  coord_cartesian(ylim=c(0, 13))+
  scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+
  coord_cartesian(xlim=c(0, 13))

enter image description here

如您所见,即使完全相同的代码传递给两个轴,x轴和y轴也不相等。我不明白为什么。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:5)

set.seed(100)
df <- data.frame(b = sample(1:10, 10), a = sample(1:10, 10))    


ggplot(data=df, aes(a,b))+
theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                   panel.grid.major = element_blank(), 
                   panel.grid.minor = element_blank(), 
                   axis.line = element_line(colour = "black")) +
geom_text(aes(label=rownames(df)), color="black") +
scale_y_continuous(breaks = c(1,3,5,7,9,11,13)) +
scale_x_continuous(breaks = c(1,3,5,7,9,11,13)) +
coord_fixed(ylim=c(0, 13),xlim=c(0, 13))

enter image description here

答案 1 :(得分:2)

scale_*_continuous个函数可以使用limits参数代替coord_cartesian

ggplot(data=df, aes(a,b))+
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                       panel.grid.major = element_blank(), 
                       panel.grid.minor = element_blank(), 
                       axis.line = element_line(colour = "black"))+
    geom_text(aes(label=rownames(df)), color="black")+
    scale_y_continuous(breaks = c(1,3,5,7,9,11,13), limits = c(0, 13)) +
    scale_x_continuous(breaks = c(1,3,5,7,9,11,13), limits = c(0, 13))

plot with proper limits

...或者如果您坚持使用coord_cartesian,请在一次通话中执行此操作。否则它会裁剪你的图形,然后再次裁剪它,这就是导致问题的原因。

ggplot(data=df, aes(a,b))+
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                       panel.grid.major = element_blank(), 
                       panel.grid.minor = element_blank(), 
                       axis.line = element_line(colour = "black"))+
    geom_text(aes(label=rownames(df)), color="black")+
    scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+
    scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+
    coord_cartesian(xlim=c(0, 13), ylim = c(0, 13))
# returns the same thing