ggplot:geom_point 和 facet_grid 的正确 x 轴排序

ggplot: Correct ordering of x-axis for a geom_point with facet_grid

我正在努力为散点图正确排序我的 x 轴,我希望离散 x 轴标签在第二个离散因子中按特定组的数字因子的增加大小排序。并且要用 facet_grid(或 facet_wrap 如果在这种情况下更好?)由第四个离散因子分隔。我希望这是有道理的?如果没有,希望我在下面的示例中解释后它会。

似乎有几个有用的在线帮助页面,我确定答案就在某处 - 但我似乎无法将其应用到我的案例中。

这是我的示例数据集...

Car = c("A","A","A","B","B","C","C","D","D","E","E","F","F","G","G","G","H","H","H","H","I","I","J","J","J","K","K","K","L","L","M","M","N","N","N","O","O","P","P","Q","Q","R","R","S","S","T","T","U","U","U","V","V","V","V","X","X","X")
Area = c("MMR","QRT","VF","QRT","VF","MMR","QRT","MMR","QRT","MMR","QRT","QRT","VF","MMR","QRT","VF","MMR","QRT","PP","VF","QRT","VF","QRT","PP","VF","MMR","QRT","VF","QRT","VF","QRT","VF","MMR","QRT","VF","QRT","VF","QRT","VF","QRT","VF","MMR","QRT","MMR","QRT","MMR","QRT","MMR","QRT","VF","MMR","QRT","PP","VF","MMR","QRT","VF")
Distance = c(100,0.0022,1320,0.002,1056,1030,0.025,62.1,0.06,80,0.011,7.2,100,671,91.677,165,0.61,0.1102,0.08,11.5,0.173,327,0.159,0.82,0.01902,10,0.0079,23,0.186,0.02235,0.038,0.022,100,0.016,0.01359,0.18,0.02291,0.00048,1000,0.007,8.21,1000,0.0349,100,0.0056,100,0.022,100,0.05,13,17.9,0.032,0.22,87,100,0.09,0.0251)
Country = c("UK","UK","UK","UK","UK","UK","UK","UK","UK","UK","UK","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","FR","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM")
df=data.frame(Car, Area, Distance, Country)
df

我希望绘制 X 轴为 'Car',Y 轴为 'Distance' 的绘图。我想使用 facet_grid 由 'Country' 分割的情节,并且在每个方面 Id 中,我希望通过增加 'Area' 因子中 'QRT' 的距离来排序 x 轴.

下面的代码是我想要的情节(x 轴排序问题除外)

Fig2B<- ggplot(df,aes(x=Car,y=Distance,colour=Area)) + 
  coord_trans(y = "log10") +
  geom_point() +
  facet_grid(. ~ Country, scales = "free", space="free")

我最接近重新订购的是通过以下有用的 post

使用下面的代码,我可以创建一个似乎正确排序的新因子。

#Remove grouping
ungroup(df) %>%
# 2. Arrange by
#   i.  facet group
#   ii. bar height
arrange(Country, Distance, Area) %>%
# 3. Add order column of row numbers
mutate(order = row_number())

但是我不知道如何将它带到下一阶段并使用文章中的代码在我的情节中使用它。我收到以下消息...

Don't know how to automatically pick scale for object of type function. Defaulting to continuous. Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 0, 57

我现在不确定从这里去哪里。

I can create a new factor that appears to order it correctly.

这是正确的目标。

I'd like the x-axis to be ordered by increasing distance of 'QRT' in the 'Area' factor

好的,所以我们需要这个顺序。

order = 
    ## filter down to just QRT
    filter(df, Area == "QRT") %>%
    ## get mean distance for each car (just in case there are
    ## multiple QRT values for a single car - more general than your example)   
    group_by(Car) %>%                   
    summarize(qrtdist = mean(Distance)) %>%
    ## sort ascending
    arrange(qrtdist) %>%
    ## make the Car column a character
    mutate(Car = as.character(Car))

所以这个新 order 数据集的 Car 列应该有正确的顺序。现在我们将此排序应用于原始数据,绘图将按预期工作:

df$Car = factor(df$Car, levels = order$Car)

ggplot(df,aes(x=Car,y=Distance,colour=Area)) + 
  coord_trans(y = "log10") +
  geom_point() +
  facet_grid(. ~ Country, scales = "free", space="free")

使用base

上面是花哨的 dplyr 方式,但在这种情况下我们实际上可以使用 base 简化很多。有一个命令 reorder() 用于通过某个其他变量的函数对因子的水平重新排序。

在这种情况下,我们想要 reorder df$Car 因子,使用 df$Distance 的值,其中 df$Area"QRT"

df$Car = reorder(
    # factor to reorder
    df$Car,  
    # vector that is Distance when Area is "QRT" and NA otherwise
    ifelse(df$Area == "QRT", df$Distance, NA),
    # function of that vector
    FUN = mean,
    # additional FUN argument: remove NA values
    na.rm = TRUE
)

没有所有评论,我们可以这样做:

df$Car = reorder(df$Car, ifelse(df$Area == "QRT", df$Distance, NA), mean, na.rm = TRUE)

ggplot(df,aes(x=Car,y=Distance,colour=Area)) + 
  coord_trans(y = "log10") +
  geom_point() +
  facet_grid(. ~ Country, scales = "free", space="free")