繁体   English   中英

如何在ggplot2中调整facet strip的标签

[英]How to adjust labels of facet strip in ggplot2

我尝试了一个新的包ggthemr,但我遇到了两个问题。

数据集是:

tma<-data.frame(
invasion=c(0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1),
sediment=c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1),
nitrogen=c( 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1),
type=c("Evenness index",
"Evenness index",
"Evenness index",
"Evenness index",
"Total mass(g)",
"Total mass(g)",
"Total mass(g)",
"Total mass(g)",
"Evenness index",
"Evenness index",
"Evenness index",
"Evenness index",
"Total mass(g)",
"Total mass(g)",
"Total mass(g)",
"Total mass(g)"),
mean=c(0.76109,
0.63923,
0.78138,
0.73626,
59.0425,
56.15383,
50.39167,
44.9215,
0.60109,
0.55402,
0.81587,
0.74957,
62.207,
53.21033,
49.99517,
42.38767),
standard.error=c(0.03638,
0.06232,
0.045,
0.03912,
5.87524,
5.87524,
5.87524,
5.87524,
0.05093,
0.04807,
0.02694,
0.04843,
5.87524,
5.87524,
5.87524,
5.87524)
)

tma$nitrogen <- as.factor(tma$nitrogen)
tma$sediment <- as.factor(tma$sediment)

代码是:

library(tidyverse)
library(ggthemr)

ggthemr('light', layout = 'scientific',
    spacing = 1, type = 'inner', line_weight = 0.6,
    )

ggplot(tma, aes(invasion, mean, color = sediment)) +
geom_ribbon(
    aes(ymin = mean - standard.error, ymax = mean + standard.error,group=sediment),
    alpha = 0.2,
    color = NA
    ) +
geom_line(size = 1) +
geom_point(show.legend = TRUE) +
facet_grid(type ~ nitrogen, scales = "free_y") +
scale_x_discrete(breaks = NULL, expand = c(0, 0.10)) +
scale_y_continuous(sec.axis = dup_axis(name=NULL))+
theme(legend.position = "bottom") +
theme(panel.spacing.x = unit(0.1, "lines"), panel.spacing.y = unit(0.1, "lines")) +
theme(strip.text.x = element_text(size = 10),
    strip.text.y = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.title.x = element_text(size = 14),
    ) +
xlab("From not invaded to invaded") +
ylab("Mean with standard error")

输出是这样的:

在此输入图像描述

我预计复制的y轴位于图的右侧,并且在小平面条的左侧,但它实际上位于小平面条的右侧。

另一个问题是小面条不能完全显示,例如,总质量(g)的标题。

出了什么问题? 我不知道应该调整哪些参数。 非常感谢。

library(devtools)    
# devtools::install_github('cttobin/ggthemr') # need this to install ggthemr
library(ggthemr)

ggthemr('light', layout = 'scientific',
        spacing = 1, type = 'inner', line_weight = 0.6
)

ggplot(tma, aes(invasion, mean, color = sediment)) +
  geom_ribbon(
    aes(ymin = mean - standard.error, 
        ymax = mean + standard.error,group = sediment), 
    alpha = 0.2, color = NA) +
  geom_line(size = 1) +
  geom_point(show.legend = TRUE) +
  facet_grid(type ~ nitrogen, scales = "free_y") +
  scale_x_discrete(breaks = NULL, expand = c(0, 0.10)) +
  scale_y_continuous(sec.axis = dup_axis(name=NULL))+
  theme(legend.position = "bottom",
        panel.spacing.x = unit(0.1, "lines"), 
        panel.spacing.y = unit(0.1, "lines"),
        strip.text.x = element_text(size = 10),
        strip.text.y = element_text(size = 14, margin = margin(0,0,0,5)),
        strip.placement = "outside",
        axis.title.y = element_text(size = 14),
        axis.title.x = element_text(size = 14)) +
  xlab("From not invaded to invaded") +
  ylab("Mean with standard error")

笔记:

  1. 为了修复你在“y轴左侧”的“刻面标签”问题,我添加了theme(strip.placement = "outside") 这将条带放在轴的另一侧。
  2. 为了解决“标签未完全显示”的问题,我在strip.text.y element_text中添加了margin = margin(0,0,0,5)参数。 这可以控制条带文本的边距。 首先,调整哪个数字似乎令人困惑,但是一个快速的?element_text告诉我顺序是:t = top,r = right,b = bottom,l = left(想想“ tr ou bl e”)。 因此,最后一个数字控制条带文本的左边距。
  3. 我还格式化了你的代码,使它更清洁。 例如,结合所有theme

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM