如何更改facet_grid ggplot的某些美感?

时间:2019-12-16 17:10:27

标签: r ggplot2 graph data-visualization facet-grid

我使用facet_gridpatchwork创建了该图,因为我需要为每个参数都定制一个次级y轴,并且它们都具有不同的比例。我成功地调整了大多数美学,使其与图表所需的功能相匹配,除了几个地方:

Plot

  1. 用“站点”匹配颜色。我想分别将红色,蓝色和绿色与Port,Bluff和Palm匹配。它不适用于我在scale_color_manual中使用的代码。
  2. 重命名带状文本。我之前尝试使用expression(paste()),但是它不起作用,尤其是希腊字母。我想在每行的右边分别添加这些提示文本:ETR [max],ɑ和E [k]。 []中的字母为下标。

感谢您提出任何建议。我没有足够的精力去尝试本周制作的内容,尤其是关于脱衣舞表演的文字。

我的数据框:data file

我的代码是:

abrv_mo <- with (params, month.abb[month]) params <- transform(params, month = abrv_mo) params <- params[order(match(params$month, month.abb)), ] params$month <- factor(params$month, month.abb, ordered = TRUE) params$month<- as.Date(ISOdate(2019, as.numeric(params$month), 15))

p1 <- ggplot() +   geom_hline(yintercept = 19.6, linetype = "dashed")
+   geom_line(data = tmpr2, 
            aes(month, tmp*0.98), 
            alpha = 0.4) +   geom_errorbar(data = subset(params, variable == "max"),
                aes(x= month, ymin = mean - se, ymax = mean +se, color = site),
                width = 8) +   geom_point(data = subset(params, variable == "max"), 
             aes(x=month, y=mean, color = site, group=site),
             size = 2.5) +   facet_grid(rows = vars(variable),
             cols = vars(site),
             switch = "y", scale = "free_y") +   scale_x_date(name = NULL, date_labels = "%b", 
               seq(as.Date("2019-01-15"), 
                   as.Date("2019-07-15"), by = "1 month")) + # ?strftime() for more options   scale_y_continuous(limits = c(5,40), breaks = seq(5, 40, by = 15),
                     expand = c(0,0), 
                     sec.axis = sec_axis(~./0.98)) +   scale_color_manual(name = "Site",
                    labels = c("Port", "Bluff", "Palm"),
                    values = c("#FC4E07","#00AFBB", "#C3D7A4")) +   theme_bw() +   theme(plot.background = element_blank(),
        strip.background = element_blank(),
        strip.placement = "outside",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_rect(size=1, colour = "black"),
        panel.spacing = unit(0.3, "lines"),
        axis.line = element_line(size=0.1, colour = "black"),
        axis.ticks.y = element_line(size=0.5, colour = "black"),
        axis.text.x = element_blank(),
        axis.text.y = element_text(size=10, color="black", margin = margin(t = 0.5, l = 0.5)),
        text = element_text(size = 18),
        legend.position="none",
        plot.margin=margin(l = -1, unit = "cm")) +   ylab(NULL)  

p2 <- ggplot() +   geom_hline(yintercept = 0.16, linetype = "dashed")
+   geom_line(data = tmpr2, 
            aes(month, tmp*0.008),
            alpha = 0.4) +   geom_errorbar(data = subset(params, variable=="slope"),
                aes(x= month, ymin = mean - se, ymax = mean +se, color = site),
                width = 8) +   geom_point(data = subset(params, variable == "slope"), 
             aes(x=month, y=mean, color=site, group=site),
             size = 2.5) +   facet_grid(rows = vars(variable),
             cols = vars(site),
             switch = "y",
             scale = "free_y") +   scale_x_date(name = NULL, date_labels = "%b", 
               seq(as.Date("2019-01-15"), 
                   as.Date("2019-07-15"), by = "1 month")) + # ?strftime() for more options   scale_y_continuous(breaks = seq(0.15,
0.26, by = 0.05),
                     expand = c(0,0), 
                     limits = c(0.15,0.26),
                     sec.axis = sec_axis(~./0.008, name = "Temperature (°C)")) +   scale_color_manual(name = "Site",
                     labels = c("Port", "Bluff", "Palm"),
                     values = c("#FC4E07","#00AFBB", "#C3D7A4")) +   theme_bw() +   theme(plot.background = element_blank(),
        strip.background = element_blank(),
        strip.placement = "outside",
        strip.text.x = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_rect(size=1, colour = "black"),
        panel.spacing = unit(0.3, "lines"),
        axis.line = element_line(size=0.1, colour = "black"),
        axis.ticks.y = element_line(size=0.5, colour = "black"),
        axis.text.x = element_blank(),
        axis.text.y = element_text(size=10, color="black", margin = margin(t = 0.5, l = 0.5)),
        axis.text.y.right = element_text(size=10, color="black", margin = margin(t = 0.5, r = 10)),
        text = element_text(size = 18),
        legend.position="none",
        plot.margin=margin(l = -1.5, unit = "cm")) +   ylab(NULL)  

p3 <- ggplot() +   geom_hline(yintercept = 140, linetype = "dashed") + geom_line(data = tmpr2, 
            aes(month, tmp*7),
            alpha = 0.4) +   geom_errorbar(data = subset(params, variable=="ek"),
                aes(x= month, ymin = mean - se, ymax = mean +se, color = site),
                width = 8) +   geom_point(data = subset(params, variable=="ek"), 
             aes(x=month, y=mean, color=site, group=site),
             size = 2.5) +   facet_grid(rows = vars(variable),
             cols = vars(site),
             switch = "y",
             scale = "free_y") +   scale_x_date(name = NULL, date_labels = "%b", 
               seq(as.Date("2019-01-15"), 
                   as.Date("2019-07-15"), by = "1 month")) + # ?strftime() for more options   scale_y_continuous(expand = c(0,0), 
                     breaks = seq(25, 250, by = 100),
                     limits = c(25,250),
                     sec.axis = sec_axis(~./7)) +   scale_color_manual(name = "Site",
                     labels = c("Port", "Bluff", "Palm"),
                     values = c("#FC4E07","#00AFBB", "#C3D7A4")) +   theme_bw() +   theme(plot.background = element_blank(),
        strip.background = element_blank(),
        strip.placement = "outside",
        strip.text.x = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_rect(size=1, colour = "black"),
        panel.spacing = unit(0.3, "lines"),
        axis.line = element_line(size=0.1, colour = "black"),
        axis.ticks.y = element_line(size=0.5, colour = "black"),
        axis.text.x = element_text(angle = 45,size=10, color="black", hjust = 1,
                                   margin = margin(t = 0.5, r = 0.5)),
        axis.text.y = element_text(size=10, color="black", margin = margin(t = 0.5, l = 0.5)),
        text = element_text(size = 18),
        legend.position="none",
        plot.margin=margin(l = -1.5, unit = "cm")) +   ylab(NULL) 

library(patchwork) 
p1 + p2 + p3 +   plot_layout(ncol = 1)

0 个答案:

没有答案