user113156
user113156

Reputation: 7107

two different scale_colour_gradient2() for different lines in ggplot2

Base Model: https://www.dropbox.com/s/jhp2g4n89grhwi6/pr_base.RData?dl=0

Opt Model: https://www.dropbox.com/s/0mdupo1ryi4a7o3/pr.RData?dl=0

I have saved them as (base model = pr_base) and (optimial model = pr) - pr being precision recall.

I have plotted them as a ggplot using the following code:

pr <-  readRDS("pr.rds")
pr_base <- readRDS("pr_base.rds")



ggplot() +
  geom_line(data = data.frame(pr$curve), aes(x = X1, y = X2, color = X3)) +
  geom_line(data = data.frame(pr_base$curve), aes(x = X1, y = X2, color = X3)) +
  labs(x = "r",y = "p", title = "prc", colour = "legend") +
  scale_colour_gradient2(low = "white", mid = "grey", high = "black")

What I am trying to do is to reverse the scales / colours for the two models, to have the first geom_line as a white to black scale and the second geom_line as a black to white scale in order to differenciate them. (ignore the AUPRC in the image). At the moment they are both white - black scales.

enter image description here

Upvotes: 0

Views: 410

Answers (1)

dww
dww

Reputation: 31452

This is a bit of a hack. And I'm not even sure its a good idea to present your data like this - it somewhat goes against the principle that visualizations should try to represent the data as clearly as posible. Its probably worth thinking about another way to represent your data. Anyway...

For the second line mapping you can use color = 1-X3 to reverse the color mapping. Then you can add a 2nd colorbar legend using a fill aesthetic. We apply the fill aesthetic to a dummy geom_bar that has zero height bars, because unless you use the aesthetic, it will not show in the legend

d1 = data.frame(X1 = 0:5, X2=0:5, X3=(0:5)/5)
d2 = data.frame(X1 = 0:5, X2=1:6, X3=(0:5)/5, Xfake = 0)

ggplot(mapping =  aes(X1, X2)) +
  geom_line(data = d1, aes(color = X3)) +
  geom_line(data = d2, aes(color = 1-X3)) +
  geom_bar(data = d2, mapping = aes(y=Xfake, fill = X3), stat = 'identity') +
  scale_colour_gradient(name = 'trace1', low = "white", high = "black") +
  scale_fill_gradient(name = 'trace2', low = "black", high = "white")

enter image description here

Upvotes: 1

Related Questions