ggplot2:替换图例键并按字母顺序排序

我用ggplot2(Hadley Wikham)用两种语言制作了一套图表.我可以通过重命名原始数据集中的变量,在两个单独的工作流程中生成它们.相反,我希望修改一个ggplot对象:我希望首先用英语生成图形,然后将标签翻译成法语.怎么可以/我应该更改ggplot对象内的图例键?然后,我如何对图例键进行排序?

我探索这种方法的原因是我希望我的情节颜色和符号在英语和法语中是相同的,同时按字母顺序排列图例键.问题是法语和英语的传奇键没有相同的字母顺序(西班牙语与Espagne).比较从MWE获得的图例键:图例键在英语图例中按字母顺序排序,但在法语图例中不正确.

《ggplot2:替换图例键并按字母顺序排序》

替换xlab,ylab,ggtitle以及修改轴标签的样式(例如数字格式)非常简单,因此我的重点实际上是图例键及其在图例中列出的顺序.

一个具有大量名称的MWE,用于说明在下面的方法中需要多次复制名称的繁琐程度(一次分组,另一次用于颜色,再次用于形状等):

    df <- structure(list(year = c("2006", "2007", "2006", "2007", "2006", 
    "2007", "2006", "2007", "2006", "2007", "2006", "2007", "2006", 
    "2007", "2006", "2007", "2006", "2007", "2006", "2007", "2006", 
    "2007", "2006", "2007", "2006", "2007", "2006", "2007", "2006", 
    "2007", "2006", "2007", "2006", "2007", "2006", "2007", "2006", 
    "2007", "2006", "2007"), country = c("Australia", "Australia", 
    "Austria", "Austria", "Belgium", "Belgium", "Canada", "Canada", 
    "Denmark", "Denmark", "Finland", "Finland", "France", "France", 
    "Germany", "Germany", "Greece", "Greece", "Italy", "Italy", "Japan", 
    "Japan", "Netherlands", "Netherlands", "New Zealand", "New Zealand", 
    "Norway", "Norway", "Portugal", "Portugal", "Spain", "Spain", 
    "Sweden", "Sweden", "Switzerland", "Switzerland", "United Kingdom", 
    "United Kingdom", "United States", "United States"), value = c(33, 
    33, 33, 33, 30, 30, 34, 34, 30, 30, 33, 33, 28, 29, 27, 27, 40, 
    39, 35, 35, 35, 35, 27, 27, 33, 33, 27, 27, 37, 37, 32, 32, 31, 
    31, 32, 31, 32, 32, 33, 33)), .Names = c("year", "country", "value"
    ), row.names = c(NA, -40L), class = "data.frame")

    library("ggplot2")
    ggplot(data = df, aes(x = year, y = value, group = country, colour = country)) + 
        geom_line(size = 0.5) + geom_point(size = 1)
    ggsave(last_plot(), file = "stackoverflow-1.png")

    ggplot(data = df, aes(x = year, y = value, group = factor(country, labels = c("Australie", "Autriche", "Belgique", "Canada", "Danemark", "Finlande", "France", "Allemagne", "Grèce", "Italie", "Japon", "Pays-Bas", "Nouvelle-Zélande", "Norvège", "Portugal", "Espagne", "Suède", "Suisse", "Royaume-Uni", "États-Unis")), colour = factor(country, labels = c("Australie", "Autriche", "Belgique", "Canada", "Danemark", "Finlande", "France", "Allemagne", "Grèce", "Italie", "Japon", "Pays-Bas", "Nouvelle-Zélande", "Norvège", "Portugal", "Espagne", "Suède", "Suisse", "Royaume-Uni", "États-Unis")))) + geom_line(size = 0.5) + geom_point(size = 1) + theme(legend.title = element_blank())
    ggsave(last_plot(), file = "stackoverflow-2.png")

如果我只使用变量的一个子集(示例中的国家/地区),我想有一个不会破坏的方法.最方便且不易出错的方法是定义这样的映射:

list("A Cuckoo Land" = "Un Pays Idyllique", # This mapping is not used
 "Australia" = "Australie", 
 "Austria" = "Autriche", 
 "Belgium" = "Belgique", 
 "Canada" = "Canada",
 "Denmark" = "Danemark", 
 "Finland" = "Finlande", 
 "France" = "France", 
 "Germany" = "Allemagne", 
 "Greece" = "Grèce", 
 "Italy" = "Italie", 
 "Japan" = "Japon", 
 "Netherlands" = "Pays-Bas", 
 "New Zealand" = "Nouvelle-Zélande", 
 "Norway" = "Norvège", 
 "Portugal" = "Portugal", 
 "Spain" = "Espagne", 
 "Sweden" = "Suède", 
 "Switzerland" = "Suisse", 
 "United Kingdom" = "Royaume-Uni", 
 "United States" = "États-Unis")

并且在图例键内,用右侧替换左侧的每次出现. (如果该方法可以处理三种语言方法,甚至更好,例如像“比利时”= c(“Belgique”,“Bélgica”)这样的映射.

最佳答案 实际上,我可以通过创建具有相同列名的数据框列表来实现此目的,但使用不同语言的国家/地区名称.如果有很多数据框,那么创建数据框列表可能会有点工作,但我相当肯定它会比使用grobs和gtables更加麻烦.一个例子:

key <- unlist(list("A Cuckoo Land" = "Un Pays Idyllique", # This mapping is not used
                   "Australia" = "Australie", 
                   "Austria" = "Autriche", 
                   "Belgium" = "Belgique", 
                   "Canada" = "Canada",
                   "Denmark" = "Danemark", 
                   "Finland" = "Finlande", 
                   "France" = "France", 
                   "Germany" = "Allemagne", 
                   "Greece" = "Grèce", 
                   "Italy" = "Italie", 
                   "Japan" = "Japon", 
                   "Netherlands" = "Pays-Bas", 
                   "New Zealand" = "Nouvelle-Zélande", 
                   "Norway" = "Norvège", 
                   "Portugal" = "Portugal", 
                   "Spain" = "Espagne", 
                   "Sweden" = "Suède", 
                   "Switzerland" = "Suisse", 
                   "United Kingdom" = "Royaume-Uni", 
                   "United States" = "États-Unis"))
df_eng <- df
df_fra <- df
df_fra$country <- unlist(key[df_eng$country])

dfs <- list('english' = df_eng,'french' = df_fra)

library("ggplot2")
#Now you can create one "default" plot...
p <- ggplot(data = dfs[['english']], 
            aes(x = year, y = value, 
                group = country, colour = country)) + 
  geom_line(size = 0.5) + 
  geom_point(size = 1)
print(p)

#And simply swap out the data frame...
p %+% dfs[['french']]
点赞