R 将颜色更改为ggplot对象的定义调色板
我想使用函数将所有R 将颜色更改为ggplot对象的定义调色板,r,colors,ggplot2,color-palette,R,Colors,Ggplot2,Color Palette,我想使用函数将所有geom.*类型对象的默认颜色更改为特定调色板 下面是geom\u line()和使用函数change\u colors() #加载ggplot2和tidyr库 需要(ggplot2) 需要(三年) #创建模拟数据帧 df以下内容应满足您的要求。请注意,它仅更改映射到变量的颜色。直接传递到geom.*的颜色不会受到影响(下面有一个示例)。有关修改颜色或填充(以最先映射的为准)的方法,请参阅本帖的下半部分 change_colours <- function(p, pale
geom.*
类型对象的默认颜色更改为特定调色板
下面是geom\u line()
和使用函数change\u colors()
#加载ggplot2和tidyr库
需要(ggplot2)
需要(三年)
#创建模拟数据帧
df以下内容应满足您的要求。请注意,它仅更改映射到变量的颜色。直接传递到geom.*
的颜色不会受到影响(下面有一个示例)。有关修改颜色
或填充
(以最先映射的为准)的方法,请参阅本帖的下半部分
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 post
p <- ggplot(df, aes(x=TS, y=price, group=stock))
使用不同的调色板:
change_colours(p + geom_smooth(aes(colour=stock)),
c('firebrick', 'midnightblue', 'violet', 'seagreen'))
如上所述,这只会更改映射到变量的颜色
和填充
。例如,它不会影响以下各项的颜色:
change_colours(p + geom_point(colour=rep(c('tomato', 'hotpink', 'cadetblue'), each=300)),
custom_pal)
作为对OP的响应,您可以轻松检测正在使用的映射类型(例如,alpha
,color
,fill
)。只要看看p$layers[[1]]$mapping
如果我们假设第一层的第一个填充
或颜色
映射是要更改颜色的映射,则可以执行以下操作:
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 post
p <- ggplot(df, aes(x=TS, y=price, group=stock))
显示第一个映射颜色/填充美学的优先级:
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'), type='fill')
我不明白这个问题。似乎您的代码已经在执行您要求的操作了。你有什么要求?啊,我明白了。对于所有原语……是否有办法检查/检测是否使用填充物或颜色作为“主”着色“刷”?e、 g.尝试时:df2%分组依据(股票)%>%汇总(平均价格=平均价格);p2
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 post
p <- ggplot(df, aes(x=TS, y=price, group=stock))
change_colours(p + geom_point(aes(fill=stock), pch=21),
c('white', 'grey50', 'grey80'))
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'))
change_colours(p + geom_point(aes(color=stock, fill=stock), pch=21) +
geom_smooth(aes(color=stock)),
c('black', 'grey50', 'grey80'), type='fill')