如何使用ggplot在轴上显示等于0的数据点?

时间:2017-02-15 20:18:06

标签: r ggplot2

我试图让我的数据点等于0出现在x轴上,避免数据点被轴切割。 我也希望保持原点(两个轴都相同)。

Here is my graph:

我用它来获得0的原点并设置我的轴​​的极限:

scale_y_continuous(limits = c(0, 40), expand = c(0, 0))+
scale_x_continuous(limits = c(0, 30), expand = c(0, 0))

如果我改变了

的y限制
scale_y_continuous(limits = c(-1, 40))

它也会改变原点......

以下是我需要的所有代码:

require(ggplot2)
myplot<-ggplot(data=NULL,aes(x = Length, y = Complete)) +
geom_point(data = C,shape=1, size=3)+
geom_point(data = T, size=3)+
scale_y_continuous(limits = c(0, 40), expand = c(0, 0))+
scale_x_continuous(limits = c(0, 30), expand = c(0, 0))+
xlab("Inflorescence Length (cm)")+
ylab("Successful Weevils")+
theme_bw() + 
theme(panel.border = element_blank())+
theme(panel.grid.major = element_blank())+
theme(panel.grid.minor = element_blank())+
theme(axis.text=element_text(size=12))+
theme(axis.title=element_text(size=14,face="bold"))+
theme(axis.line.x=element_line())+
theme(axis.line.y=element_line())+
theme(plot.margin = unit(c(1,1,1,1), "cm"))+
stat_smooth(data = C, method = lm, se=FALSE, color="grey")+
stat_smooth(data = T, method = lm, se=FALSE, color="black")
myplot

谢谢!

1 个答案:

答案 0 :(得分:1)

你必须关闭裁剪:

library(grid)
library(ggplot2)
df <- data.frame(x=1:1000, y=runif(1000))
p <- ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(limits = c(0,1), expand=c(0,0))
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)