我有一些x值在0到100范围内的数据,如下所示:
library(ggplot2)
set.seed(42)
df <- data.frame(x=c(rep(100, 20), runif(100, min=0, max=100)),
y=rnorm(120, mean=4, sd=2))
由此代码生成的简单散点图:
ggplot(df, aes(x=x, y=y)) +
geom_point(size=5) +
theme(panel.grid.major=element_line(color='black'),
panel.grid.minor=element_line(color='black'),
panel.background=element_rect(fill='white'))
但是要强调0到100范围之外的x值是没有意义的,我想在x = 0和x = 100处精确地剪切x轴和水平网格线。我了解到,通过向expand
对象添加scale_x_continuous(limits=c(0, 100), expand=c(0, 0))
,ggplot
的正确方法是from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class Threebythreeone(BoxLayout):
pass
class Noughtsandcrosses(BoxLayout):
pass
class nandxApp(App):
def build(self):
return Noughtsandcrosses()
if __name__ == "__main__":
nandxApp().run()
。结果:
这缩短了网格线,但它也会剪切左边距和右边距的散点图标记,以及x轴上的100标签。我可以在边距之前切断x轴和网格线,但是渲染标记和轴标签就好像边距还在那里一样吗?
答案 0 :(得分:3)
您可以使用library(ggplot2)
library(scales)
yr = pretty(df$y)
xr = pretty(df$x)
ggplot() +
geom_segment(aes(y=rep(min(yr), length(xr)), yend=rep(max(yr), length(xr)),
x=xr, xend=xr), colour="grey70") +
geom_segment(aes(x=rep(min(xr), length(yr)), xend=rep(max(xr), length(yr)),
y=yr, yend=yr), colour="grey70") +
geom_point(data=df, aes(x,y), size=5) +
scale_y_continuous(breaks=yr, expand=c(0,0.02*diff(range(df$y)))) +
scale_x_continuous(breaks=xr, expand=c(0,0.02*diff(range(df$x)))) +
theme_classic() +
theme(axis.line=element_blank()) +
labs(x="x", y="y")
来创建网格线来控制网格线的范围。例如:
/