ggplot2 交错轴标签
ggplot2 stagger axis labels
我正在制作一个 ggplot。 x轴是因子,标签很长。
我无法缩短标签,它们尽可能短。
我有兴趣使标签垂直偏移。我的偏好是让每个奇数标签都在高度 0 处,每个偶数标签在距离 x 轴更远 2 个单位的高度处。
我看过这里,ggplot-hopeful-help,但很难理解发生了什么,所以无法制作一个有用的版本。
有什么想法吗??
(下面的示例代码...我不太擅长这里的代码格式化,看来...对不起。)
library("ggplot2");
stack <- data.frame(value =rnorm(n = 1000, sd = 2, mean=34)); stack$fact <- as.factor(rep(1:5, each=1000/5));
ggplot(stack, aes(x=fact, y=value)) + geom_boxplot(aes(fill=fact))+ scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=c("hi","don't suggest I shorten the text","I need long labels", "This is a long factor label","This label is very long"))
不管你懂不懂,好像都挺好用的。在你的问题中调用情节 your_plot
:
your_plot + theme(axis.text.x = element_text(vjust = grid::unit(c(-2, 0, 2), "points")))
在 theme()
中指定首选项是调整 ggplot 陷阱的方法。 axis.text.x
仅修改 x-axis 文本,该文本是使用 element_text()
设置的首选项创建的。您可以在 element_text()
中指定字体大小、字体系列、旋转角度等。 vjust
代表 "vertical justification",因此将 vjust
设置为三个值,-2、0 和 2 会将这些值应用于连续的 x-axis 标签。 (显然是负面的,这让我感到惊讶。)
使用 grid::unit()
允许我们指定文本垂直移动的单位(在本例中为点)。查看 ?grid::units
表明您可以使用英寸、厘米或其他几个单位。
唯一的问题是与 x 轴标题重叠。我认为解决这个问题的最简单方法是在它之前添加几个换行符 "\n"
:
your_plot +
theme(axis.text.x = element_text(vjust = grid::unit(c(-2, 0, 2), "points"))) +
labs(x = "\n\nfact")
另一个解决方案是旋转文本:
your_plot + theme(axis.text.x = element_text(angle = -90, hjust = 0, vjust = 0))
stack <- data.frame(value =rnorm(n = 1000, sd = 2, mean=34))
stack$fact <- as.factor(rep(1:5, each=1000/5))
ggplot(stack, aes(x=fact, y=value)) +
geom_boxplot(aes(fill=fact)) +
scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=c("hi","don't suggest I shorten the text","I need long labels", "This is a long factor label","This label is very long"))+
theme(axis.text.x = element_text(vjust = grid::unit(c(0, 2), "points"))) +
theme(axis.title.x = element_text(vjust = -0.6))
实际上the link you reported中讨论的解决方案只需要稍作修改。
我还添加了
theme(axis.title.x = element_text(vjust = -0.6))
降低 x 轴标签以防止重叠。
我正在制作一个 ggplot。 x轴是因子,标签很长。
我无法缩短标签,它们尽可能短。
我有兴趣使标签垂直偏移。我的偏好是让每个奇数标签都在高度 0 处,每个偶数标签在距离 x 轴更远 2 个单位的高度处。
我看过这里,ggplot-hopeful-help,但很难理解发生了什么,所以无法制作一个有用的版本。
有什么想法吗??
(下面的示例代码...我不太擅长这里的代码格式化,看来...对不起。)
library("ggplot2");
stack <- data.frame(value =rnorm(n = 1000, sd = 2, mean=34)); stack$fact <- as.factor(rep(1:5, each=1000/5));
ggplot(stack, aes(x=fact, y=value)) + geom_boxplot(aes(fill=fact))+ scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=c("hi","don't suggest I shorten the text","I need long labels", "This is a long factor label","This label is very long"))
不管你懂不懂,好像都挺好用的。在你的问题中调用情节 your_plot
:
your_plot + theme(axis.text.x = element_text(vjust = grid::unit(c(-2, 0, 2), "points")))
在 theme()
中指定首选项是调整 ggplot 陷阱的方法。 axis.text.x
仅修改 x-axis 文本,该文本是使用 element_text()
设置的首选项创建的。您可以在 element_text()
中指定字体大小、字体系列、旋转角度等。 vjust
代表 "vertical justification",因此将 vjust
设置为三个值,-2、0 和 2 会将这些值应用于连续的 x-axis 标签。 (显然是负面的,这让我感到惊讶。)
使用 grid::unit()
允许我们指定文本垂直移动的单位(在本例中为点)。查看 ?grid::units
表明您可以使用英寸、厘米或其他几个单位。
唯一的问题是与 x 轴标题重叠。我认为解决这个问题的最简单方法是在它之前添加几个换行符 "\n"
:
your_plot +
theme(axis.text.x = element_text(vjust = grid::unit(c(-2, 0, 2), "points"))) +
labs(x = "\n\nfact")
另一个解决方案是旋转文本:
your_plot + theme(axis.text.x = element_text(angle = -90, hjust = 0, vjust = 0))
stack <- data.frame(value =rnorm(n = 1000, sd = 2, mean=34))
stack$fact <- as.factor(rep(1:5, each=1000/5))
ggplot(stack, aes(x=fact, y=value)) +
geom_boxplot(aes(fill=fact)) +
scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=c("hi","don't suggest I shorten the text","I need long labels", "This is a long factor label","This label is very long"))+
theme(axis.text.x = element_text(vjust = grid::unit(c(0, 2), "points"))) +
theme(axis.title.x = element_text(vjust = -0.6))
实际上the link you reported中讨论的解决方案只需要稍作修改。 我还添加了
theme(axis.title.x = element_text(vjust = -0.6))
降低 x 轴标签以防止重叠。