本文介绍了将颜色更改为ggplot对象的定义调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想使用函数将所有 geom _ * 类型对象的默认颜色更改为特定调色板。 下面是 geom_line()并使用函数 change_colours() #load ggplot2和tidyr库 require(ggplot2) require(tidyr) #创建一个模拟数据框架 df< - data.frame(cbind(var1 = 500 * cumprod(1 + rnorm(300,0,0.04)), var2 = 400 * cumprod(1 + rnorm (300,0,0.04)), var3 = 300 * cumprod(1 + rnorm(300,0,0.04)))) df $ TS df #创建基本基本图p #自定义托盘 custom_pal #313131,#335291,#9AC933,#33B4E5 #8ED2EC,#63A158,#CADB35,#575757,#4C689F,#A7D04C,#4CBDE8,#9DD8EE,# 76AD6D,#D1DF4F,#8C8C8C,#7F93BA,#C0DE80,#80D0EE,#B8E3F3,#9DC496,#DEE881,#B4B4B4 #99A9C8,#CDE499,#99D9F2,#C7E9F5,#B1D0AB,#E5ED9A,#DCDCDC) #函数改变颜色 change_colours< - function(ggplot_obj,pal){p< - ggplot_obj group_data< - p $ data [,as.character(p $ mapping $ group )] n_groups group_data_cols p + theme_light()+ geom_line(color = group_data_cols)} p p1 p1 我希望将 change_colours()函数更改为更通用的 geom_ * 类型图层 任何帮助将非常感激。解决方案以下应该做你所做的。注意,它只改变映射到变量的颜色。直接传递到 geom _ * 的颜色不会受到影响(下面有一个示例)。对于修改 color 或 填充(以先映射的方法)的方法, change_colours< - function(p,palette){n< - nlevels p $ data [[deparse(p $ mapping $ group)]]) tryCatch(as.character(palette), error = function(e)stop('palette should be a vector of colors' ,call。= FALSE)) if(n> length(palette))stop('Paless中没有足够的颜色') pal< - function(n)palette [seq_len ] p + theme_light()+ discrete_scale('color','foo',pal)} 这里,df来自OP的帖子p< - ggplot(df,aes(x = TS,y = price,group = stock)) #NB:custom_pal在OP的帖子中定义 change_colours(p + geom_line )),custom_pal) change_colours(p + geom_point(aes(color = stock)),custom_pal) 使用不同的调色板: change_colours(p + geom_smooth(aes(color = stock)),c('firebrick','midnightblue' violet','seagreen')) 如上所述,这只会更改 color 和 fill 映射到变量。例如,它对以下颜色没有影响: change_colours(p + geom_point c('tomato','hotpink','cadetblue'),each = 300)), custom_pal) b $ b 回应OP的评论,您可以轻松地检测到什么类型的正在使用映射(例如 alpha , color , fill )。只需看看 p $ layers [[1]] $ mapping 。 如果我们假设第一个填充或 color 第一层的映射是你想改变颜色的映射,你可以这样做: change_colours< - function p,palette,type){n tryCatch(as.character(palette), error = function(e)stop('palette应该是一个颜色的向量,调用。= FALSE)) if(n> length(palette))stop('调色板中没有足够的颜色' $ b if(missing(type)) type< - grep('color | fill',names(p $ layers [[1]] $ mapping),value = TRUE)[1] pal p + theme_light()+ discrete_scale(type,'foo',pal)} # df来自OP的帖子p 示例: 更改填充而不是颜色: change_colours(p + geom_point(aes(fill = stock),pch = 21),c('white','grey50','grey80')) 显示第一个映射的颜色/填充美感的优先级: change_colours(p + geom_point(fill = stock,color = stock) ,pch = 21)+ geom_smooth(aes(color = stock)),c('black','grey50','grey80')) / pre> change_colours(p + geom_point(aes(color = stock,fill = stock),pch = 21)+ geom_smooth(aes(color = stock)),c('black','grey50 ','grey80')) 使用类型参数覆盖第一个映射美学的优先级,例如: change_colours(p + geom_point(aes(color = stock,fill = stock),pch = 21)+ geom_smooth ,c('black','grey50','grey80'),type ='fill') I would like to change the default colours to a specific palette for all geom_* type objects by using a function.Below is an example for geom_line() and using the function change_colours()# load ggplot2 and tidyr libraryrequire(ggplot2)require(tidyr)# create a mock data framedf <- data.frame(cbind(var1=500*cumprod(1+rnorm(300, 0, 0.04)), var2=400*cumprod(1+rnorm(300, 0, 0.04)), var3=300*cumprod(1+rnorm(300, 0, 0.04))))df$TS <- as.POSIXct(Sys.time()+seq(300))df <- gather(df, stock, price, -TS)# create basic base graphp <- ggplot(df, aes(x=TS, y=price, group=stock))+geom_line(aes(colour=stock))# custom palletcustom_pal <- c("#002776", "#81BC00", "#00A1DE", "#72C7E7", "#3C8A2E", "#BDD203", "#313131", "#335291", "#9AC933", "#33B4E5", "#8ED2EC", "#63A158", "#CADB35", "#575757", "#4C689F", "#A7D04C", "#4CBDE8", "#9DD8EE", "#76AD6D", "#D1DF4F", "#8C8C8C", "#7F93BA", "#C0DE80", "#80D0EE", "#B8E3F3", "#9DC496", "#DEE881", "#B4B4B4", "#99A9C8", "#CDE499", "#99D9F2", "#C7E9F5", "#B1D0AB", "#E5ED9A", "#DCDCDC")# the function the change colourschange_colours <- function(ggplot_obj, pal){ p <- ggplot_obj group_data <- p$data[, as.character(p$mapping$group)] n_groups <- length(unique(group_data)) group_data_cols <- pal[group_data] p + theme_light()+ geom_line(colour=group_data_cols)}pp1 <- change_colours(ggplot_obj=p, pal=custom_pal)p1I am hoping to change the change_colours() function to be more generic for all geom_* type layersAny help would be much appreciated. 解决方案 The following should do what you're after. Note that it only changes colours that are mapped to variables. Colour passed directly to the geom_* won't be affected (there's an example below). For an approach that modifies colour or fill (whichever is mapped first), see the bottom half of this post.change_colours <- function(p, palette) { n <- nlevels(p$data[[deparse(p$mapping$group)]]) tryCatch(as.character(palette), error=function(e) stop('palette should be a vector of colours', call.=FALSE)) if(n > length(palette)) stop('Not enough colours in palette.') pal <- function(n) palette[seq_len(n)] p + theme_light() + discrete_scale('colour', 'foo', pal)}# Here, df is from the OP's postp <- ggplot(df, aes(x=TS, y=price, group=stock))Examples:# NB: custom_pal is defined in the OP's postchange_colours(p + geom_line(aes(colour=stock)), custom_pal)change_colours(p + geom_point(aes(colour=stock)), custom_pal)And with a different palette:change_colours(p + geom_smooth(aes(colour=stock)), c('firebrick', 'midnightblue', 'violet', 'seagreen'))As mentioned above, this will only change colour and fill that are mapped to variables. For example, it'll have no effect on the colours for the following:change_colours(p + geom_point(colour=rep(c('tomato', 'hotpink', 'cadetblue'), each=300)), custom_pal)In response to the OP's comment, you can easily detect what types of mappings are being used (e.g. alpha, colour, fill). Just look at p$layers[[1]]$mapping.If we assume that the first fill or colour mapping of the first layer is the mapping for which you want to change colours, you can do:change_colours <- function(p, palette, type) { n <- nlevels(p$data[[deparse(p$mapping$group)]]) tryCatch(as.character(palette), error=function(e) stop('palette should be a vector of colours', call.=FALSE)) if(n > length(palette)) stop('Not enough colours in palette.') if(missing(type)) type <- grep('colour|fill', names(p$layers[[1]]$mapping), value=TRUE)[1] pal <- function(n) palette[seq_len(n)] p + theme_light() + discrete_scale(type, 'foo', pal)}# Here, df is from the OP's postp <- ggplot(df, aes(x=TS, y=price, group=stock))Examples:Changing fill instead of colour:change_colours(p + geom_point(aes(fill=stock), pch=21), c('white', 'grey50', 'grey80'))Showing priority of first mapped colour/fill aesthetic:change_colours(p + geom_point(aes(fill=stock, color=stock), pch=21) + geom_smooth(aes(color=stock)), c('black', 'grey50', 'grey80'))change_colours(p + geom_point(aes(color=stock, fill=stock), pch=21) + geom_smooth(aes(color=stock)), c('black', 'grey50', 'grey80'))Override the priority of the first mapped aesthetic with the type argument, e.g.:change_colours(p + geom_point(aes(color=stock, fill=stock), pch=21) + geom_smooth(aes(color=stock)), c('black', 'grey50', 'grey80'), type='fill') 这篇关于将颜色更改为ggplot对象的定义调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 20:35