R 新手并尝试改善分组箱线图的外观

R novice and trying to improve the appearance of grouped box plots

我找到了一些在线数据集并设法制作了一些复杂的箱线图,这些箱线图具有我正在寻找的大部分功能。我很感激社区帮助使这些情节看起来更好,例如:

Veusz 程序允许您将晶须模式更改为(例如 I.5 IQR、9/91 百分位数、1 stddev),最好也有该选项。我也不明白为什么我的第一个箱形图中的数据点(链接如下)偏离中心。

下面的链接是我根据自己的数据制作的一些分组箱线图的屏幕截图。我通过破坏和修复东西学习得最好,如果有人有时间写出具有很多特征的箱形图的代码,我会解构它以查看每个部分的作用并在线搜索代码以获得更好的理解它是如何工作的。

  1. Box plot of my data 1
  2. Box plot of my data 2
  3. Box plot of my data 3
    structure(list(X. = structure(c(1L, 12L, 23L, 34L, 45L, 56L, 
    67L, 71L, 72L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 
    14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L, 27L, 
    28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 
    42L, 43L, 44L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 
    57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 68L, 69L, 70L
    ), .Label = c("# 1", "# 10", "# 11", "# 12", "# 13", "# 14", 
    "# 15", "# 16", "# 17", "# 18", "# 19", "# 2", "# 20", "# 21", 
    "# 22", "# 23", "# 24", "# 25", "# 26", "# 27", "# 28", "# 29", 
    "# 3", "# 30", "# 31", "# 32", "# 33", "# 34", "# 35", "# 36", 
    "# 37", "# 38", "# 39", "# 4", "# 40", "# 41", "# 42", "# 43", 
    "# 44", "# 45", "# 46", "# 47", "# 48", "# 49", "# 5", "# 50", 
    "# 51", "# 52", "# 53", "# 54", "# 55", "# 56", "# 57", "# 58", 
    "# 59", "# 6", "# 60", "# 61", "# 62", "# 63", "# 64", "# 65", 
    "# 66", "# 67", "# 68", "# 69", "# 7", "# 70", "# 71", "# 72", 
    "# 8", "# 9"), class = "factor"), Label = structure(c(1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Sample 1", "Sample 2", "Sample 3"
    ), class = "factor"), Rescan = structure(c(1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 4L, 4L), .Label = c("Rescan 0", "Rescan 1", "Rescan 2", "Rescan 3"
    ), class = "factor"), Porosity = c(15.19, 15.72, 15.83, 15.57, 
    15.11, 14.15, 17.24, 17.53, 18.21, 18.8, 18.32, 19.59, 17.4, 
    17.98, 19.33, 18.94, 18.32, 18.17, 19.67, 20.55, 19.04, 18.18, 
    19.59, 18.19, 18.97, 18.64, 18.83, 17.24, 18.09, 17.74, 22.28, 
    22.29, 21.35, 21.96, 23.12, 22.9, 22.9, 21.06, 23.34, 22.82, 
    21.42, 20.48, 21.22, 22.75, 21.62, 22.24, 24.28, 20.48, 14.79, 
    13.69, 13.4, 14.46, 14.13, 13.55, 20.67, 19.81, 21.2, 20.77, 
    22.29, 21.94, 19.49, 19.29, 19.43, 20.31, 21.77, 19.39, 22.37, 
    21.46, 21.86, 21.58, 21.82, 23.02)), class = "data.frame", row.names = c(NA, 
    -72L))

希望这能回答一些问题,但老实说,很难找出您提出的个别问题。也许把它们放在点数中?

整体造型:

有一些 pre-made 主题可以尝试,我喜欢:

  1. theme_void() - 删除大部分内容
  2. theme_classic() - 让背景看起来更漂亮
  3. theme_minimal() - 移除外边框并使背景更漂亮

您可以在绘图代码末尾使用 + theme_void() 将它们添加为图层。

对于所有其他特定定制,请查看 ?theme(),因为您可以做很多事情。

标签:

要删除标题 'label' 添加 labs(legend = '') 作为图层。您还可以使用它来修改 x、y、标题和标题文本。

如果您想完全删除图例,可以在 geom_jitter() 层中添加 show.legend = F。 (例如 geom_jitter(show.legend = F))这意味着它显示在图表上,但没有出现在图例中。

方面:

要更改 facte_wrap 中的背景颜色,请使用 theme(strip.background = element_rect(color = 'desired_colour'))

要更改 facte_wrap 中文本的颜色,请使用 theme(strip.text = element_text(color = 'desired_colour'))

轴线:

添加theme(axis.line = element_blank())

分:

由于 geom_jitter(),您的点偏离了中心。请尝试 geom_point()

这是你的情节示例,其中包含你想做的事情。我想您可以从这里根据您的需要调整代码:

ggplot(mydata, aes(Label,Porosity,fill=Label))+
  geom_boxplot()+

  # shift strips down
  facet_wrap(~Rescan, strip.position = "bottom")+
  # add exact points
  geom_point(alpha=0.2)+
  # add your preferred colors here (hexcode also works fine)
  scale_fill_manual(values=c("red","blue","green"))+
  # appearance
  theme_classic()+
  # legend options
  theme(legend.title      = element_blank(),
        legend.text       = element_text(color ="black", size = 8),
        legend.position   = "top", # "bottom" or "right"
        legend.key.size   = unit(1, "cm"),
        legend.spacing.x  = unit(5, "mm"),
        legend.direction  = "horizontal", # or "vertical"
        legend.background = element_blank())+
  # reverse legend keys
  guides(fill = guide_legend(reverse = F))+ # set T to take action
  # scaling y-axis
  scale_y_continuous(expand = c(0, 0), limits = c(0,max(mydata$Porosity)),breaks = seq(0,max(mydata$Porosity,10)))+
  # paramaters of the axes
  theme(
    axis.text = element_text(color = "black", angle = 0, hjust = 0.5, vjust = 0.5, size = 8),
    # axis.title  = element_blank(), # activate for no axis titles
    axis.line = element_line(color = "black", size = 0.5), # use element_blank() for no lines
    axis.ticks.length=unit(-0.1, "cm"), # negative values turn them inside
    plot.background = element_blank(),
    text = element_text(family = "Arial"),
    strip.background = element_blank(),
    strip.placement = "outside")+ # or "inside"
  # name your axes
  ylab("your y lab")+
  xlab("your x lab")+
  # add x-axis in each facet
  annotate("segment", y=0,yend=0,x=0,xend=Inf)