按均值对 x 轴上的箱线图进行排序

Ordering box plots on x axis by mean

我正在尝试使用 ggplot2 在 R 中绘制箱线图。

这是我的示例数据代码:

df = structure(list(Closeness = c(0.0919540229885057, 0.0950259836674091, 0.0957367240089753, 0.0960240060015004, 0.0901408450704225, 0.0970432145564822, 0.0939794419970631, 0.0943952802359882, 0.0921526277897768, 0.0914285714285714, 0.0933625091174325, 0.0953090096798213, 0.0917562724014337, 0.0960960960960961, 0.0937728937728938, 0.0909090909090909, NA, 0.0946045824094605, 0.0864280891289669, 0.0879120879120879, 0.0905233380480905, 0.100313479623824, 0.0993017843289372, 0.0942562592047128, 0.0950965824665676, 0.0907801418439716, NA, NA, 0.0950965824665676, 0.0913633119200571, NA, 0.0926864590876177, NA, 0.0948148148148148, 0.0958801498127341, 0.0945347119645495, 0.0931586608442504, 0.090014064697609, 0.0968229954614221, 0.0963855421686747, 0.0926193921852388, 0.0919540229885057, 0.0947446336047372, 0.0917562724014337, 0.0905874026893135, 0.0950965824665676, NA, 0.0926193921852388, 0.0900774102744546, 0.0977845683728037), Var1 = c("Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group", "Group"), Var2 = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "A", "A", "K", "K", "G", "G", "N", "N", "O", "O", "A", "P", "P", "P", "Q", "Q", "Q", "Q", "A", "A", "A", "A", "R", "R", "R", "R", "S", "S", "S", "S", "L", "L", "L", "L", "L", "L", "L")), .Names = c("Closeness", "Var1", "Var2"), row.names = c(NA, 50L), class = "data.frame")

tmp <- data.frame(df, check.names=T)
tmp <- melt(tmp, id="Closeness", variable.name="Var1", value.name="Var2")
tmp$Var1 <- gsub("(.*)\.[0-9]", "\1", tmp$Var1)
df <- subset(tmp, Var2!="")

df_g = subset(df, Var1=="Group")
df_c = subset(df, Var1=="Cat")

ggplot(df_c, aes(x = df_g$Var2, y = df_g$Closeness), position = "dodge") + # geom_point() +
geom_boxplot(outlier.size = 1.5) #+ geom_jitter(position=position_jitter(width=.2, height=0))

产生这个(具有完整数据集):

现在,我有两个问题:

  1. 我希望类别(A、B、C、D)按均值降序排列;
  2. 有些类别只有一个样本(即 B、D 和 E)。我想在绘图之前删除它们。

这可以使用 ggplot2 吗?如果可以,如何进行?

通常我会评论并关闭,例如

  • How do you specifically order ggplot axis?,
  • Order barchart in R,
  • How to change the order of a discrete x scale in ggplot?,
  • Order bars in ggplot2 bargraph,
  • sorting - R ggplot ordering bars

或者在 Stack Overflow 中搜索 "ggplot2 order" 时出现的任何内容。如果你想要箱线图的具体例子(方法是一样的),见

  • Ordering x in ggplot2 boxplot using computed statistic,
  • How to boxplot factors and order one of the factors according to a continuous variable in ggplot2?
  • .

甚至 您在不到 2 周前提出的问题。不同geom,原理相同。

但是,您还有一些其他问题,其中之一是在 aes() 中使用 data$column,这让我有点恼火,所以让我们也解决这个问题。

不要在 aes()! 中使用 data$column 这意味着您没有正确使用数据参数。相关:完全不清楚为什么你用空数据框 df_c 开始绘图,而 df_g 拥有你需要的一切:

ggplot(df_g, aes(x = Var2, y = Closeness), position = "dodge") + 
    geom_boxplot(outlier.size = 1.5) 

正确使用 data 参数并且 aes() 中指定 data$column 将确保您的绘图在所有情况下都能正常工作。如果您在 aes() 中使用 $,则分面和其他复杂功能可能无法使用。如果您需要在一个图中使用多个数据框,请在图层级别进行(例如,geom_point(data = other_data, aes(x = x_var, y = y_var)))。你仍然不需要在aes().

中使用$

至于你说的两个问题,都是通过编辑你的数据解决的。 ggplot非常擅长绘制数据,你只需要让你的数据看起来像你想要绘制的那样。

I'd like the categories (A, B, C, D) to be ordered by descending mean;

订购数据中的因素!

df_g$Var2 = with(df_g, reorder(x = Var2, X = Closeness, FUN = function(x) -mean(x, na.rm = TRUE)))

Some categories only have one sample (i.e. B, D, and E). I'd like to remove them before plotting.

好的,删除它们!您可以将它们从您的数据中完全删除,或者只是将您提供给绘图的数据子集化:

more_than_one = levels(df_g$Var2)[table(df_g$Var2) > 1]

ggplot(subset(df_g, Var2 %in% more_than_one), aes(Var2, Closeness)) +
    geom_boxplot()