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:
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. 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()
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. 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())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.