ggplot2中x轴刻度标签的下标和宽度限制

5
这是我目前针对上述图片的代码:
ggplot(AllData, aes(Year, AGResiduals, fill=Type)) + 
  geom_boxplot(outlier.size=0) + 
  scale_fill_manual(values=c("skyblue4", "skyblue"),
                    name="Male Type", 
                    labels=c("Guarders","Sneakers")) + 
  labs(x=NULL, y = "Residual of Accessory Gland Mass x Total Mass") +
  scale_x_discrete(limits=c("2007","2008","2010","2011","2013","2014","2015"), 
                   labels=str_wrap(c("2007 (nG=37, nS=8)","2008 (nG=4, nS=6)","2010 (nG=31, nS=6)","2011 (nG=55, nS=5)","2013 (nG=202, nS=24)","2014 (nG=63)","2015 (nG=59, nS=3)"),
                   width=6)) +
  theme(plot.title = element_text(size = rel(1.4)), 
        axis.title = element_text(size = rel(1.2)),
        axis.text.x = element_text(size = rel(1.5)),
        axis.text.y = element_text(size = rel(1.5)), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"))

我希望将每个x轴刻度标签中的“G”和“S”变为下标(它们表示两个不同组的样本量,G和S)。
写作
expression(2007 (n[G]=37, n[S]=8)

如果我删除前面的内容,这个功能可以工作。

str_wrap

因为某种原因需要编写代码。

我需要限制每个x轴刻度标签的文本宽度,因此需要保留str_wrap或在表达式函数中使用换行符。

我还不能用因为我必须设置要显示的年份范围,所以不能用我的标签列表替换成因子。

请有人帮忙解决如何制作一个允许下标的3行x轴刻度标签?

2个回答

2

我找不到一种在多行上显示表达式的方法,但您可以尝试旋转标签:

library(stringr)
library(ggplot2)
library(scales)
library(dplyr)

n <- 100
y <- as.character(sample(2007:2015,n,replace=T))
t <- sample(c("Guarders","Guarders","Sneakers"),n,replace=T)
r <- rnorm(n,10,20)
nsk <- sum(t=="Sneakers")
r[ t=="Sneakers" ] <- rnorm(nsk,1,5)
AllData <- data.frame(Year=y,AGResiduals=r,Type=t)

sdf <- AllData %>% group_by( Year ) %>% 
                   summarize( n=n(), ng=sum(Type=="Guarders") ) 

fmts <- rep("%s (n[G]==%d) ~~ (n[S]==%d)",nrow(sdf))
labs2 <- do.call(sprintf,list(fmts,sdf$Year,sdf$ng, sdf$n-sdf$ng ) )

ex2 <- parse(text=labs2)

ggplot(AllData, aes(Year, AGResiduals, fill=Type)) + 
  geom_boxplot(outlier.size=0) + 
  scale_fill_manual(values=c("skyblue4", "skyblue"),
                    name="Male Type", 
                    labels=c("Guarders","Sneakers")) + 
  labs(x=NULL, y = "Residual of Accessory Gland Mass x Total Mass") +
  scale_x_discrete(limits=c("2007","2008","2010","2011","2013","2014","2015"), 
                   labels=ex2) +
  theme(plot.title = element_text(size = rel(1.4)), 
        axis.title = element_text(size = rel(1.2)),
        axis.text.x = element_text(size = rel(1.0),angle=-30,hjust=0),
        axis.text.y = element_text(size = rel(1.5)), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"))

产生这样的结果: 输入图像描述

使用此代码 fmts <- rep("atop(%s,(n[G]==%d) ~ (n[S]==%d))",nrow(sdf)),您可以得到两行,但我无法得到第三行。如果将波浪号替换为逗号,则第三行会消失。此外,我必须调用 ggplot 两次才能绘制图形,否则会出现错误,但第二次就可以正常工作。非常挑剔。@baptiste 可能知道。 - rawr
好的,我会加进去。 - Mike Wise
作为第二个解决方案,我会将其添加进去,因为它确实是有效的。 - Mike Wise
非常感谢您的回复!这两个解决方案都很棒,最终我不确定会选择哪一个。如果在使用建议时遇到任何问题,我会告诉您的。谢谢! - Jessica Miller
请接受其中一个。点赞也会受到赞赏。它们需要很多工作。 - Mike Wise

2

Rawr提出了一个建议,可以让您获得两行,但不是三行。由于它不需要旋转,我将其输入为第二个解决方案。

这个:

library(stringr)
library(ggplot2)
library(scales)
library(dplyr)
set.seed(23456)
n <- 100
y <- as.character(sample(2007:2015,n,replace=T))
t <- sample(c("Guarders","Guarders","Sneakers"),n,replace=T)
r <- rnorm(n,10,20)
nsk <- sum(t=="Sneakers")
r[ t=="Sneakers" ] <- rnorm(nsk,1,5)
AllData <- data.frame(Year=y,AGResiduals=r,Type=t)

sdf <- AllData %>% group_by( Year ) %>% 
  summarize( n=n(), ng=sum(Type=="Guarders") ) 

 fmts <- rep("atop(%s, n[G]==%d ~~ n[S]==%d)",nrow(sdf)) # two rows
labs2 <- do.call(sprintf,list(fmts,sdf$Year,sdf$ng, sdf$n-sdf$ng ) )

ex2 <- parse(text=labs2)

ggplot(AllData, aes(Year, AGResiduals, fill=Type)) + 
  geom_boxplot(outlier.size=0) + 
  scale_fill_manual(values=c("skyblue4", "skyblue"),
                    name="Male Type", 
                    labels=c("Guarders","Sneakers")) + 
  labs(x=NULL, y = "Residual of Accessory Gland Mass x Total Mass") +
  scale_x_discrete(limits=c("2007","2008","2010","2011","2013","2014","2015"), 
                   labels=ex2) +
  theme(plot.title = element_text(size = rel(1.4)), 
        axis.title = element_text(size = rel(1.2)),
        axis.text.x = element_text(size = rel(1.0),angle=0,hjust=0),
        axis.text.y = element_text(size = rel(1.5)), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"))

这段文本的英译为:

生成以下内容:

enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接