8

我正在尝试更改 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图

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

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

4

3 回答 3

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)

在此处输入图像描述

于 2012-08-22T00:54:47.087 回答
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)

于 2019-02-26T15:26:34.010 回答
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"))
于 2012-08-21T23:51:44.270 回答