Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

r - Paste name in scale_fill/color in colorspace does not work in a loop

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.

question from:https://stackoverflow.com/questions/66049336/paste-name-in-scale-fill-color-in-colorspace-does-not-work-in-a-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...