我在ggplot2中创建了一个情节,并且想知道是否有任何方法可以将x轴LABELS放置在绘图内部,因为它会整理它很多而我无法解决这个命令会在哪里。下面的代码和图,任何帮助非常感谢!谢谢!
ggplot(gen, aes(Genera, y = Depth.occurred)) +
geom_boxplot() +
scale_x_discrete(position = "top") +
labs(x = "Genera", y = "Depth (m)") +
scale_y_reverse(breaks = seq(0, 90, by = 10)) +
theme_bw() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(axis.title.y = element_text(face="bold", size = 12)) +
theme(axis.title.x = element_text(face="bold", size = 12)) +
geom_hline(yintercept=30, linetype="dashed", color = "red") +
theme(axis.ticks.x=element_blank()) +
stat_n_text(size = 3, y.pos = 1, fontface = "italic") +
theme(panel.border = element_blank()) +
theme(axis.line = element_line(colour = "black")) +
theme(axis.text.x = element_text(size = 9, vjust = 1))
在这里创建的情节: plot
使用下面的第一种方法(y位置编辑)更新了绘图,但在某些标签updated plot上有奇怪的字体
答案 0 :(得分:0)
我不知道在常规ggplot表达式中设置轴标签的方法,因为我没有看到主题上的选项,但你可以只是抑制x轴文本标签,并将x轴标签添加为geom_text积
# what you have so far minus theme(axis.text.x), and combining theme elements
gg <- ggplot(gen, aes(Genera, y = Depth.occurred)) +
geom_boxplot() +
geom_hline(yintercept=30, linetype="dashed", color = "red") +
stat_n_text(size = 3, y.pos = 1, fontface = "italic") +
labs(x = "Genera", y = "Depth (m)") +
scale_x_discrete(position = "top") +
scale_y_reverse(breaks = seq(0, 90, by = 10)) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.title.y = element_text(face="bold", size = 12),
axis.title.x = element_text(face="bold", size = 12),
axis.ticks.x=element_blank(),
axis.line = element_line(colour = "black"))
gg + theme(axis.text.x = element_blank()) +
geom_text(aes(x=Genera, y=2, label=Genera))
或者你可以获得你的情节ylims并使用一些微调因子将geom_text y值设置为y max,因为它看起来你的n也打印在y.pos = 1
# y-range
gg_yrange <- ggplot_build(gg)$layout$panel_ranges[[1]]$y.range
gg + theme(axis.text.x = element_blank()) +
geom_text(aes(x=Genera, y=p_yrange[1]-2, label=Genera))