首页 > 解决方案 > 基于 x 轴标签的颜色

问题描述

我想根据y-axis配色方案为每列着色(使用彩虹前 7 种颜色)。例如,在y-axis标签的情况下,A我想将 c 框绿色交换为"#9400D3",类似地,在G标签的情况下,我会将绿色交换为'#FF000'。为丢失的样本数据道歉,因为我的 dput 太大。欢迎任何帮助。

示例代码:

ggplot(df3, aes(x = time, y = index, fill = value))+
  theme_bw()+ 
  scale_x_continuous(breaks =  c(4,8,12,16,20,24),expand = c(-0, 0), name= "")+ 
  geom_raster() +
  facet_grid(~ day)+
  theme(plot.title = element_text(hjust = 0.5)) + 
  theme(legend.title = element_text(family="Times",color = "black", size = 16, face="bold"),
        legend.text = element_text(family="Times", color = "black", size = 16,face="bold"),
        legend.position="top",
        plot.title = element_text(hjust = 0.5))+
  theme(panel.spacing = unit(1, "mm"),  axis.text.x = element_text(hjust = 1)) + 
  scale_fill_manual(values=c("yellow", "green"))+  
  labs(x="Time", y ="", fill="Legend:")+ 
  theme(axis.text.x = element_text( hjust = 1,family="Times", face="bold", size=12,  color="black"), 
        axis.title.x = element_text(family="Times", face="bold", size=16, color="black"),
        axis.text.y = element_text(family="Times", face="bold", size=12,  color=colour),strip.text = element_text(size=12, face="bold"), 
        axis.title.y = element_text(family="Times", face="bold", size=16, color="black")
  )

样本数据:My dput is too large to copy so I provide thedput head

structure(list(X1 = c(1, 2, 3, 4, 5, 6), index = structure(1:6, .Label = c("A", 
"B", "C", "D", "E", "F", "G"), class = "factor"), variable = c("Monday_04:00", 
"Monday_04:00", "Monday_04:00", "Monday_04:00", "Monday_04:00", 
"Monday_04:00"), value = c("N", "N", "N", "N", "Y", "N"), day = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("a", "b", "c", "d", "e", "f", 
"g"), class = "factor"), time = c(4, 4, 4, 4, 4, 4)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

前50个案例:

structure(list(X1 = c(2342, 4499, 3761, 1282, 1836, 2696, 2226, 
2280, 3956, 3147, 3371, 2596, 2142, 3824, 3627, 2611, 3778, 3401, 
582, 4180, 2521, 2541, 2422, 118, 1556, 4702, 2877, 3458, 1714, 
1040, 639, 1907, 2356, 318, 4312, 332, 2234, 3766, 4575, 3933, 
1083, 1411, 3942, 3626, 3130, 2403, 837, 1540, 4566, 4262), index = structure(c(4L, 
5L, 2L, 1L, 2L, 1L, 7L, 5L, 1L, 4L, 4L, 6L, 7L, 2L, 1L, 7L, 5L, 
6L, 1L, 1L, 1L, 7L, 7L, 6L, 2L, 5L, 7L, 7L, 6L, 4L, 2L, 3L, 4L, 
3L, 7L, 3L, 1L, 7L, 4L, 6L, 5L, 4L, 1L, 7L, 1L, 2L, 4L, 7L, 2L, 
6L), .Label = c("A", "B", "C", "D", "E", "F", "G"), class = "factor"), 
    variable = c("Thursday_15:30", "Sunday_20:30", "Saturday_18:15", 
    "Tuesday_01:45", "Wednesday_21:30", "Friday_04:15", "Thursday_11:15", 
    "Thursday_13:15", "Saturday_01:15", "Friday_20:15", "Saturday_04:15", 
    "Thursday_00:30", "Thursday_08:15", "Saturday_20:30", "Saturday_13:30", 
    "Thursday_01:00", "Saturday_18:45", "Saturday_05:15", "Monday_00:45", 
    "Sunday_09:15", "Thursday_22:00", "Thursday_22:30", "Thursday_18:15", 
    "Monday_08:00", "Wednesday_11:30", "Sunday_03:45", "Friday_10:30", 
    "Saturday_07:15", "Wednesday_17:00", "Tuesday_17:00", "Monday_02:45", 
    "Wednesday_00:00", "Thursday_16:00", "Monday_15:15", "Sunday_13:45", 
    "Monday_15:45", "Thursday_11:45", "Saturday_18:15", "Sunday_23:15", 
    "Saturday_00:15", "Tuesday_18:30", "Wednesday_06:15", "Saturday_00:45", 
    "Saturday_13:15", "Friday_19:45", "Thursday_17:45", "Tuesday_09:45", 
    "Wednesday_10:45", "Sunday_23:00", "Sunday_12:00"), value = c("Y", 
    "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "Y", 
    "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "Y", 
    "Y", "N", "N", "Y", "N", "N", "N", "Y", "N", "Y", "N", "N", 
    "N", "N", "N", "N", "N", "N", "N", "N", "N", "Y", "Y", "N", 
    "N"), day = structure(c(4L, 7L, 6L, 2L, 3L, 5L, 4L, 4L, 6L, 
    5L, 6L, 4L, 4L, 6L, 6L, 4L, 6L, 6L, 1L, 7L, 4L, 4L, 4L, 1L, 
    3L, 7L, 5L, 6L, 3L, 2L, 1L, 3L, 4L, 1L, 7L, 1L, 4L, 6L, 7L, 
    6L, 2L, 3L, 6L, 6L, 5L, 4L, 2L, 3L, 7L, 7L), .Label = c("a", 
    "b", "c", "d", "e", "f", "g"), class = "factor"), time = c(15.5, 
    20.5, 18.25, 25.75, 21.5, 4.25, 11.25, 13.25, 25.25, 20.25, 
    4.25, 24.5, 8.25, 20.5, 13.5, 25, 18.75, 5.25, 24.75, 9.25, 
    22, 22.5, 18.25, 8, 11.5, 27.75, 10.5, 7.25, 17, 17, 26.75, 
    24, 16, 15.25, 13.75, 15.75, 11.75, 18.25, 23.25, 24.25, 
    18.5, 6.25, 24.75, 13.25, 19.75, 17.75, 9.75, 10.75, 23, 
    12)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", 
"data.frame"))

在此处输入图像描述

标签: rdataframeggplot2

解决方案


为代表加扰 OP 的共享数据集

感谢您提供数据,但与提供前 5 行相比,提供数据集的前 50 行并没有那么有用,因为在此期间df3$valuefor each的值df3$index不会改变。

在这种情况下,我正在获取您的数据并对其进行加扰,以便它可以成为您数据的更具代表性的示例:

set.seed(1980)
df3$value <- df3$value[sample(1:50, 50, replace=T)]

# then run OP's plot code posted in the question

在此处输入图像描述

重新着色瓷砖

OP 希望根据 y 轴的值对每个图块进行颜色编码 - 并将颜色与 y 轴匹配,该轴使用 7 种彩虹色(例如rainbow(7))。这里的问题是 OP 已经根据df3$value. 由于 OP 没有具体说明他们设想的着色方式,我将假设 OP 想要:

  • 对于其中 的任何图块df3$value == "Y",填充颜色应对应于用于标记 y 轴 ( df3$index) 的彩虹色。
  • 对于任何 tile df3$value == "N",填充颜色应该是一些中性颜色(灰色?)或根本不着色。

基本上,我们想要映射fill = df3$index,然后scale_fill_manual()使用命名向量指定填充颜色。我们只想将 fill 映射到df3$indexwhen df3$value == "Y"。最简单的方法是应用于wheregeom_raster()的子集。df3df3$value == "Y"

color_vector <- rainbow(7)
names(color_vector) <- LETTERS[1:7]

ggplot(df3, aes(x = time, y = index, fill = index))+
  theme_bw()+
  scale_x_continuous(breaks =  c(4,8,12,16,20,24),expand = c(-0, 0), name= "")+
  # geom_raster(fill='gray', alpha=0.2) +
  geom_raster(data=subset(df3, value=='Y')) +
  facet_grid(~ day) +
  scale_fill_manual(values=color_vector) +
  labs(x="Time", y ="", fill="Legend:")+
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.spacing = unit(1, "mm"),
    legend.title = element_text(family="Times",color = "black", size = 16, face="bold"),
    legend.text = element_text(family="Times", color = "black", size = 16,face="bold"),
    legend.position="top",
    axis.text.x = element_text( hjust = 1,family="Times", face="bold", size=12,  color="black"),
    axis.title.x = element_text(family="Times", face="bold", size=16, color="black"),
    axis.text.y = element_text(family="Times", face="bold", size=12,  color=rainbow(7)),
    strip.text = element_text(size=12, face="bold"),
    axis.title.y = element_text(family="Times", face="bold", size=16, color="black")
  )

在此处输入图像描述

请注意,我geom_raster()在上面的绘图代码中注释掉了另一个调用。如果您取消注释该行,它将首先为任何具有df3$value == "N". 归根结底,这是一种审美选择。

一些美学修改

对 OP 的一个一般说明是,对主题元素的所有修改theme(...通常应该一起进行。严格来说,您不必这样做,但这是更好的做法,您会在上面的第二个绘图代码中注意到,我已将所有元素收集在一起。有重复,所以必须清理很多。

可能存在一个问题,即如何显示更多“灰色表示值等于 N”,而彩色填充仅显示索引。有几种方法可以解决这个问题,但我将展示 OP 可以在此处使用的一种可能方式。

ggplot(df3, aes(x = time, y = index, fill = index))+
  theme_bw()+
  scale_x_continuous(breaks =  c(4,8,12,16,20,24),expand = c(-0, 0), name= "")+
  geom_raster(aes(fill='No Value'), alpha=0.2) +
  geom_raster(data=subset(df3, value=='Y')) +
  facet_grid(~ day) +
  scale_fill_manual(values=c(color_vector, 'No Value'="gray")) +
  labs(x="Time", y ="", fill="Legend:")+
  guides(fill=guide_legend(override.aes = list(alpha=c(1,1,1,1,1,1,1,0.2)))) +
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.spacing = unit(1, "mm"),
    legend.title = element_text(family="Times",color = "black", size = 16, face="bold"),
    legend.text = element_text(family="Times", color = "black", size = 16,face="bold"),
    legend.position="right",
    axis.text.x = element_text( hjust = 1,family="Times", face="bold", size=12,  color="black"),
    axis.title.x = element_text(family="Times", face="bold", size=16, color="black"),
    axis.text.y = element_text(family="Times", face="bold", size=12,  color=rainbow(7)),
    strip.text = element_text(size=12, face="bold"),
    axis.title.y = element_text(family="Times", face="bold", size=16, color="black")
  )

在此处输入图像描述

请注意,将“No Value”放在aes()第一个引号中,geom_raster()会将填充标签分配给"No Value"图例并将其放置在图例中。然后我需要指定这个值在scale_fill_manual()调用中应该是灰色的。最后,创建的图例无法正确映射 alpha 的美学以匹配字形,因此我包含了guides(...部分,我曾经override.aes()在其中修复最终灰色字形的 alpha 以获得正确的 alpha 设置。

过于复杂的答案?当然,但我这样做部分是为了向 OP 展示他们如何调整情节的一些建议。


推荐阅读