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

inh*_*kht 8 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
Run Code Online (Sandbox Code Playgroud)

这是我的情节命令:

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

我得到的结果如下:

R图

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

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

Luc*_*zer 11

这里使用粘贴,自由秤和一些重新贴标签的解决方案

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述