编辑ggplot中轴刻度和轴标签之间的间距。

huangapple go评论84阅读模式
英文:

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()).

编辑ggplot中轴刻度和轴标签之间的间距。

Created on 2023-04-10 with reprex v2.0.2

This is what I want:

编辑ggplot中轴刻度和轴标签之间的间距。

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()`).

编辑ggplot中轴刻度和轴标签之间的间距。<!-- -->

<sup>Created on 2023-04-10 with reprex v2.0.2</sup>

This is what I want:

编辑ggplot中轴刻度和轴标签之间的间距。

I have tried to use the axis.text.x = element_text(margin = margin(t = 0,r = 0,b = 2,l = 0, unit=&quot;cm&quot;))) 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()`).

编辑ggplot中轴刻度和轴标签之间的间距。

创建于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 %&gt;% 
  ggplot() +
  geom_point(aes(x=bill_length_mm,bill_depth_mm, fill=species), size=5, shape=21, color=&quot;white&quot;, alpha=0.7) +
  scale_fill_manual(values=c(&quot;#1B6CA8&quot;,&quot;#D78E00&quot;,&quot;#438A5E&quot; )) +
  guides(fill=guide_legend(override.aes = list(alpha=1))) +
  theme_bw() +
  theme(text=element_text(size=12), 
        axis.ticks.length = unit(0.25, &quot;cm&quot;), 
        axis.text.x = element_text(margin = margin(5,0,0,0)), 
        axis.text.y = element_text(margin = margin(0,5,0,0)))
#&gt; Warning: Removed 2 rows containing missing values (`geom_point()`).

编辑ggplot中轴刻度和轴标签之间的间距。<!-- -->

<sup>Created on 2023-04-10 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年4月10日 20:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定