Reputation: 2290
I would like to color (using the rainbow first 7 colours) each column based on y-axis
color scheme. For example in the case of y-axis
label A
I would like to swap the c box green colour to "#9400D3"
, similarly in the case of G
label I would swap the green colours to '#FF000'
. Apologise for the missing sample data as my dput was too large. Any help is welcomed.
Sample code:
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")
)
Sample data: My
dputis 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"))
The first 50 cases:
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"))
Upvotes: 0
Views: 73
Reputation: 13863
Thanks for the data, but giving the first 50 rows of your dataset is not all that more useful compared to giving the first 5, since the values of df3$value
for each df3$index
do not change during that time.
In this case, I'm taking your data and scrambling it so that it can be a more representative example of your data:
set.seed(1980)
df3$value <- df3$value[sample(1:50, 50, replace=T)]
# then run OP's plot code posted in the question
OP wanted to color code each tile according to the value of the y axis - and match the coloring to the y axis, which is using the set of 7 rainbow colors (ex. rainbow(7)
). The issue here is that OP is already color-coding each tile according to df3$value
. Since OP did not specify exactly how they envision the coloring to happen, I'm going to assume that OP wants:
df3$value == "Y"
, the fill color should correspond to the rainbow color used to label the y axis (df3$index
).df3$value == "N"
, the fill color should be some neutral color (gray?) or not colored at all.Basically, we want to map fill = df3$index
, and then specify the colors for fill in scale_fill_manual()
using a named vector. We only want to map fill to df3$index
when df3$value == "Y"
. The easiest way to do this is to apply geom_raster()
to the subset of df3
where 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")
)
Note that I have another geom_raster()
call commented out in the plot code above. If you uncomment that line, it will first draw a gray background for any tile that has df3$value == "N"
. Ultimately, that's an aesthetic choice.
One general note to the OP is that all modifications to the theme elements via theme(...
should customarily go together. You don't have to, strictly-speaking, but it is much better practice, and you'll note in the second plot code above, I've gathered all the elements together. There were duplicates, so a lot had to be cleaned up.
There may be a question of how you can show a bit more that "gray means value is equal to N" and colored fill is just showing the index. There's a few ways to approach this, but I'll show one possible way OP could use here.
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")
)
Note that putting "No Value" in quotes inside aes()
for the first geom_raster()
will assign that fill label to "No Value"
and place it inside the legend. I then needed to specify this values should be gray in the scale_fill_manual()
call. Finally, the legend created will not properly map the aesthetic for alpha to match the glyphs, so I included the guides(...
part, where I used override.aes()
to fix the alpha for the final gray glyph to have the proper alpha setting.
Overly complicated answer? Sure, but I'm doing this partly to demonstrate some suggestions for OP on how they may adjust the plot.
Upvotes: 1