Categorical axes in plotly heatmapgl - r

Hi I've some problems with heatmapgl plot (but with heatmap I get the same) of plotly package.
I've a distance matrix between a series of elements. I would like to plot a heatmap with names of elements on axes and possibly also on hover text labels.
library(plotly)
values=c(22.63409, 323.93780, 366.31498, 467.94524, 461.93251, 643.61306, 665.03471, 482.49047, 500.06208, 662.71600, 673.29995, 567.99427, 558.41419, 632.86704, 615.55000, 508.01659, 462.50232, 411.39160, 399.00598, 286.55681, 268.69871)
z_matrix=matrix(NA,7,7)
z_matrix[lower.tri(z_matrix, diag=FALSE)] <- values
elements_names=c("ATHOTPlun04", "ATHOTPlun10", "ATHOTPlun16", "ATHOTPlun22", "ATHOTPmar04", "ATHOTPmar10", "ATHOTPmar16")
q <- plot_ly(
x=elements_names,
y=elements_names,
z = z_matrix, type = "heatmapgl") %>%
layout(title = 'Distances for ATHOTP route')
q
Although this is what I get when I run the code:
I tried also to set axes type to "category" in layout options but nothing seems to change. What am I missing? Thank you!

heatmap-gl is used for images where you have continuous x- and y-axes. Therefore you need to change type to a non-continuous type, e.g. type = "heatmap".
q <- plot_ly( x=elements_names,
y=elements_names,
z = z_matrix, type = "heatmap") %>%
layout(title = 'Distances for ATHOTP route')
q

Related

Change colors for NA values to gray in R plotly scatter 3d

I'm trying to generate a plot in which I want to show NA values in translucent gray, and a set of numeric values I want to plot with a blue-red color scale. By default, plotly colors my NAs with a dark gray solid color, but I figure there must be a way to change that somehow. Is there anyway to do it? Here is a mockup of my code:
library("plotly")
blue_red <- rev(c("#A50026", "#D73027", "#F46443", "#FDAE61", "#FEE090"
, "#E0F3F8", "#ABD9E9", "#74ADD1", "#4575B4", "#313695"))
mtcars$brand <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
mtcars[mtcars$brand=="Merc","mpg"] <- NA
fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec,
marker = list(size = 6),
color=~mpg,
colors = blue_red,
type = "scatter3d",
mode = "markers")
fig
I found a somewhat similar question here: https://community.plotly.com/t/how-to-change-colors-for-na-values-to-gray-in-a-choropleth-map/15746/3 but I haven't been able to translate that to solve my problem.
Edited to make sense OK, so you have known triplets for x,y,z coordinates and you want to assign colors to various values of mpg. Can you simply do the following?
mtcars[mtcars$brand=="Merc","mpg"] <- your_chosen_value and assign a desired color to that value.
If that distorts the colors mapped to numeric values that are genuine, maybe it's simpler to plot all the non-"Merc" data with one call and then plot all the "Merc" data with a second call that uses a simple, single-value color assignment instead of your blue_red vector.

R: 1 Dimensional "scatterplot"

I am using the R programing language. Recently, I came across this previous stackoverflow post where it describes how to make a 1 dimensional scatter plot in R: How can I plot a 1-D plot in R?:
x <- rnorm(100,10,10)
x <- data.frame(x,1) ## 1 is your "height"
plot(x, type = 'o', pch = '|', ylab = '')
My question: is it possible to transform the above plot into a "plotly" plot?
Suppose I have the following data:
library(plotly)
x <- rnorm(100,10,10)
color <- rnorm(100, 2,1)
frame = data.frame(x,color)
Would it be possible to do something like this?
fig <- plot_ly(data = frame, x = ~frame$x, color ~ frame$color )
fig
I get the following error when running this code:
No trace type specified:
Based on info supplied, a 'histogram' trace seems appropriate.
Can someone please show me how to do this?
Thanks
Source: https://plotly.com/r/line-and-scatter/
In plotly language, a trace is the type of visualization that you would like to use to display your data. So the error basically lets you know that you have not specified any trace and that the program is picking one for you: "a histogram". For scatterplots, you need type = "scatter" and mode = "markers'.
Also, inside the plot_ly() function, once you specify the data argument, you can simply access the columns with the column name preceded by a tilde ~.
Finally, since you want a one dimensional scatterplot along the x-axis, you need to add y = " " to the plot_ly() function.
This is how you can achieve your desired result:
library(plotly)
x <- rnorm(100,10,10)
color <- rnorm(100, 2,1)
frame = data.frame(x,color)
plot_ly(type = "scatter", mode = "markers", data = frame, x = ~x, y = " ", color = ~color )
Note that plotly is a very rich framework and you can read the appropriate documentation to learn how to customize your plot to your liking.

R plotly - get bin sizes / breaks

given
library(plotly)
plot_ly(data = wind, x = ~r, type = "histogram")
how do I obtain the sizes of bins/breaks from the default algorithm? I checked the plotly object, I couldn't find the info. It is probably rendered on the JS side?
The output should be in sense of
hist(wind)$breaks

How to plot multiple y axes with plotly in R for loop

I'm trying to use a for loop to define multiple y axes in plotly graph in R. Below is an example. Does anyone have any suggestions or experience with getting something like this to work?
#example of dataset of plotly to plot multiple y axes in for loop
library(plotly)
len = length(names(mtcars))
names = names(mtcars)
n=0
p= plot_ly()
for (i in names){
n=n+1
p=add_trace(p,data = mtcars, x = mtcars[,"mpg"], y = mtcars[,i], yaxis = paste0('y',ifelse(n==1,'',n)), type = 'scatter', mode = 'scatter')
}
n=0
for (i in 1:len)
{n=n+1
layout(p,assign(paste0('yaxis', ifelse(n==1,"",n)),list( overlaying = "y", side = ifelse((n %% 2) == 0,'right','left'), title = paste0(n, " y axis")
)) )
}
p
I'm thinking that the assign with the multiple paste0's may be causing alot of problems with this. Is there any other way you all can think of to accomplish this? I'm trying to incorporate this into a shiny app to plot functions based on user input. So an lapply() or for loop is ideal. I'm not sure if I'll be able to predefine the yaxes so that all selected variables are plotted. Any help is greatly appreciated. Is there any instance of setting yaxes like this in R? It must be possible right?

Creating Hexbins with Dates in R hexbin()

I am trying to create hexbins where the x-axis is a date using the hexbin function in the hexbin package in R. When I feed in my data, it seems to convert the dates into a numeric, which gets displayed on the x-axis. I want it force the x-axis to be a date.
#Create Hex Bins
hbin <- hexbin(xData$Date, xData$YAxis, xbins = 80)
#Plot using rBokeh
figure() %>%
ly_hexbin(hbin)
This gives me:
Here's a brute force approach using the underlying grid plotting package. The axes are ugly; maybe someone with better grid skills than I could pretty them up.
# make some data
x = seq.Date(as.Date("2015-01-01"),as.Date("2015-12-31"),by='days')
y = sample(x)
# make the plot and capture the plot
p <- plot(hexbin(x,y),yaxt='n',xaxt='n')
# calculate the ticks
x_ticks_date <-
x_ticks <- axTicks(1, log = FALSE, usr = as.numeric(range(x)),
axp=c(as.numeric(range(x)) ,5))
class(x_ticks_date) <- 'Date'
y_ticks_date <-
y_ticks <- axTicks(1, log = FALSE, usr = as.numeric(range(y)),
axp=c(as.numeric(range(y)) ,5))
class(y_ticks_date) <- 'Date'
# push the ticks to the view port.
pushViewport(p$plot.vp#hexVp.off)
grid.xaxis(at=x_ticks, label = format(y_ticks_date))
grid.yaxis(at=y_ticks, label = format(y_ticks_date))

Resources