ggplot:使用facet_grid

时间:2017-03-01 10:30:45

标签: r ggplot2 scatter-plot facet factors

我正在努力为散点图正确地命令我的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

我希望有一个情节,我有' Car'在x轴和距离'在Y轴上。我想用“国家/地区”分割的情节。使用facet_grid并在每个方面Id中像x轴一样通过增加' QRT'在'区域'因素。

以下代码是我的目标(x轴排序问题除外)

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

Plot without correct x axis order

我最接近重新排序的是通过以下有用的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())

但是我无法弄清楚如何将它带到下一个阶段并使用文章中的代码在我的情节中使用它。我收到以下消息......

  

不知道如何自动选择类型函数对象的比例。违约持续。   错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,:参数意味着行数不同:0,57

我现在不知道从哪里开始。

1 个答案:

答案 0 :(得分:1)

  

我可以创建一个看似正确排序的新因素。

这是正确的目标。

  

我希望通过增加QRT&#39;的距离来排序x轴。在&#39;区域&#39;因子

好的,我们需要这个订购。

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))

因此,此新Car数据集的order列应具有正确的排序。现在我们将这个排序应用于原始数据,并且绘图将按照需要工作:

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"的值。{/ p>

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")