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


日耀九洲
2025-03-07 03:14:55 (1月前)
  1. 我试图在一个盒子和抖动图上进一步分开(水平)图例文本和图例框。复杂因素是我用来使箱线图水平的coord_flip。在主题我......

2 条回复
  1. 0# 听风~ | 2019-08-31 10-32



    使用其中之一

    stringr::str_pad()

    要么

    theme(legend.spacing.x = …)

    或两者




    1. 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”),

    2.                    ### added
    3.                    labels = stringr::str_pad(c("Drug", "Placebo"), 10, "right")) +
    4. scale_y_continuous(breaks = seq(0,84,14), minor_breaks = seq(0, 84, 14)) + # changes minor break line
    5. coord_flip() +
    6. labs(y = "Score") +
    7. geom_hline(yintercept = c(groupDF$m), linetype = "dotted") +
    8. geom_segment(x = 2.38, xend = 2.38, y = groupDF$m[2] + .1, yend = groupDF$m[1] - .1, size = .7, 
    9.              arrow = arrow(end = "both", type = "open", length = unit(0.15, "cm")), colour = "#696969") +
    10. annotate("text", x = 2.46, y = mean(groupDF$m), label = paste0("italic(p) == ", 0.02), parse = T) +
    11. theme_bw() +
    12. theme(axis.title.y = element_blank(),
    13.       axis.ticks.y = element_blank(),
    14.       axis.text.y = element_blank(),
    15.       axis.text.x = element_text(size = 13),
    16.       axis.title.x = element_text(size = 13, face = "bold", 
    17.                                   margin = margin(t = 0, r = 0, b = 10, l = 0), vjust = -2), 
    18.       legend.title = element_blank(),
    19.       legend.position = "top",
    20.       ### added
    21.       legend.spacing.x = unit(0.25, 'cm'),
    22.       legend.spacing.y = unit(.1, "cm"),
    23.       legend.box.spacing = unit(.1, "cm"), # adjusts distance of box from x-axis
    24.       legend.key.size = unit(1, "cm"),
    25.       legend.text = element_text(size = 13, face = "bold"),
    26.       strip.text = element_text(size = 13, face = "bold"),
    27.       panel.grid.major.y = element_blank(),
    28.       panel.grid.major.x = element_line(size=.4, color="#F7F7F7"))
    29. </code>






    创建于2019-03-11由

    代表包

    (v0.2.1.9000)


登录 后才能参与评论