在ggplot中的coord_flip()之后调整图例文本和框之间的空间

时间:2019-03-12 01:23:50

标签: r ggplot2

我正在尝试将图例文本和图例框在框和抖动图中移开(水平)。复杂的因素是我用来使箱线图水平的coord_flip。在theme中,我尝试同时使用legend.spacing.xlegend.spacing.y,但都没有影响图例文本和图例框之间的距离。

这是带有伪数据的图形。我知道比需要的还要复杂,但是我需要能够使其适应所有复杂情况。

library(dplyr)
library(ggplot2)
set.seed(01234)

# make some data
totDays <- data.frame(id = 1:80,
                      group = rep(c("Placebo", "Drug"), each = 40),
                      total84 = c(pmin(abs(round(rnorm(40, 55, 30))),84), pmin(abs(round(rnorm(40, 38, 30))),84)))


# get some descriptives
(groupDF <- totDays %>% group_by(group) %>%
                        dplyr::summarise(m = mean(total84, na.rm = T),
                                         sd = sd(total84, na.rm = T),
                                         count = n()) %>%
                        mutate(se = sd/sqrt(count)))

# now for the box and scatter plot
(g <- ggplot(totDays, aes(group, total84, colour = group)) +
              geom_jitter(size = 1, width = 0.1) + # so points aren't overlaid, width controls how much jitter
              geom_point(stat = "summary", fun.y = "mean", shape = 3, size = 3, colour = "black") + # crosses for mean
              geom_boxplot(alpha = 0, width = 0.5, lwd = 1, size = 0.5) +
              scale_color_manual(values = c("#00AFBB", "#E7B800")) +
              scale_y_continuous(breaks = seq(0,84,14), minor_breaks = seq(0, 84, 14)) + # changes minor break line
              coord_flip() +
              labs(y = "Score") +
              geom_hline(yintercept = c(groupDF$m), linetype = "dotted") +
              geom_segment(x = 2.38, xend = 2.38, y = groupDF$m[2] + .1, yend = groupDF$m[1] - .1, size = .7, arrow = arrow(end = "both", type = "open", length = unit(0.15, "cm")), colour = "#696969") +
              annotate("text", x = 2.46, y = mean(groupDF$m), label = paste0("italic(p) == ", 0.02), parse = T) +
              theme_bw() +
              theme(axis.title.y = element_blank(),
                    axis.ticks.y = element_blank(),
                    axis.text.y = element_blank(),
                    axis.text.x = element_text(size = 13),
                    axis.title.x = element_text(size = 13, face = "bold", margin = margin(t = 0, r = 0, b = 10, l = 0), vjust = -2), # note the use of margin to move the title away from the axis text
                    legend.title = element_blank(),
                    legend.position = "top",
                    legend.spacing.y = unit(.1, "cm"),
                    legend.box.spacing = unit(.1, "cm"), # adjusts distance of box from x-axis
                    legend.key.size = unit(1, "cm"),
                    legend.text = element_text(size = 13, face = "bold"),
                    strip.text = element_text(size = 13, face = "bold"),
                    panel.grid.major.y = element_blank(),
                    panel.grid.major.x = element_line(size=.4, color="#F7F7F7")))

enter image description here

1 个答案:

答案 0 :(得分:1)

使用stringr::str_pad()theme(legend.spacing.x = ...)或同时使用

g <- ggplot(totDays, aes(group, total84, colour = group)) +
    geom_jitter(size = 1, width = 0.1) + # so points aren't overlaid, width controls how much jitter
    geom_point(stat = "summary", fun.y = "mean", shape = 3, size = 3, colour = "black") + # crosses for mean
    geom_boxplot(alpha = 0, width = 0.5, lwd = 1, size = 0.5) +
    scale_color_manual(values = c("#00AFBB", "#E7B800"),

                       ### added
                       labels = stringr::str_pad(c("Drug", "Placebo"), 10, "right")) +

    scale_y_continuous(breaks = seq(0,84,14), minor_breaks = seq(0, 84, 14)) + # changes minor break line
    coord_flip() +
    labs(y = "Score") +
    geom_hline(yintercept = c(groupDF$m), linetype = "dotted") +
    geom_segment(x = 2.38, xend = 2.38, y = groupDF$m[2] + .1, yend = groupDF$m[1] - .1, size = .7, 
                 arrow = arrow(end = "both", type = "open", length = unit(0.15, "cm")), colour = "#696969") +
    annotate("text", x = 2.46, y = mean(groupDF$m), label = paste0("italic(p) == ", 0.02), parse = T) +
    theme_bw() +
    theme(axis.title.y = element_blank(),
          axis.ticks.y = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_text(size = 13),
          axis.title.x = element_text(size = 13, face = "bold", 
                                      margin = margin(t = 0, r = 0, b = 10, l = 0), vjust = -2), 
          legend.title = element_blank(),
          legend.position = "top",

          ### added
          legend.spacing.x = unit(0.25, 'cm'),

          legend.spacing.y = unit(.1, "cm"),
          legend.box.spacing = unit(.1, "cm"), # adjusts distance of box from x-axis
          legend.key.size = unit(1, "cm"),
          legend.text = element_text(size = 13, face = "bold"),
          strip.text = element_text(size = 13, face = "bold"),
          panel.grid.major.y = element_blank(),
          panel.grid.major.x = element_line(size=.4, color="#F7F7F7"))

reprex package(v0.2.1.9000)于2019-03-11创建