[英]Colour based on x axis labels
我想根据y-axis
每列着色(使用彩虹前 7 种颜色)。 例如,在y-axis
label A
我想交换Z4A8A08F09D37B73795649038408B5F333Z BOX绿色至'#FF000'
"#9400D3"
,类似于G
。 为丢失的样本数据道歉,因为我的 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 the
dput 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"))
感谢您提供数据,但与提供前 5 行相比,提供数据集的前 50 行并没有那么有用,因为在此期间每个df3$index
的df3$value
的值不会改变。
在这种情况下,我正在获取您的数据并对其进行加扰,以便它可以成为您数据的更具代表性的示例:
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 个彩虹 colors 的集合(例如rainbow(7)
)。 这里的问题是 OP 已经根据df3$value
对每个图块进行了颜色编码。 由于 OP 没有具体说明他们设想的着色方式,我将假设 OP 想要:
df3$value == "Y"
的任何图块,填充颜色应对应于用于 label y 轴 ( df3$index
) 的彩虹色。df3$value == "N"
的任何图块,填充颜色应该是一些中性颜色(灰色?)或根本不着色。 基本上,我们想要 map fill = df3$index
,然后使用命名向量指定 colors 以填充scale_fill_manual()
。 我们只想在df3$value == "Y"
时将 map 填充到df3$index
。 最简单的方法是将geom_raster()
应用于df3
的子集,其中df3$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")
)
请注意,我在上面的 plot 代码中注释掉了另一个geom_raster()
调用。 如果取消注释该行,它将首先为任何具有df3$value == "N"
的图块绘制灰色背景。 归根结底,这是一种审美选择。
对 OP 的一个一般说明是,通过theme(...
对主题元素进行的所有修改通常应该 go 一起。严格来说,你不必这样做,但这是更好的做法,你会在第二个 plot 上面的代码,我把所有的元素都收集在一起了。有重复,所以需要清理很多。
可能存在一个问题,即如何显示更多“灰色表示值等于 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")
)
请注意,在第一个geom_raster()
的aes()
中将“无值”放在引号中会将填充 label 分配给"No Value"
并将其放在图例中。 然后我需要在scale_fill_manual()
调用中指定这个值应该是灰色的。 最后,创建的图例不会正确 map alpha 的美学以匹配字形,所以我包括了guides(...
部分,我使用override.aes()
来修复最终灰色字形的 alpha 以具有适当的阿尔法设置。
过于复杂的答案? 当然,但我这样做部分是为了向 OP 展示他们如何调整 plot 的一些建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.