【问题标题】:Why are the colors wrong on this ggplot?为什么这个 ggplot 上的颜色错误?
【发布时间】:2016-10-22 00:31:14
【问题描述】:

我是 ggplot2 的新手,所以请怜悯我。

我的第一次尝试产生了一个奇怪的结果(至少对我来说很奇怪)。我可重现的 R 代码是:

library(ggplot2)
iterations = 7
variables = 14
data <- matrix(ncol=variables, nrow=iterations)

data[1,] = c(0,0,0,0,0,0,0,0,10134,10234,10234,10634,12395,12395)
data[2,] = c(18596,18596,18596,18596,19265,19265,19390,19962,19962,19962,19962,20856,20856,21756)
data[3,] = c(7912,11502,12141,12531,12718,12968,13386,17998,19996,20226,20388,20583,20879,21367)
data[4,] = c(0,0,0,0,0,0,0,43300,43500,44700,45100,45100,45200,45200)
data[5,] = c(11909,11909,12802,12802,12802,13202,13307,13808,21508,21508,21508,22008,22008,22608)
data[6,] = c(11622,11622,11622,13802,14002,15203,15437,15437,15437,15437,15554,15554,15755,16955)
data[7,] = c(8626,8626,8626,9158,9158,9158,9458,9458,9458,9458,9458,9458,9558,11438)

df <- data.frame(data)
n_data_rows = nrow(df)

previous_volumes = df[1:(n_data_rows-1),]/1000
todays_volume    = df[n_data_rows,]/1000

time = seq(ncol(df))/6
min_y = min(previous_volumes, todays_volume)
max_y = max(previous_volumes, todays_volume)
ylimit = c(min_y, max_y)
x = seq(nrow(previous_volumes))

# This gives a plot with 6 gray lines and one red line, but no Ledgend

p = ggplot()

for (row in x) {
  y1 = as.integer(previous_volumes[row,])
  dd = data.frame(time, y1)
  p = p + geom_line(data=dd, aes(x=time, y=y1, group="1"), color="gray")
}
p 

此代码生成正确的情节...但没有图例。情节看起来像:

如果我在“aes”中移动“颜色”,我现在会得到一个图例......但是颜色是错误的。 例如代码:

p = ggplot()

for (row in x) {
  y1 = as.integer(previous_volumes[row,])
  dd = data.frame(time, y1)
  p = p + geom_line(data=dd, aes(x=time, y=y1, group="1", color="gray"))
}

y2 = as.integer(todays_volume[1,])
dd = data.frame(time, y2)
p = p + geom_line(data=dd, aes(x=time, y=y2, group="2", colour="red"))
p

产生:

为什么线条颜色不对?

查尔斯

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    可以在单个图层的基础上控制颜色(即颜色 = XYZ)变量,但是,这些不会出现在任何图例中。当您将美学(即在这种情况下为颜色美学)映射到数据中的变量时,就会产生图例,在这种情况下,您需要指示如何表示该特定映射。如果您没有明确指定,ggplot2 将尝试做出最佳猜测(例如因子数据与数值数据的离散和连续映射之间的差异)。这里有很多选项,包括(但不限于):scale_colour_continuousscale_colour_discretescale_colour_brewerscale_colour_manual

    听上去,scale_colour_manual 可能就是您所追求的,请注意,在下面我已将数据中的“变量”列映射到颜色美学,而在“变量”数据中,离散值 [PREV-A to PREV-F,Today] 存在,所以现在我们需要指示实际颜色 'PREV-A','PREV-B',...'PREV-F' 和 'Today' 代表什么。

    或者,如果变量列包含“实际”颜色(即十六进制 '#FF0000' 或名称 'red'),那么您可以使用 scale_colour_identity。我们还可以创建另一列类别('Previous','Today')以使事情变得更容易一些,在这种情况下,一定要引入'group'美学映射以防止具有相同颜色的系列(实际上是不同的series) 在它们之间连续。

    首先准备数据,然后通过一些不同的方法来分配颜色。

    # Put data as points 1 per row, series as columns, start with 
    # previous days
    df.new  = as.data.frame(t(previous_volumes))
    
    #Rename the series, for colour mapping
    colnames(df.new) = sprintf("PREV-%s",LETTERS[1:ncol(df.new)])
    
    #Add the times for each point.
    df.new$Times     = seq(0,1,length.out = nrow(df.new))
    
    #Add the Todays Volume
    df.new$Today = as.numeric(todays_volume)
    
    #Put in long format, to enable mapping of the 'variable' to colour.
    df.new.melt       = reshape2::melt(df.new,'Times')
    
    #Create some colour mappings for use later
    df.new.melt$color_group    = sapply(as.character(df.new.melt$variable),
                                        function(x)switch(x,'Today'='Today','Previous'))
    df.new.melt$color_identity = sapply(as.character(df.new.melt$variable),
                                        function(x)switch(x,'Today'='red','grey'))
    

    这里有几种不同的处理颜色的方法:

    #1. Base plot + color mapped to variable
    plot1 = base + geom_path(aes(color=variable)) + 
      ggtitle("Plot #1")
    
    #2. Base plot + color mapped to variable, Manual scale for Each of the previous days and today
    colors = setNames(c(rep('gray',nrow(previous_volumes)),'red'),
                                     unique(df.new.melt$variable))
    plot2 = plot1 + scale_color_manual(values = colors) + 
      ggtitle("Plot #2")
    
    #3. Base plot + color mapped to color group
    plot3 = base + geom_path(aes(color = color_group,group=variable)) + 
      ggtitle("Plot #3")
    
    #4. Base plot + color mapped to color group, Manual scale for each of the groups
    plot4 = plot3 + scale_color_manual(values = c('Previous'='gray','Today'='red')) +
      ggtitle("Plot #4")
    
    #5. Base plot + color mapped to color identity
    plot5 = base + geom_path(aes(color = color_identity,group=variable))
    plot5a = plot5 + scale_color_identity() +  #Identity not usually in legend
      ggtitle("Plot #5a")
    plot5b = plot5 + scale_color_identity(guide='legend') + #Identity forced into legend
      ggtitle("Plot #5b")
    
    gridExtra::grid.arrange(plot1,plot2,plot3,plot4,
                            plot5a,plot5b,ncol=2,
                            top="Various Outputs")
    

    所以考虑到你的问题,#2 或#4 可能是你所追求的,使用#2,我们可以添加另一个层来渲染最后一个点的值:

    #Additionally, add label of the last point in each series.
    df.new.melt.labs = plyr::ddply(df.new.melt,'variable',function(df){ 
      df       = tail(df,1) #Last Point
      df$label = sprintf("%.2f",df$value)
      df
    })
    baseWithLabels = base +   
      geom_path(aes(color=variable)) +
      geom_label(data = df.new.melt.labs,aes(label=label,color=variable),
                 position = position_nudge(y=1.5),size=3,show.legend = FALSE) +
      scale_color_manual(values=colors)
    print(baseWithLabels)
    

    如果您希望能够区分各种“PREV-X”行,那么您还可以将linetype 映射到此变量和/或使标签几何图形更具描述性,下面演示了这两种修改:

    #Add labels of the last point in each series, include series info:
    df.new.melt.labs2 = plyr::ddply(df.new.melt,'variable',function(df){ 
      df       = tail(df,1) #Last Point
      df$label = sprintf("%s: %.2f",df$variable,df$value)
      df
    })
    baseWithLabelsAndLines = base +   
      geom_path(aes(color=variable,linetype=variable)) +
      geom_label(data = df.new.melt.labs2,aes(label=label,color=variable),
                 position = position_nudge(y=1.5),hjust=1,size=3,show.legend = FALSE) +
      scale_color_manual(values=colors) +
      labs(linetype = 'Series')
    print(baseWithLabelsAndLines)
    

    【讨论】:

    • 也许我没有让自己清楚颜色。我的颜色对我来说很重要。您的解决方案非常优雅,我很欣赏您所做的一切。但是,您提到的“数千”篇文章在描述如何告诉 ggplot 不要更改我分配的颜色方面做得很少。
    【解决方案2】:

    这个问题很老,但我在处理同样的问题时发现了它。我从here 得到的解决方案是将scale_colour_identity() 添加到您的ggplot 对象中-

    p = p + geom_line(data=dd, aes(x=time, y=y2, group="2", colour="red"))
    p = p + scale_colour_identity()
    p
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 2023-04-05
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多