如何为ggplot的嵌套循环中的各个图分配相同的颜色? [英] How to assign same color to factors across plots in a nested loop for ggplot?

查看:187
本文介绍了如何为ggplot的嵌套循环中的各个图分配相同的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用scale_fill_manual为嵌套的for循环中许多图上的因子分配相应的颜色.但是,最终的图最终都是黑色的.

I am trying to use scale_fill_manual to assign corresponding colors to factors across many plots in a nested for loop. However, the resulting plots end up all being black.

我的总体循环如下:

for(i in seq(from=0, to=100, by=10)){
   for{j in seq(from=0, to=100, by=10)){
       print(ggplot(aes(x , y), data = df)+
        geom_point(inherit.aes = FALSE,data = subset(df,factor_x==i&factor_y==j), aes(x, y, size=point,color=Group))+
        theme_bw()}}

我正在尝试为组"中的每个因子分配自己的颜色,该颜色始终一致.我尝试使用:

I am trying to assign each factor in "Group" its own color that plots consistently. I've tried using:

col<-colorRampPalette(brewer.pal(9,"Set1"))(16)

然后我将每种颜色分配给组"中的特定因素.

Then I assigned each color to a specific factor in "Group."

但是,在嵌套循环中使用刻度尺手动时,这些因素完全没有颜色.

However, when using scale manual in the nested loop there is no color at all for the factors.

for(i in seq(from=0, to=100, by=10)){
   for{j in seq(from=0, to=100, by=10)){
       print(ggplot(aes(x , y), data = df)+
        geom_point(inherit.aes = FALSE,data = subset(df,factor_x==i&factor_y==j), aes(x, y, size=point))+
        theme_bw()+scale_fill_manual(values=col)}}

如何在循环中生成的许多图上为组"中的分类值集成颜色方案?

How can I integrate a color scheme for the categorical values in "Group" across the many plots generated in the loop?

推荐答案

该想法是创建一个命名的颜色向量,该向量将所需的颜色分配给您用于该颜色(或填充)的因子变量的每个潜在水平情节中的美学.然后在scale_color_manual(或scale_fill_manual)中使用该颜色矢量设置绘图颜色.这将为所需的因子级别分配所需的颜色,而不管用于给定图的特定数据框中是否存在给定的因子级别.

The idea is to create a named vector of colors that assigns desired colors to each potential level of the factor variable you're using for the color (or fill) aesthetic in your plot. Then use that color vector in scale_color_manual (or scale_fill_manual) to set the plot colors. This will assign the desired color to the desired factor level, regardless of whether a given factor level is present in the particular data frame used for a given plot.

这是一个简单的例子:

library(ggplot2)

# Plotting function
pfunc = function(data, x, y, col_var, color_vec, drop=TRUE) {
  ggplot(data, aes_string(x, y, colour=col_var)) +
    geom_point(size=3) +
    scale_colour_manual(values=color_vec, drop=drop)
}

现在使用内置的iris数据帧(使用整个数据帧和部分数据)运行该功能.

Now run the function with the built-in iris data frame using the whole data frame and a subset of the data.

# Named vector giving desired color for each Species
col = setNames(c("green","red","blue"), levels(iris$Species))

pfunc(iris, "Petal.Width", "Petal.Length", "Species", col)
pfunc(subset(iris, Species=="versicolor"), 
      "Petal.Width", "Petal.Length", "Species", col)
pfunc(subset(iris, Species=="versicolor"), 
      "Petal.Width", "Petal.Length", "Species", col, drop=FALSE)

或使用diamonds数据框:

n = length(levels(diamonds$color))
col = setNames(hcl(seq(15,375,length=n+1)[1:n], 100, 65), levels(diamonds$color))

set.seed(2)
dat = diamonds[sample(1:nrow(diamonds), 200), ]

pfunc(dat, "carat", "price", "color", col)
pfunc(subset(dat, color %in% c("D","G")), "carat", "price", "color", col)
pfunc(subset(dat, color %in% c("G","I")), "carat", "price", "color", col)

这篇关于如何为ggplot的嵌套循环中的各个图分配相同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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