英文:
Remove any space beyond the square with data
问题
I have the general mtcars
data as an example.
我以通用的 mtcars
数据为例。
I create a quick geom_point()
with two variables:
我创建了一个快速的 geom_point()
,使用了两个变量:
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23)
What I want is to remove everything beyond the "gray-matrix" square.
我想要移除灰色矩形之外的所有内容。
For instance, I removed the axis, titles, and ticks:
例如,我移除了坐标轴、标题和刻度:
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23) +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
However, in this figure there is still a white area between the gray area and the external limit of the image.
然而,在这个图中,灰色区域和图像的外部边界之间仍然存在白色区域。
This can be visualized in RStudio. In the next image, I show the "white" margin that still persists in the plot:
这可以在RStudio中可视化。在下一张图片中,我展示了在图中仍然存在的"白色"边距:
My question, therefore, is how can I remove that white area beyond the gray square so that the square takes full space?
因此,我的问题是如何移除超出灰色方块的白色区域,使方块占满整个空间?
英文:
I have the general mtcars
data as an example.
I create a quick geom_point()
with two variables:
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23)
What I want is to remove everything beyond the "gray-matrix" square.
For instance, I removed the axis, titles and ticks:
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23) +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
However, in this figure there is still a white area between the gray area and the external limit of the image.
This can be visualized in RStudio. In the next image I show the "white" margin that still persists in the plot:
My question, therefore, is how can I remove that white area beyond the gray square, so that the square takes full space?
答案1
得分: 3
Using theme_void
> "一个完全空白的主题。"
ggplot(data.frame(x = 0:4, y = 0:4), aes(x, y)) +
geom_point(size = 2, shape = 23) +
"一个完全空白的主题。"
theme_void() +
设置扩展为0以去除更多的空白
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
检查
theme(panel.background = element_rect("yellow"))
英文:
Using theme_void
> "A completely empty theme."
ggplot(data.frame(x = 0:4, y = 0:4), aes(x, y)) +
geom_point(size = 2, shape = 23) +
# "A completely empty theme."
theme_void() +
# set expansion to 0 to remove more white space
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
# check
theme(panel.background = element_rect("yellow"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论