[英](R ggplot2) Remove grid lines and spacing between panel border and axis text or ticks
Here is an example plot:这是一个示例图:
library(tidyverse)
tibble(x=c(0,100), y=c(1,0)) %>%
ggplot(aes(x=x,y=y)) +
geom_segment(x=0, y=0, xend=0, yend=1.1, color="gray60", size=0.6, lineend="square") +
geom_segment(x=0, y=0, xend=110, yend=0, color="gray60", size=0.6, lineend="square") +
geom_line(color="#66ccff", size=3, lineend = "round") +
theme_minimal() +
labs(y="Y Label", x="X Label") +
scale_x_continuous(breaks=seq(0,100,by=20), minor_breaks=NULL) +
scale_y_continuous(labels = scales::percent, minor_breaks=NULL) +
theme(axis.title = element_text(size = 16, color="gray30"),
axis.text = element_text(size=14, color="gray30"))
I want to eliminate the grid outside the limits of the data.
我想消除数据限制之外的网格。 This (less ideal, imo) hack with the rectangles covers up the grid lines outside of the panel border:
这个(不太理想,imo)用矩形修改覆盖了面板边框外的网格线:
tibble(x=c(0,100), y=c(1,0)) %>%
ggplot(aes(x=x,y=y)) +
geom_line(color="#66ccff", size=3, lineend = "round") +
geom_segment(x=0, y=0, xend=0, yend=1.1, color="gray60", size=0.6, lineend="square") +
geom_segment(x=0, y=0, xend=110, yend=0, color="gray60", size=0.6, lineend="square") +
theme_minimal() +
geom_rect(xmin=-5, xmax=0, ymin=-4, ymax=1.1, fill="white") +
geom_rect(xmin=-5, xmax=110, ymin=-4, ymax=0, fill="white") +
labs(y="Y Label", x="X Label") +
scale_x_continuous(breaks=seq(0,100,by=20), minor_breaks=NULL) +
scale_y_continuous(labels = scales::percent, minor_breaks=NULL) +
theme(axis.title = element_text(size = 16, color="gray30"),
axis.text = element_text(size=14, color="gray30"))
But I really want a way to eliminate/reduce that space, so that it looks more like this:
但我真的想要一种方法来消除/减少那个空间,让它看起来更像这样:
I very often find people who want plots to look like this.
我经常发现有人希望情节看起来像这样。 How could I do that?
我怎么能那样做?
If I understood you well, what you want is to remove the ticks that point to each of the values in the X and Y axis.如果我理解你,你想要的是删除指向 X 和 Y 轴上每个值的刻度。 If that is the case, you can try with :
如果是这种情况,您可以尝试:
tibble(x=c(0,100), y=c(1,0)) %>%
ggplot(aes(x=x,y=y)) +
geom_segment(x=0, y=0, xend=0, yend=1.1, color="gray60", size=0.6, lineend="square") +
geom_segment(x=0, y=0, xend=110, yend=0, color="gray60", size=0.6, lineend="square") +
geom_line(color="#66ccff", size=3, lineend = "round") +
theme_minimal() +
labs(y="Y Label", x="X Label") +
scale_x_continuous(breaks=seq(0,100,by=20), minor_breaks=NULL) +
scale_y_continuous(labels = scales::percent, minor_breaks=NULL) +
theme(axis.title = element_text(size = 16, color="gray30"),
axis.text = element_text(size=14, color="gray30"),
axis.ticks = element_blank())
The only modification to your original code was the axis.ticks = element_blank()
对原始代码的唯一修改是
axis.ticks = element_blank()
EDIT: It seems (from the image with your desired output) that you also want to make the axis to be more close to the panel of your plot.编辑:似乎(从具有所需输出的图像中)您还希望使轴更靠近绘图面板。 If that's the case, you can try to add
expand = c(0,0)
to both scale_x_continuos
and scale_y_continuos
functions.如果是这种情况,您可以尝试将
expand = c(0,0)
添加到scale_x_continuos
和scale_y_continuos
函数中。 If that's true, your code should look like this:如果这是真的,您的代码应如下所示:
tibble(x=c(0,100), y=c(1,0)) %>%
ggplot(aes(x=x,y=y)) +
geom_segment(x=0, y=0, xend=0, yend=1.1, color="gray60", size=0.6, lineend="square") +
geom_segment(x=0, y=0, xend=110, yend=0, color="gray60", size=0.6, lineend="square") +
geom_line(color="#66ccff", size=3, lineend = "round") +
theme_minimal() +
labs(y="Y Label", x="X Label") +
scale_x_continuous(breaks=seq(0,100,by=20), minor_breaks=NULL, expand = c(0,0)) +
scale_y_continuous(labels = scales::percent, minor_breaks=NULL, expand = c(0,0)) +
theme(axis.title = element_text(size = 16, color="gray30"),
axis.text = element_text(size=14, color="gray30"),
axis.ticks = element_blank())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.