0

I am trying to have a logarithmic scale for the color bar in R, any ideas on how I can do it?

My code:

TEST_DATAFRAME = read.table(TEST_FILE, sep="\t",skip=2, header=T)
PROD_DATAFRAME = read.table(PROD_FILE, sep="\t",skip=2, header=T)

PARAMETER = "Vf_High"
LST_RESIDUAL <- PROD_DATAFRAME[PARAMETER] - TEST_DATAFRAME[PARAMETER]
PARAM_DATAFRAME <- data.frame("NEW_MEASUREMENT" = TEST_DATAFRAME[PARAMETER], 
                              "OLD_MEASUREMENT" = PROD_DATAFRAME[PARAMETER], 
                              "RESIDUAL" = LST_RESIDUAL)

colnames(PARAM_DATAFRAME) <- c("NEW_MEASUREMENT","OLD_MEASUREMENT","RESIDUAL")

p <- plot_ly(PARAM_DATAFRAME, x = ~OLD_MEASUREMENT, y = ~NEW_MEASUREMENT, color=~RESIDUAL,  colorscale = "Log",
             hovertext = paste("<b>New Measurement :</b>", PARAM_DATAFRAME$NEW_MEASUREMENT,
                               "<br><b>Old Measurement :</b>", PARAM_DATAFRAME$OLD_MEASUREMENT,
                               "<br><b>Residual :</b>" , PARAM_DATAFRAME$RESIDUAL)) %>% add_markers()

#p <- layout(p, color = list(type = "log"))
p

this is my current output:

enter image description here

I also tried the following code:

TEST_DATAFRAME = read.table(TEST_FILE, sep="\t",skip=2, header=T)
PROD_DATAFRAME = read.table(PROD_FILE, sep="\t",skip=2, header=T)

PARAMETER = "Vf_High"
LST_RESIDUAL <- PROD_DATAFRAME[PARAMETER] - TEST_DATAFRAME[PARAMETER]
PARAM_DATAFRAME <- data.frame("NEW_MEASUREMENT" = TEST_DATAFRAME[PARAMETER], 
                              "OLD_MEASUREMENT" = PROD_DATAFRAME[PARAMETER], 
                              "RESIDUAL" = abs(LST_RESIDUAL))

colnames(PARAM_DATAFRAME) <- c("NEW_MEASUREMENT","OLD_MEASUREMENT","RESIDUAL")

brks <- pretty(range(PARAM_DATAFRAME$RESIDUAL))
gg <- ggplot(PARAM_DATAFRAME, aes(NEW_MEASUREMENT, OLD_MEASUREMENT, color = RESIDUAL)) +
  geom_point() +
  scale_colour_gradient(
    low = "blue", high = "red",
    trans = "log", breaks = brks, labels = brks) +
  theme_minimal()

ggplotly(gg)

this is the result:

enter image description here

How can I give it legible labelling? I want to configure the color bar as the following:

enter image description here

Adhil
  • 1,678
  • 3
  • 20
  • 31

1 Answers1

3

You could use ggplot to generate the logarithmic colour scale and then have plotly::ggplotly turn the ggplot grob into a plotly object.

Since you don't provide reproducible sample data, here is an example based on mtcars

brks <- pretty(range(mtcars$disp))
gg <- ggplot(mtcars, aes(mpg, wt, colour = disp)) +
    geom_point() +
    scale_colour_gradient(
        low = "blue", high = "red",
        trans = "log", breaks = brks, labels = brks) +
    theme_minimal()

ggplotly(gg)

enter image description here


Update

In response to your comment, here is a minimal reproducible example.

First we generate some sample data

set.seed(2018)
df <- data.frame(
    x = 1:100,
    y = 1:100 + rnorm(100),
    val = 10^seq(-5, 2, length.out = 100))

We then define suitable breaks

brks <- 10^seq(floor(log10(min(df$val))), ceiling(log10(max(df$val))), by = 1)

Generate ggplot grob

gg <- ggplot(df, aes(x, y, colour = val)) +
    geom_point() +
    scale_colour_gradient(
        low = "blue", high = "red",
        trans = "log",
        breaks = brks,
        labels = brks) +
    theme_minimal()

Finally show as plotly object

ggplotly(gg)

enter image description here


Without using ggplotly

To achieve the same without the ggplotly detour, use the colorbar argument within marker to define tick spacings and labels.

# Determine range of log10-scale
rg <- range(log10(df$val))

# Plot
plot_ly(
    df, 
    x = ~x, y = ~y, 
    type = "scatter",
    mode = "markers",
    marker = list(
        color = ~log10(val),
        colorbar = list(
            tickmode = "array",
            ticktext = 10^seq(floor(rg[1]), ceiling(rg[2])),
            tickvals = seq(floor(rg[1]), ceiling(rg[2])))))

enter image description here

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • I modified my answer, please review – Adhil Nov 29 '19 at 06:04
  • 1
    @Adhil Not clear what you're asking. Please make your example reproducible by including [minimal & reproducible data & code](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). We don't have access to either of those files, so don't have anything concrete to work with. – Maurits Evers Nov 29 '19 at 06:05
  • its a quite large Test Data file, most of the RESIDUALS are between 0 -0.01, however there are outlier residuals between 1 and 2, i want to show that in the color scale, however, i have difficulty implementing a logarithmic scale on the colour bar, the code you gave me works well, however, number labelling on the color bar is not clear, anyway I can clean it up? – Adhil Nov 29 '19 at 06:23
  • @Adhil So what kind of labels do you want? That shouldn’t be difficult to implement by specifying appropriate breaks. Can you give me some concrete numbers? – Maurits Evers Nov 29 '19 at 09:09
  • please refer to my updated query I have mentioned how I want the color bar to look. – Adhil Dec 02 '19 at 10:31
  • How to do this directly in plotly without ggplot? – kosk Dec 20 '21 at 12:44
  • 1
    @kosk Please see my latest edit. – Maurits Evers Dec 21 '21 at 02:06
  • Mauritis Evers, thank you. Is it possible to apply somehow this solution of yours for log to log and/sqrt transformation on a plotly map that I mention here: https://stackoverflow.com/questions/70404498/r-plotly-maping-issue ? Is it possible to sqrt/log transform somehow colors on a map but to keep the original values on the color-bar? Thanks – kosk Dec 21 '21 at 08:33
  • 1
    @kosk Yes I'm pretty sure that shouldn't be too difficult. I had a look at your post but it's missing some representative sample data. Without sample data it's difficult to demonstrate with a concrete example. – Maurits Evers Dec 21 '21 at 11:30
  • @MauritsEvers Here is the mock data with pretty much same issue if you could help: https://stackoverflow.com/questions/70415844/how-to-log-transform-values-for-color-in-plotly-but-to-keep-original-values-on-c?noredirect=1#comment124494859_70415844 – kosk Dec 21 '21 at 12:54
  • @kosk Agreed, this is exactly the same issue. I've posted an answer [here](https://stackoverflow.com/a/70443652/6530970). – Maurits Evers Dec 22 '21 at 02:33