使用ggplot2在分面点图中的因子顺序

时间:2012-08-21 22:31:53

标签: r ggplot2

我正在尝试更改ggplot2中多面点图的各个方面的绘图顺序,但我无法让它工作。这是我的融化数据集:

> London.melt
                      country medal.type count
1                 South Korea       gold    13
2                       Italy       gold     8 
3                      France       gold    11
4                   Australia       gold     7
5                       Japan       gold     7
6                     Germany       gold    11
7  Great Britain & N. Ireland       gold    29
8          Russian Federation       gold    24
9                       China       gold    38
10              United States       gold    46
11                South Korea     silver     8
12                      Italy     silver     9
13                     France     silver    11
14                  Australia     silver    16
15                      Japan     silver    14
16                    Germany     silver    19
17 Great Britain & N. Ireland     silver    17
18         Russian Federation     silver    26
19                      China     silver    27
20              United States     silver    29
21                South Korea     bronze     7
22                      Italy     bronze    11
23                     France     bronze    12
24                  Australia     bronze    12
25                      Japan     bronze    17
26                    Germany     bronze    14
27 Great Britain & N. Ireland     bronze    19
28         Russian Federation     bronze    32
29                      China     bronze    23
30              United States     bronze    29

这是我的情节命令:

qplot(x = count, y = country, data = London.melt, geom = "point", facets = medal.type ~.)

我得到的结果如下:

R plot

facet本身按照我想要的顺序出现在这个图中。然而,在每个方面,我想按计数排序。也就是说,对于每种类型的奖牌,我都希望获得奖牌数量最多的国家,依此类推。我没有方面成功使用的程序(比如说我们只看金牌)就是使用因子reorder上的country函数,按count排序,但是这个在本例中不起作用。

我非常感谢您提出的任何建议。

3 个答案:

答案 0 :(得分:10)

这是一个使用粘贴,自由缩放和一些重新标记的解决方案

library(ggplot2)
London.melt$medal.type<-factor(London.melt$medal.type, levels = c("gold","silver","bronze"))
# Make every country unique
London.melt$country_l <- with(London.melt, paste(country, medal.type, sep = "_"))
#Reorder the unique countrys
q <- qplot(x = count, y = reorder(country_l, count), data = London.melt, geom = "point") +   facet_grid(medal.type ~., scales = "free_y")
# Rename the countries using the original names
q + scale_y_discrete("Country", breaks = London.melt$country_l, label = London.melt$country)

enter image description here

答案 1 :(得分:1)

这显然已经很晚了,我所做的某些事情可能还不到6年前,但是我在执行类似任务时遇到了这个问题。我总是不愿意为矢量设置刻度标签-使用可以对原始标签进行操作的功能会更安全。

为此,我要基于国家和奖牌创建一个因子ID列,并在其中两个列中都没有出现一些定界符-在这种情况下,_可以使用。然后使用forcats::fct_reorder,我可以按count排序该列。此列的最后几个级别位于下面,应该与计数最高的国家/地区和奖牌组合相对应。

library(tidyverse)

London_ordered <- London.melt %>%
  mutate(id = paste(country, medal.type, sep = "_") %>%
           as_factor() %>%
           fct_reorder(count, .fun = min))

levels(London_ordered$id) %>% tail()
#> [1] "Great Britain & N. Ireland_gold" "United States_silver"           
#> [3] "United States_bronze"            "Russian Federation_bronze"      
#> [5] "China_gold"                      "United States_gold"

然后使用此ID作为您的y轴。这样,您将拥有非常长的标签,其中包括奖牌类型。由于使用了独特的定界符,您可以为y轴标签编写一个内联函数,该函数将删除定界符和其后的所有文字字符,只剩下国家/地区。将构面规范移至facet_wrap函数,然后可以设置自由y缩放。

qplot(x = count, y = id, data = London_ordered, geom = "point") +
  scale_y_discrete(labels = function(x) str_remove(x, "_\\w+$")) +
  facet_wrap(~ medal.type, scales = "free_y", ncol = 1)

答案 2 :(得分:0)

这是我能用qplot做的最好的事情。不完全是你要求但更接近。 OOOPs我看到你已经想到了这一点。

q <- qplot(x = count, y = reorder(country, count), data = London.melt, geom = "point", facets = medal.type ~.)

这是一个dput版本,以便其他人可以改进:

dput(London.melt)
structure(list(country = structure(c(9L, 6L, 3L, 1L, 7L, 4L, 
5L, 8L, 2L, 10L, 9L, 6L, 3L, 1L, 7L, 4L, 5L, 8L, 2L, 10L, 9L, 
6L, 3L, 1L, 7L, 4L, 5L, 8L, 2L, 10L), .Label = c("Australia", 
"China", "France", "Germany", "Great Britain & N. Ireland", "Italy", 
"Japan", "Russian Federation", "South Korea", "United States"
), class = "factor"), medal.type = structure(c(2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("bronze", 
"gold", "silver"), class = "factor"), count = c(13L, 8L, 11L, 
7L, 7L, 11L, 29L, 24L, 38L, 46L, 8L, 9L, 11L, 16L, 14L, 19L, 
17L, 26L, 27L, 29L, 7L, 11L, 12L, 12L, 17L, 14L, 19L, 32L, 23L, 
29L)), .Names = c("country", "medal.type", "count"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30"))