gpt4 book ai didi

r - 如何增加 ggplot2 中的图例标题行间距?

转载 作者:行者123 更新时间:2023-12-04 03:15:48 26 4
gpt4 key购买 nike

我是一个全新的 R 用户,这个社区对我的学习过程非常有帮助,非常感谢!

我有一个 ggplot 图例标题,其中包含一个斜体字和一个上标,我试图将其堆叠成三行。看起来斜体字和上标占用了额外的空间,因此不会很好地堆叠。理想情况下,我的标题是:

上一页巨囊藻 密度m^2

每个词各占一行,但我很难到达那里...

这是我的代码(抱歉它很丑,记住我是新手!)和结果图:

    ggplot(mtcars,aes(x=wt,y=mpg, color = hp))+
geom_jitter(size = 16) +
scale_color_continuous(high="black", low="light grey") +
theme_bw() +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
legend.title=element_text(size=20),
legend.text=element_text(size =15),
legend.key = element_rect(size = 5),
legend.key.height=unit(3,"line"),
legend.key.size = unit(2.5, 'lines')) +
xlab("Weight") +
ylab("MPG") +
labs(color = expression(paste("Previous\n", italic("Macrocystis\n"),
"Density", " ", m^2), sep=" "))

legend label with italicized Mac but weird spacing over two lines

感谢您的帮助,感谢您提供如此出色的资源!

更新:感谢@baptiste 让我更接近一点,但我无法按正确的顺序排列单词,这让我有点抓狂。这是我迄今为止最好的草稿:

    ggplot(mtcars,aes(x=wt,y=mpg, color = hp))+
geom_jitter(size = 16) +
scale_color_continuous(high="black", low="light grey") +
theme_bw() +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
legend.title=element_text(size=20),
legend.text=element_text(size =15),
legend.key = element_rect(size = 5),
legend.key.height=unit(3,"line"),
legend.key.size = unit(2.5, 'lines')) +
xlab("Weight") +
ylab("MPG") +
labs(color = expression(atop(italic("Macrocystis\n"),
paste("Previous\nDensity", " ", m^2), sep=" ")))

Mac previous density instead of previous mac density

最佳答案

这有点老套,因为它实际上不受支持,但是如果您使用嵌套的 atop() 和 scriptstyle() 进行某些格式化:

ggplot(mtcars,aes(x=wt,y=mpg, color = hp))+
geom_jitter(size = 16) +
scale_color_continuous(high="black", low="light grey") +
theme_bw() +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
legend.title=element_text(size=20),
legend.text=element_text(size =15),
legend.key = element_rect(size = 5),
legend.key.height=unit(3,"line"),
legend.key.size = unit(2.5, 'lines')) +
xlab("Weight") +
ylab("MPG") +
labs(color = expression(atop(scriptstyle("Previous"), atop(paste(italic(" Macrocystis")),
" Density m" ^2 ), sep=" ")))

enter image description here

关于r - 如何增加 ggplot2 中的图例标题行间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41109110/

26 4 0
文章推荐: c# - 为什么不能将 Action 委托(delegate)用作线程池的 WaitCallback?