Home > Blockchain >  How to change a certain point on a scatterplot in Plotly a different colour to rest of data
How to change a certain point on a scatterplot in Plotly a different colour to rest of data

Time:11-23

enter image description here

I am using Plotly to plot a scatterplot of GWAS data and want to highlight a certain point a different colour to the rest of the data. I have tried multiple times but unable to find away around this in Plotly. Any advice would be great please.

input data looks like this:

input data

fig <- fig %>% add_trace(data=data_1, x = ~BP, y = ~log, name = "data",  mode = "markers", type = "scatter",
                         y = c(117300000, 117900000), marker = list(size = 8, color = '#d62728'), 
                         x = c(117558703), y = c(19.75696195), marker = list(color = 'blue',size = 8), type = "scatter")
fig 

CodePudding user response:

One of the easiest ways is to create a variable to identify that specific point. I created sample data here and assigned a colour variable equal to 1 for the point I want in another color.

df = tibble(bp = round(rnorm(10,5,2),2),
            log = round(rnorm(10,6,1.5),2))
df$colour <- as.factor(ifelse(df$bp == 4.41,1 ,0))
fig <- plot_ly(data = df, x = ~bp, y = ~log, group_by = ~colour,marker = list(color = factor(df$colour,labels=c("red","purple"))))  %>%
add_trace(data = df, x = ~bp, y = ~log, mode = 'markers', type = 'scatter')
fig

enter image description here

  • Related