色彩空间中scale_fill/color中的粘贴名称无法循环使用 [英] Paste name in scale_fill/color in colorspace does not work in a loop

查看:67
本文介绍了色彩空间中scale_fill/color中的粘贴名称无法循环使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for循环 function 内的 colorspace 中使用 scale_fill_continuous_diverging 时,不会为粘贴图例标题的名称,并通过以下示例中的错误输入.

When using scale_fill_continuous_diverging from colorspace inside a for loop or function does not paste for the name of legend title and through an error as in the example below.

library("ggplot2")
library(colorspace)
set.seed(100)
df <- data.frame(country = LETTERS, V = runif(26, -40, 40))
df$country = factor(LETTERS, LETTERS[order(df$V)]) # reorder factors

    fun_plt<-function(x){
      
      x<-df
      gg <- ggplot(df, aes(x = country, y = V, fill = V)) +
        geom_bar(stat = "identity") +
        labs(y = "Under/over valuation in %", x = "Country") +
        coord_flip() + theme_minimal()
      gg
     #Lets say want to paste test in the leged when running through function
      tt<-"test"
  
      gg2<-gg+
        scale_fill_continuous_diverging(name=paste0(tt,"V"),
                                        palette = "Blue-Red 3", l1 = 30, l2 = 100, p1 = .9, p2 = 1.2)
      gg2
    }


fun_plt(df)

它独立运行良好,但是在函数内部运行时会出错.

It works well independently but gives an error when run inside a function.

推荐答案

这与基础 ggplot2 缩放功能( discrete_scale 连续标尺 binned_scale )由 colorspace 中的比例尺函数调用.调用环境并非总是被保留,因此对某些参数(如 name )的非标准评估无法按预期进行.

This was a problem with the way how the underlying ggplot2 scale functions (discrete_scale, continuous_scale, binned_scale) were called by the scale functions in colorspace. The calling environment was not always preserved and hence the non-standard evaluation of some arguments like name did not work as intended.

我现在已经在R-Forge上的 colorspace 的当前开发版本2.0-1中修复了该问题.安装此程序可以修复该错误:

I have fixed the problem now in version 2.0-1, the current development version of colorspace on R-Forge. Installing this should fix the bug:

install.packages("colorspace", repos = "http://R-Forge.R-project.org")

这篇关于色彩空间中scale_fill/color中的粘贴名称无法循环使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆