添加whitespace/在ggplot中X轴和Y轴之间增加space
Add whitespace / increase the space between the X and the Y axis in ggplot
我正在为一个需要具有特定外观的项目绘制一些图。他们需要在轴线和绘图面板之间有一些 space。
library(ggplot2)
plot_data <- data.frame(X = 1:10, Y = 1:10)
ggplot() + geom_point(data = plot_data, aes(x = X, y = Y)) +
theme(axis.line = element_line(colour = "black", linetype = "solid"))
我有什么
我想在我的绘图的 X 轴和 Y 轴之间添加一些距离,但我的轴线不会扩展,就像我使用扩展命令时一样。
plot_data <- data.frame(X = 1:10, Y = 1:10)
ggplot() + geom_point(data = plot_data, aes(x = X, y = Y)) +
theme(axis.line = element_line(colour = "black", linetype = "solid"))+
scale_x_continuous(name = "X", limits = c(1, 10), expand = c(0.1,0)) +
scale_y_continuous(name = "Y", limits = c(1, 10), expand = c(0.1,0))
我能做什么
在 R 中有快速可靠的方法吗?
我想要什么
提前谢谢大家!
您可以使用 lemon
包实现此 "capping" 行。您可以使用以下代码来实现此目的:
library(ggplot2)
library(lemon)
### your code
plot_data <- data.frame(X = 1:10, Y = 1:10)
p <- ggplot() + geom_point(data = plot_data, aes(x = X, y = Y)) +
theme(axis.line = element_line(colour = "black", linetype = "solid"))+
scale_x_continuous(name = "X", limits = c(1, 10), expand = c(0.1,0)) +
scale_y_continuous(name = "Y", limits = c(1, 10), expand = c(0.1,0))
### using the lemon package
p + coord_capped_cart(bottom='right', left='none', gap = 0.15)
### mimic the view of your plot
p2 <- p + coord_capped_cart(bottom='right', left='none', gap = 0.15)
p2 + theme(panel.background = element_rect(fill = "white"))
生成以下图片:
另一种选择:删除轴线并使用 geom_segment 创建新的轴线。
plot_data <- data.frame(X = 1:10, Y = 1:10)
library(ggplot2)
ggplot() + geom_point(data = plot_data, aes(x = X, y = Y)) +
theme(axis.line = element_blank(),panel.background =element_blank())+
geom_segment(aes(x=0,y=1,xend=0,yend=10))+
geom_segment(aes(x=1,y=0,xend=10,yend=0))+
scale_x_discrete(limits=c(1,2,3,4,5, 8,10))+
scale_y_discrete(limits=c(1:10))
我正在为一个需要具有特定外观的项目绘制一些图。他们需要在轴线和绘图面板之间有一些 space。
library(ggplot2)
plot_data <- data.frame(X = 1:10, Y = 1:10)
ggplot() + geom_point(data = plot_data, aes(x = X, y = Y)) +
theme(axis.line = element_line(colour = "black", linetype = "solid"))
我有什么
我想在我的绘图的 X 轴和 Y 轴之间添加一些距离,但我的轴线不会扩展,就像我使用扩展命令时一样。
plot_data <- data.frame(X = 1:10, Y = 1:10)
ggplot() + geom_point(data = plot_data, aes(x = X, y = Y)) +
theme(axis.line = element_line(colour = "black", linetype = "solid"))+
scale_x_continuous(name = "X", limits = c(1, 10), expand = c(0.1,0)) +
scale_y_continuous(name = "Y", limits = c(1, 10), expand = c(0.1,0))
我能做什么
在 R 中有快速可靠的方法吗?
我想要什么
提前谢谢大家!
您可以使用 lemon
包实现此 "capping" 行。您可以使用以下代码来实现此目的:
library(ggplot2)
library(lemon)
### your code
plot_data <- data.frame(X = 1:10, Y = 1:10)
p <- ggplot() + geom_point(data = plot_data, aes(x = X, y = Y)) +
theme(axis.line = element_line(colour = "black", linetype = "solid"))+
scale_x_continuous(name = "X", limits = c(1, 10), expand = c(0.1,0)) +
scale_y_continuous(name = "Y", limits = c(1, 10), expand = c(0.1,0))
### using the lemon package
p + coord_capped_cart(bottom='right', left='none', gap = 0.15)
### mimic the view of your plot
p2 <- p + coord_capped_cart(bottom='right', left='none', gap = 0.15)
p2 + theme(panel.background = element_rect(fill = "white"))
生成以下图片:
另一种选择:删除轴线并使用 geom_segment 创建新的轴线。
plot_data <- data.frame(X = 1:10, Y = 1:10)
library(ggplot2)
ggplot() + geom_point(data = plot_data, aes(x = X, y = Y)) +
theme(axis.line = element_blank(),panel.background =element_blank())+
geom_segment(aes(x=0,y=1,xend=0,yend=10))+
geom_segment(aes(x=1,y=0,xend=10,yend=0))+
scale_x_discrete(limits=c(1,2,3,4,5, 8,10))+
scale_y_discrete(limits=c(1:10))