Dekike
Dekike

Reputation: 1294

How can I know the code for the colours I used in a ggplot to add a vertical line with this same colour?

I have a df like this:

set.seed(123)
df <- data.frame(Delay=rep(-5:6, times=8, each=1),
                 ID= rep(c("A","B","C","D"), times=1, each=24),
                 variable=rep(c("R2","SE"), times=4, each=12),
                 value=c(0.3,0.4,0.51,0.58,0.64,0.78,0.68,0.63,0.54,0.45,0.32,0.22,0.78,0.68,0.59,0.55,0.47,0.35,0.28,0.41,0.50,0.58,0.63,0.73,0.3,0.4,0.51,0.58,0.64,0.78,0.68,0.63,0.54,0.45,0.32,0.22,0.78,0.68,0.59,0.55,0.47,0.35,0.28,0.41,0.50,0.58,0.63,0.73,0.3,0.4,0.51,0.58,0.64,0.78,0.68,0.63,0.54,0.45,0.32,0.22,0.78,0.68,0.59,0.55,0.47,0.35,0.28,0.41,0.50,0.58,0.63,0.73,0.3,0.4,0.51,0.58,0.64,0.78,0.68,0.63,0.54,0.45,0.32,0.22,0.78,0.68,0.59,0.55,0.47,0.35,0.28,0.41,0.50,0.58,0.63,0.73))

df$ID <- as.factor(df$ID)
df$variable <- as.factor(df$variable)


Plot<-  ggplot(df[df$ID=="B",], aes(x=Delay, y=value, group=variable, colour=variable)) + 
  geom_point(size=1) + 
  geom_line () +
  theme_hc() +
  theme(legend.position="right") +
  labs(x= '\nDelay',y=expression(R^{2})) +
  guides(color=guide_legend(override.aes=list(fill=NA))) +
  scale_x_continuous(breaks=seq(-5,5,1)) + 
  scale_color_jco() 
Plot

I am plotting just data of B.

enter image description here

I would like to add a vertical for the minimum value of SE and a vertical line for the maximum value of R2. I would like that the lines had the same colour than the variable. However, I don't know how to do it. The colour of the vertical lines are black as you can see below, so I don't know how to indicate I want the specific colour I Used previously.

Plot <- Plot + geom_vline(xintercept = 0)
Plot

enter image description here

Does anyone know how add both vertical lines using the same colours that for the variables?

Upvotes: 2

Views: 1086

Answers (2)

Allan Cameron
Allan Cameron

Reputation: 174576

I think @r2evans approach to your specific problem is the correct one. However, to answer the more general question about how you can retrieve the colours from an applied colour scale (e.g. if you want to modify the colour etc), you can get it without going through ggbuild, using the following:

Plot$scales$get_scales("colour")$palette(2)
[1] "#0073C2FF" "#EFC000FF"

So we could do:

# Get colours
my_blue <- Plot$scales$get_scales("colour")$palette(2)[1]
my_yellow <- Plot$scales$get_scales("colour")$palette(2)[2]

# Get index of max R2 and min SE
maxR2 <- which.max(df$value[df$ID == "B" & df$variable == "R2"])
minSE <- which.min(df$value[df$ID == "B" & df$variable == "SE"])

# Get value of Delay at maxR2 and minSE
D_R2 <- df$Delay[df$ID == "B" & df$variable == "R2"][maxR2]
D_SE <- df$Delay[df$ID == "B" & df$variable == "SE"][minSE]

# Plot lines at the correct positions and with the desired colours
Plot + geom_vline(aes(xintercept = D_R2), colour = my_blue) +
       geom_vline(aes(xintercept = D_SE), colour = my_yellow)

enter image description here

Upvotes: 2

r2evans
r2evans

Reputation: 161085

You don't need to find the color to instruct ggplot2 to reuse it: you can supply "new data" with your desired x-intercept lines, and identify each v-line as belonging to a particular variable to use that variable's color.

I don't have your original Plot object or call, so my colors/theme will be different.

library(ggplot2)
ggplot(df, aes(Delay, value, color = variable)) +
  geom_line() +
  geom_vline(aes(xintercept = Delay, color = variable),
             data = data.frame(Delay = 0, variable = "R2"))

single vertical line

Or with multiple v-lines:

ggplot(df, aes(Delay, value, color = variable)) +
  geom_line() +
  geom_vline(aes(xintercept = Delay, color = variable),
             data = data.frame(Delay = c(-1, 1, 2), variable = c("R2", "SE", "R2")))

multiple vertical lines


This edit might answer this and your other question:

mins <- do.call(rbind, by(df, df[,c("ID", "variable")], function(z) z[which.min(z$value),]))
mins
#    Delay ID variable value
# 12     6  A       R2  0.22
# 36     6  B       R2  0.22
# 60     6  C       R2  0.22
# 84     6  D       R2  0.22
# 19     1  A       SE  0.28
# 43     1  B       SE  0.28
# 67     1  C       SE  0.28
# 91     1  D       SE  0.28

ggplot(df[df$ID == "B",], aes(Delay, value, color = variable)) +
  geom_line() +
  geom_vline(aes(xintercept = Delay, color = variable), data = mins)

more ggplot2

Or if you want to see multiple IDs, you can facet,

ggplot(df, aes(Delay, value, color = variable)) +
  geom_line() +
  geom_vline(aes(xintercept = Delay, color = variable), data = mins) +
  facet_wrap("ID")

faceted ggplot2

Upvotes: 2

Related Questions