【问题标题】:making a WCS Munsell color chart in R, problems with order in scale_fill_manual, ggplot2在 R 中制作 WCS Munsell 颜色图表,scale_fill_manual 中的顺序问题,ggplot2
【发布时间】:2021-11-23 22:21:42
【问题描述】:

我想为世界颜色调查使用的芯片制作孟塞尔颜色图表。它应该是这样的:

需要的信息可以在WCS页面here找到,我采取以下步骤:

library(munsell) # https://cran.r-project.org/web/packages/munsell/munsell.pdf
library(ggplot2)

# take the "cnum-vhcm-lab-new.txt" file from: https://www1.icsi.berkeley.edu/wcs/data.html#wmt
# change by replacing .50 with .5 removing .00 after hue values

WCS <- read.csv("cnum-vhcm-lab-new.txt", sep = "\t", header = T)
WCS$hex <- mnsl2hex(hvc2mnsl(hue = WCS$MunH, value = ceiling(WCS$MunV), chroma = WCS$C), fix = T)

# this works, but the order of tiles is messed up
ggplot(aes(x=H, y=V, fill=hex), data = WCS) +   
  geom_tile(aes(x=H, y=V), show.legend = F) +
  scale_fill_manual(values = WCS$hex) +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 40)) 

结果:

显然,芯片不是按照色调和值排序,而是参考其他维度,甚至可能在原始数据框中排序。我还必须恢复 y 轴上的顺序。我想解决方案将与factor()reorder() 有关,但是怎么做呢?

【问题讨论】:

  • 您能否提供“通过删除色调值后的 .5 和 .00 进行更改”的代码?我不断收到错误。
  • 嗨布赖恩,只是在文本编辑或类似的情况下,在阅读 R 中的文件之前。查找和替换,实际上我没有正确输入,它应该是 .50 > .5 和 . 00 > 完全删除。我现在将编辑我的 OP。

标签: r ggplot2 colors


【解决方案1】:

主要是使用正确的色标(scale_fill_identity)。这可确保将十六进制值用作图块的颜色。

library(munsell) # https://cran.r-project.org/web/packages/munsell/munsell.pdf
library(ggplot2)

WCS <- read.csv(url('https://www1.icsi.berkeley.edu/wcs/data/cnum-maps/cnum-vhcm-lab-new.txt'), sep = "\t", header = T)
WCS$hex <- mnsl2hex(hvc2mnsl(hue = gsub('.00','',gsub('.50', '.5',WCS$MunH)), value = ceiling(WCS$MunV), chroma = WCS$C), fix = T)

# this works, but the order of tiles is messed up
ggplot(aes(x=H, y=V, fill=hex), data = WCS) +   
  geom_tile(aes(x=H, y=V), show.legend = F) +
  scale_fill_identity() +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 40)) 

reprex package (v2.0.1) 于 2021-10-05 创建

【讨论】:

    【解决方案2】:

    操作。 TL;DR - 您应该使用 scale_fill_identity() 而不是 scale_fill_manual()

    现在是长篇描述:ggplot2 的核心功能是将数据列映射到绘图上的特定特征,ggplot2 将其称为“美学”,使用aes() 函数。定位是通过将数据的某些列映射到xy 美学来定义的,并且您的磁贴中的不同颜色也使用aes() 映射到fill

    fill 的映射没有指定颜色,而只指定了哪些东西应该是不同的颜色。以这种方式映射时,这意味着在映射到 fill 美学的列中具有相同值的数据行(观察)将是相同的颜色,并且在映射到 fill 的列中具有不同值的观察审美会有不同的颜色。重要的是,这并没有指定颜色,而只是指定颜色是否应该不同!

    默认行为是ggplot2 将通过应用默认比例来确定要使用的颜色。对于连续(数字)值,应用连续比例,而对于离散值(如字符向量),应用离散比例。

    要查看默认行为,只需从绘图代码中删除 scale_fill_manual(...)。我在下面重新复制了您的代码,并添加了所需的修订以编程方式删除和调整".50"".00" 更改为WCS$MunH。如果您从您提供的链接下载了原始 .txt 文件,则下面的代码应该可以完全运行。

    library(munsell)
    library(ggplot2)
    
    WCS <- read.csv("cnum-vhcm-lab-new.txt", sep = "\t", header = T)
    WCS$MunH <- gsub('.50','.5', WCS$MunH)  # remove trailing "0" after ".50"
    WCS$MunH <- gsub('.00',  '', WCS$MunH)  # remove ".00" altogether
    
    WCS$V <- factor(WCS$V)  # needed to flip the axis
    
    WCS$hex <- mnsl2hex(hvc2mnsl(hue = WCS$MunH, value = ceiling(WCS$MunV), chroma = WCS$C), fix = T)
    
    ggplot(aes(x=H, y=V, fill=hex), data = WCS) +   
      geom_tile(aes(x=H, y=V), show.legend = F, width=0.8, height=0.8) +
      scale_y_discrete(limits = rev(levels(WCS$V))) +  # flipping the axis
      scale_x_continuous(breaks = scales::pretty_breaks(n = 40)) +
      coord_fixed() + # force all tiles to be "square"
      theme(
        panel.grid = element_blank()
      )
    

    你有show.legend = F,但应该有324个不同的值映射到WCS$hex列(即length(unique(WCS$hex)))。

    使用scale_fill_manual(values=...) 时,您提供了要使用的颜色名称,但它们并未映射到WCS$hex 列中的相同位置。它们是根据ggplot2 决定组织WCS$hex 的级别的方式应用的,就好像它是一个因素一样。

    为了告诉ggplot2 基本上忽略映射并仅根据您在映射到fill 的列中看到的实际颜色名称​​着色,您使用scale_fill_identity()。这必然会删除显示任何图例的能力,因为它会删除aes(fill=...) 的默认行为的映射和重新着色。无论如何,这应该可以解决您的问题:

    ggplot(aes(x=H, y=V, fill=hex), data = WCS) +   
      geom_tile(aes(x=H, y=V), width=0.8, height=0.8) +
      scale_fill_identity() +   # assign color based on text
      scale_y_discrete(limits = rev(levels(WCS$V))) +  # flipping the axis
      scale_x_continuous(breaks = scales::pretty_breaks(n = 40)) +
      coord_fixed() + # force all tiles to be "square"
      theme(
        panel.grid = element_blank()
      )
    

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 2021-08-21
      相关资源
      最近更新 更多