作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些 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'))
expand
, 通过添加
scale_x_continuous(limits=c(0, 100), expand=c(0, 0))
到我的
ggplot
目的。结果:
最佳答案
您可以使用 geom_segment
控制网格线的范围创建网格线。例如:
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")
关于r - 切断散点图网格线,但不完全在轴限制处标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46126233/
我是一名优秀的程序员,十分优秀!