I'm trying to write the symbol degrees celsius with R/Plotly in one of my titles. It works when I just use a simple plot a below:
# Working code
library(latex2exp)
set.seed(1)
betas <- rnorm(1000)
hist(betas, main = TeX("Temperature (^0C)"))
However, when I try to run the code through plotly, I get the following error: "unimplemented type 'expression' in 'HashTableSetup'".
#Initialise the plot
p <- plot_ly()
#Add axis names
#Font
f <- list(
family = "Courier New, monospace",
size = 18,
color = "#7f7f7f")
#X axis name
x <- list(
title = "x Axis",
titlefont = f)
#Y Axis name
y <- list(
title = TeX("Temperature (^0C)"),
titlefont = f)
#Add layout
p <- p %>%
layout(xaxis = x, yaxis= y)
p
Any ideas?
Try,
title = "Temperature (\u00B0C)"
Try title = expression("Temperature ("*~degree*C*")") or title = "Temperature (°C)"
I just found a hacky solution: Look for the special character on google and copy and paste it directly in the R code.
#Y Axis name
y <- list(
title = "Temperature (°C)",
titlefont = f)
I'm still interested in a less hacky solution which allows to insert LaTeX into Plotly.
Related
I have a surface I want to display with plotly (it has to be a surface and not a 3D mesh because a 3D mesh is not appropiate so I must pass on a matrix not a data.frame).
For this I want to control the hoverinfo so it won't display the x, y, z coordinated but it does display x * y = ... and x + y = .... However I am encountering a problem with the hovertext argument. Which correctly displays the things I want but also displays the x, y, z coordinates. Also, I can't get "<extra></extra>" to work.
This is attempt so far:
library(plotly)
mat <- 0:10 %*% t(0:10)
plot_ly() |>
add_surface(z = ~mat,
hovertext = paste(
"X*Y:", mat,
"<br>X+Y: ", rep(0:10, each = 11) + rep(0:10, 11),
"<extra></extra>") |>
matrix(ncol = 11),
hovertemplate = text)
I would like to know:
How can I remove the x, y, z coordinates from the hovertemplate?
How can I use "<extra></extra>" argument when using hovertemplate = text?
EDIT: Following #Kat's comment I edited this post to reflect what I learned. Thanks Kat!
Thank you for your help!
Instead of text, use hovertext.
library(plotly)
mat <- 0:10 %*% t(0:10)
plot_ly() |>
add_surface(z = ~mat,
hovertext = mat, # hovertext to use in the template
hovertemplate = paste0("X: %{x}<br>Y: %{y}<br>",
"Z: %{z}<br>Whatever: %{hovertext}",
"<extra></extra>"))
I'm creating a unique type of categorical scatter plot with plotly in R, where:
color is indicated by a continuous numeric variable
size is indicated by a continuous numeric
symbol is indicated by a categorical
both axis are categorical variables
It appears that calling "symbol" and "symbols" is affecting the color scale numbers as well as the proper location of markers.
Here is my demo code which includes hoverinfo so you can see that the "NA" for Top/C is not in the correct position.
library(plotly)
m <- data.frame(c('A','A','B','B','C','C'),
c('Top','Bottom','Top','Bottom','Top','Bottom'),
c('OK','NG','OK','OK','OK','NG'),
c(8,20,55,72,NA,43), # Top,C should be blank
c(4,5,6,7,8,9))
colnames(m) <- c("cat_1","cat_2","check","Bigness", "Twistiness")
color_scale <- list(c(0,'rgb(227,120,0)'),c(0.5,'rgb(220,220,220)') , c(1,'rgb(43,92,138)'))
p <- plot_ly(data = m,
x = ~cat_1,
y = ~cat_2,
type='scatter',
mode='markers',
symbol = ~check, ## problem line
symbols = c('square-open','square'), ## problem line
marker = list(color = ~Twistiness,
size= ~Bigness,
colorscale = color_scale,
colorbar = list(len = 0.8, y = 0.3)),
text = ~paste0('cat_1 (x): ', cat_1, '\n',
'cat_2 (y): ', cat_2, '\n',
'Big (size): ', Bigness, '\n',
'Twist (color): ', Twistiness, '\n')
) %>%
layout(margin = list(r = 8, t = 15, b = 50, l = 80),
# xaxis = list(title = ""),
yaxis = list(title = ""),
showlegend = TRUE)
p
Picture of executed code
It seems that plotly is the best way to add interactive hover as well as pass event_data to another code block in Shiny.
I have tried this via ggplotly, but the event_data is not correct and the legend cannot have custom positioning.
Here is some example code I wrote to illustrate my problem. Right now, the plot generates only the points. What I want to do is have horizontal lines that go through each point, spanning a length of 1 to each side. (i.e. for a point at (2,1) I want the line to run from (1,1) to (3,1)) How can I do this in plotly? I've looked here but can't seem to figure out how to get this to work when the y-axis is not numerical.
library(plotly)
p <- plot_ly(data = mtcars, x = ~mtcars$mpg, y = rownames(mtcars), type = 'scatter', mode = 'markers')
p
EDIT is the output from the code provided in the accepted answer (except showing all car names). I was wondering if there is a way to draw a horizontal line between each y-axis label, so that for example, between "Volvo 142E" and "Maserati Bora", a line separates them, and goes for the length of the plot. Right now, each horizontal line of the plot has a point on it. I want to separate each of those lines with another line.
To get this to work we had to replot the original scatter plot with an using the row index to prevent plotly from reordering cars. Then I added back to the axis labels.
library(plotly)
##I added the text argument(hover over text) so that I could make sure
##that the yaxis labels matched the points. feel free to delete.
p <- plot_ly(x = mtcars$mpg, y = seq_along(rownames(mtcars)), text=rownames(mtcars),
type = 'scatter', mode = 'markers')
##This sets some attributes for the yaxis. Note: in your origianl plot every other
##car name was shown on the y-axis so that is what I recreated but you could remove
##the "seq(1,32, by=2)" to show all car names.
ax <- list(
title = "",
ticktext = rownames(mtcars)[seq(1,32, by=2)],
tickvals = seq(1,32, by=2)
)
##This is taken from the plotly help page. y0 and y1 now are set to equal the row
##number and x0 and x1 are +/-1 from the car's mpg
line <- list(
type = "line",
line = list(color = "pink"),
xref = "x",
yref = "y"
)
lines <- list()
for (i in seq_along(rownames(mtcars))) {
line[["x0"]] <- mtcars$mpg[i] - 1
line[["x1"]] <- mtcars$mpg[i] + 1
line[c("y0", "y1")] <- i
lines <- c(lines, list(line))
}
p <- layout(p, title = 'Highlighting with Lines', shapes = lines, yaxis=ax)
p
Update based on OP's request.
To underline text in plotly use HTML tags. But <u> </u> does not work so we have to use <span> </span>...
ax2 <- list(
title = "", ticktext = paste0('<span style="text-decoration: underline;">',
rownames(mtcars)[1:32 %% 2 ==0],"</span>"),
tickvals = seq(1,32, by=2), style=list(textDecoration="underline"))
Here is some example code to illustrate my issue.
library(plotly)
p <- plot_ly(x = mtcars$mpg, y = seq_along(rownames(mtcars)), text=rownames(mtcars),
type = 'scatter', mode = 'markers')
ax <- list(
title = "",
ticktext = rownames(mtcars),
tickvals = seq(1,32)
)
line <- list(
type = "line",
line = list(color = "pink"),
xref = "x",
yref = "y"
layer = 'below'
)
lines <- list()
for (i in seq_along(rownames(mtcars))) {
line[["x0"]] <- mtcars$mpg[i] - 1
line[["x1"]] <- mtcars$mpg[i] + 1
line[c("y0", "y1")] <- i
lines <- c(lines, list(line))
}
p <- layout(p, title = 'Highlighting with Lines', shapes = lines, yaxis=ax)
p
I would like to add horizontal lines through the plot to separate each y-axis label. I would prefer the line split the labels as well as the graph, but splitting just the graph would suffice. I have looked extensively through the plotly reference but have yet to find anything that appears to help. I was told that there might be some sort of solution through some custom JS in the y-axisof the layout section, but am unsure on how I would go about this / am not very JS savvy.
I was able to accomplish this by adding more lines to the lines list. Under the for loop shown above, if you add the code:
for (i in seq_along(rownames(mtcars))) {
line[["x0"]] <- 10
line[["x1"]] <- 35
line[c("y0", "y1")] <- i-.5
lines <- c(lines, list(line))
}
It will place a separating line in between each of the data lines.
I am attempting to use subplots with the plot.ly R library for interactive online charting. I can successfully create a subplot, however am struggling to only have a single y-axis that is common to both charts.
The plot.ly website does provide an example for a common x-axis, however this is done slightly differently using and additional trace rather than the group option that is provided within the plot_ly() function.
example code:
library(data.table)
library(plotly)
dt <- data.table(x = c("A","B","C","D","A","B","C","D"),
y = c(12,4,3,9,5,10,3,7),
group = factor(c(rep("G1",4),rep("G2",4))))
dt$id <- as.integer(dt$group)
xx <- xaxis_standard
yy <- yaxis_standard
p <- plot_ly(dt, x=x, y=y, group = group, xaxis = paste0("x",id))
p <- layout(p, yaxis = list(range = c(0, max(y))))
p <- subplot(p, margin = 0.05)
p <- layout(p,showlegend = F, yaxis = list(anchor = 'x1'))
p
This image shows what results when I execute the code.
What I would like to have is the same chart, however without the y-axis on the right hand subplot.
Subplots are on separate axes labeled xaxis2, yaxis2, etc. Those axes are also arguments to layout().
p <- layout(p, showlegend = F, yaxis = list(anchor = 'x1'),
yaxis2 = list(showticklabels = F))
p