gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:31:58 29 4
gpt4 key购买 nike

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

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

enter image description here

替换 xlabylabggtitle ,并修改轴标签的样式(例如数字格式)相当简单,所以我的重点实际上是图例键及其在图例中的列出顺序。

带有大量名称的 MWE 来说明在下面的方法中必须多次复制名称的繁琐性(一次复制到 group ,另一次复制到 colour ,再次复制到 shape ,等等):

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

并在图例键中将左侧出现的每个位置替换为右侧。 (如果该方法可以处理三语言方法,例如像 "Belgium" = 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']]

关于r - ggplot2:替换图例键并按字母顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36926582/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com