Reputation: 21400
This is probably an easy question for ggplot2
experts: I want to use my own colors rather than the default colors. How to achieve that?
Here's a snippet of the data:
df <- structure(list(start = c(0, 251, 1976, 5127, 5717, 6783), end = c(251,
1976, 5127, 5717, 6783, 6830), minute = c(0L, 0L, 0L, 0L, 0L,
0L), AOI = c("*", "A", "*", "*", "A", "*"), AOI_col = c("blue",
"red", "blue", "blue", "red", "blue")), row.names = c(NA, -6L
), groups = structure(list(minute = 0L, .rows = structure(list(
1:6), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), row.names = 1L, class = c("tbl_df", "tbl", "data.frame"
), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
))
The colors I wish to plot are in column AOI_col
. Here's the code so far:
library(ggplot2)
ggplot(df5, aes(x = start, xend = end,
y = minute + scale(as.numeric(as.factor(AOI)))/10,
yend = minute + scale(as.numeric(as.factor(AOI)))/10,
color = AOI)) +
geom_segment(size = 2) +
scale_y_reverse(breaks = 0:53, labels = paste0(0:53, "min"), name = NULL) +
labs(title = "Gaze activity Speaker C F01") +
theme(axis.title.x.bottom = element_blank())
I've tried using aes(fill = AOI_col)
and scale_color_manual(values = AOI_col)
but to no avail. Help is appreciated!
Upvotes: 1
Views: 244
Reputation: 389
You can add a color adjustment in the geom_segment():
ggplot(df, aes(x = start, xend = end,
y = minute + scale(as.numeric(as.factor(AOI)))/10,
yend = minute + scale(as.numeric(as.factor(AOI)))/10)) +
geom_segment(size = 2, color = df$AOI_col) +
scale_y_reverse(breaks = 0:53, labels = paste0(0:53, "min"), name = NULL) +
labs(title = "Gaze activity Speaker C F01") +
theme(axis.title.x.bottom = element_blank())
Or again the complete example with the answer from @user438383:
df <-
structure(
list(
start = c(0, 251, 1976, 5127, 5717, 6783),
end = c(251,
1976, 5127, 5717, 6783, 6830),
minute = c(0L, 0L, 0L, 0L, 0L,
0L),
AOI = c("*", "A", "*", "*", "A", "*"),
AOI_col = c("blue",
"red", "blue", "blue", "red", "blue")
),
row.names = c(NA,-6L),
groups = structure(
list(
minute = 0L,
.rows = structure(
list(1:6),
ptype = integer(0),
class = c("vctrs_list_of", "vctrs_vctr",
"list")
)
),
row.names = 1L,
class = c("tbl_df", "tbl", "data.frame"),
.drop = TRUE
),
class = c("grouped_df", "tbl_df", "tbl", "data.frame")
)
library(ggplot2)
ggplot(df,
aes(
x = start,
xend = end,
y = minute + scale(as.numeric(as.factor(AOI))) / 10,
yend = minute + scale(as.numeric(as.factor(AOI))) / 10,
color = AOI
)) +
geom_segment(size = 2) +
scale_y_reverse(breaks = 0:53,
labels = paste0(0:53, "min"),
name = NULL) +
labs(title = "Gaze activity Speaker C F01") +
theme(axis.title.x.bottom = element_blank()) +
scale_colour_manual(values = unique(df$AOI_col))
Upvotes: 1
Reputation: 6206
I think the ideal thing to do is make AOI_col
a factor and sort it alphabetically, then assign the colours to be the unique values of that column:
df5$AOI_col = factor(df5$AOI_col, levels = sort(unique(df$AOI_col)))
ggplot(df5, aes(x = start, xend = end,
y = minute + scale(as.numeric(as.factor(AOI)))/10,
yend = minute + scale(as.numeric(as.factor(AOI)))/10,
color = AOI)) +
geom_segment(size = 2) +
scale_y_reverse(breaks = 0:53, labels = paste0(0:53, "min"), name = NULL) +
labs(title = "Gaze activity Speaker C F01") +
theme(axis.title.x.bottom = element_blank()) +
scale_colour_manual(values = unique(df$AOI_col))
Upvotes: 1