首页 文章

多语种情节与ggplot

提问于
浏览
0

有时需要以两种或更多种语言创建一组数据的图 . 当然可以手动输入ggplot图层来制作轴 Headers 并引导图例标签发布就绪,但我想添加一些自动化来减少所需的代码量 .

而不是添加层来替换例如在axis标签中的变量名,我想将 ggplot 调用包装在像 replace_labels(plot,target_language) 这样的函数中,它会在我定义的字典中自动查找字符串 . 我写了一些几乎可行的代码,但问题是

  • 代码依赖于ggplot的实现,它可能会发生变化 . 例如,它适用于ggplot 0.9但不适用于2.0 .

  • 它侵入 scales 对象等,并遇到标签宽度和布局等低级问题

  • 它需要创建绘图,然后检测使用了哪些字符串(因为ggplot在渲染ggplot对象之前不会完全显示标签)然后以某种方式以编程方式添加 breaks = c("something", "or", "other") 等并使用这些重新创建绘图对象

我也考虑过更换 data.frame 中的字符串,但这不是一个好的解决方案,因为我需要为每种语言编写 ggplot 调用,这对我来说似乎是不必要的代码复制 . 任何建议如何优雅地国际化地块而不需要重写ggplot?

Edit :添加了由mdag02创建的略微修改的示例 .

library(ggplot2)

language <- list(
  fr_FR = list(
    title = "Iris",
    subtitle = "Le jeu de données connu",
    caption = "French",
    x = "Longueur des sépales, cm",
    y = "Largeur des sépales, cm",
    labels = c(setosa = "Setosa", versicolor = "Versicolor", virginica = "Virginica"),
    legend = "Espèce",
    labeller = function(variable,value) c(setosa = "Setosa", versicolor = "Versicolor", virginica = "Virginica")[value]
  ),
  en_US = list(
    title = "Iris",
    subtitle = "The famous dataset",
    caption = "English",
    x = "Sepal Length, cm",
    y = "Sepal Width, cm",
    labels = c(setosa = "Setosa", versicolor = "Versicolor", virginica = "Virginica"),
    legend = "Species",
    labeller = function(variable,value) c(setosa = "Setosa", versicolor = "Versicolor", virginica = "Virginica")[value]
  )
)

for (l in names(language)) {
  message(l)
  current <- language[[l]]

  print(ggplot(data = subset(iris,
                             Species %in% c("virginica","versicolor")),
               aes(x = Sepal.Length,
                   y = Sepal.Width)) +
          geom_point() +
          facet_grid(~Species,labeller = current$labeller)+
          scale_colour_discrete(labels = current$labels,
                                name = current$legend) + 
          labs(
            title = current$title,
            subtitle = current$subtitle,
            caption = paste(current$caption, format(Sys.Date(), "%Y-%m-%d")),
            x = current$x,
            y = current$y))

  ggsave(file = paste0("iris_", l, ".png"), width = 21, height = 13, units = "cm", scale = 0.8)
}

我想这样做,以便我只需要将代码编写到geom,例如 geom_point ,并让计算机处理标签替换格式,例如 scale_colour_discrete 喜欢这样:

ggplot(data = subset(iris,
                     Species %in% c("virginica","versicolor")),
                   aes(x = Sepal.Length,
                       y = Sepal.Width)) +
              geom_point() +
              facet_grid(~Species)

我可以通过将格式化图层封装在像_2566905这样的变量中来实现这个方向,但这种方法的一个缺点是,如果我想手动更改颜色,那么我需要重新引入标签替换代码,因为我的手动格式化将覆盖 myformats 变量中的标签替换 .

此外,如果我决定要绘制 Petal.Width ,那么我需要记住更改字典和 ggplot 调用中的标签 . 我很难记住,所以我再次宁愿把它留给电脑 .

1 回答

  • 2

    您可以循环遍历字符串列表:

    library(ggplot2)
    
    language <- list(
        fr_FR = list(
                title = "Iris",
                subtitle = "Le jeu de données connu",
                caption = "French",
                x = "Longueur",
                y = "Largeur"
        ),
        en_US = list(
                title = "Iris",
                subtitle = "The famous dataset",
                caption = "English",
                x = "Length",
                y = "Width"
        )
    )
    
    for (l in names(language)) {
        message(l)
        current <- language[[l]]
    
        ggplot(data = iris, aes(Sepal.Length, Sepal.Width)) +
            geom_point() +
            geom_smooth() +
            labs(
                title = current$title,
                subtitle = current$subtitle,
                caption = paste(current$caption, format(Sys.Date(), "%Y-%m-%d")),
                x = current$x,
                y = current$y)
    
        ggsave(file = paste0("iris_", l, ".svg"), width = 21, height = 13, units = "cm", scale = 0.8)
    }
    

相关问题