英文:
Edit the space between axis ticks and axis labels in ggplot
问题
I want to learn to increase the space between the axis.ticks
and the axis labels.
Here is an example:
library(tidyverse)
library(palmerpenguins)
penguins %>%
ggplot() +
geom_point(aes(x=bill_length_mm,bill_depth_mm, fill=species), size=5, shape=21, color="white", alpha=0.7) +
scale_fill_manual(values=c("#1B6CA8","#D78E00","#438A5E" )) +
guides(fill=guide_legend(override.aes = list(alpha=1))) +
theme_bw() +
theme(text=element_text(size=12),
axis.ticks.length = unit(0.25, "cm"))
# Warning: Removed 2 rows containing missing values (geom_point()).
Created on 2023-04-10 with reprex v2.0.2
This is what I want:
I have tried to use the axis.text.x = element_text(margin = margin(t = 0,r = 0,b = 2,l = 0, unit="cm")))
but with this command, I am only moving the title. I only want to move the labels, NOT the title!
英文:
I want to learn to increase the space between the axis.ticks
and the axis labels.
Here is an example:
library(tidyverse)
library(palmerpenguins)
penguins %>%
ggplot() +
geom_point(aes(x=bill_length_mm,bill_depth_mm, fill=species), size=5, shape=21, color="white", alpha=0.7) +
scale_fill_manual(values=c("#1B6CA8","#D78E00","#438A5E" )) +
guides(fill=guide_legend(override.aes = list(alpha=1))) +
theme_bw() +
theme(text=element_text(size=12),
axis.ticks.length = unit(0.25, "cm"))
#> Warning: Removed 2 rows containing missing values (`geom_point()`).
<!-- -->
<sup>Created on 2023-04-10 with reprex v2.0.2</sup>
This is what I want:
I have tried to use the axis.text.x = element_text(margin = margin(t = 0,r = 0,b = 2,l = 0, unit="cm")))
but with this command, I am only moving the title. I only want to move the labels, NOT the title!
答案1
得分: 2
感谢来自R4DS Slack组的Philippe Massicotte的建议,以下是解决方案:
library(tidyverse)
library(palmerpenguins)
penguins %>%
ggplot() +
geom_point(aes(x=bill_length_mm, bill_depth_mm, fill=species), size=5, shape=21, color="white", alpha=0.7) +
scale_fill_manual(values=c("#1B6CA8", "#D78E00", "#438A5E" )) +
guides(fill=guide_legend(override.aes = list(alpha=1))) +
theme_bw() +
theme(text=element_text(size=12),
axis.ticks.length = unit(0.25, "cm"),
axis.text.x = element_text(margin = margin(5,0,0,0)),
axis.text.y = element_text(margin = margin(0,5,0,0)))
#> Warning: Removed 2 rows containing missing values (`geom_point()`).
创建于2023年4月10日,使用reprex v2.0.2
英文:
Thanks to the suggestion of Philippe Massicotte from the R4DS slack group here is a solution:
library(tidyverse)
library(palmerpenguins)
penguins %>%
ggplot() +
geom_point(aes(x=bill_length_mm,bill_depth_mm, fill=species), size=5, shape=21, color="white", alpha=0.7) +
scale_fill_manual(values=c("#1B6CA8","#D78E00","#438A5E" )) +
guides(fill=guide_legend(override.aes = list(alpha=1))) +
theme_bw() +
theme(text=element_text(size=12),
axis.ticks.length = unit(0.25, "cm"),
axis.text.x = element_text(margin = margin(5,0,0,0)),
axis.text.y = element_text(margin = margin(0,5,0,0)))
#> Warning: Removed 2 rows containing missing values (`geom_point()`).
<!-- -->
<sup>Created on 2023-04-10 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论