I was able to do this as shown in some examples and wanted to do it plotly, anybody know how can I do it?
Hey @tarunparmar,
You can make a subplot with a histogram and horizontal boxplot and style the layouts:
d <- mtcars
# axis styles
ax <- list(
title = "",
zeroline = FALSE,
showline = FALSE,
showticklabels = TRUE,
showgrid = FALSE
)
aax <- list(
title = "",
zeroline = FALSE,
showline = FALSE,
showticklabels = FALSE,
showgrid = FALSE
)
# box on top / hist bottom
s <- subplot(
plot_ly(d, x = ~mpg, type = "box", name = "mpg"),
plotly_empty(),
plot_ly(d, x = ~mpg, type = "histogram", name = "mpg") %>%
layout(showlegend = FALSE, xaxis = ax, yaxis = ax),
nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2),
shareX = TRUE
)
# show box/hist subplot
layout(s)
# hist on top / box bottom
ss <- subplot(
plot_ly(d, x = ~mpg, type = "histogram", name = "mpg") %>%
layout(xaxis = ax, yaxis = ax),
plotly_empty(),
plot_ly(d, x = ~mpg, type = "box", name = "mpg") %>%
layout(xaxis = aax),
nrows = 2, heights = c(0.8, 0.2), widths = c(0.8, 0.2),
shareX = FALSE
)
# show hist/box subplot
layout(ss, title="b", showlegend=FALSE)
# show both as a subplot
p <- subplot(s,ss) %>%
layout(showlegend=FALSE)
the final subplot § will give you https://plot.ly/~bdun9/761/mpg-mpg-mpg-mpg/
There is some docs on the plotly website https://plot.ly/r/subplots/. If you’re looking for something more dynamical - that is in the works https://cpsievert.github.io/plotly_book/linking-views-without-shiny.html#dynamic-aggregates
Cheers,
Branden
Worked perfectly…thanks a lot
ran into another issue…trying to assign a variable name to the title of the axes and keep getting errors.
something like this:
x_var <- "Random_Title"
x_axis_val <- list(title = x_var)
y_axis_val <- list(title = “Counts”)
histogram <- plotly(dt, x = ~Col1, type = “histogram”) %>% layout(xaxis=x_axis_val , yaxis = y_axis_val)
If you’re referring to when making a subplot then you need:
subplot(s, ss, titleX=TRUE, titleY=TRUE)
There is an example of using titleX & Y here https://plot.ly/r/text-and-annotations/#subplot-annotations.
If it is the code above there is a syntax error: plotly
ought to be plot_ly
Hi Branden,
Thanks for helping out.
I made a typo in writing it, I have it as plot_ly in my code…
What seem to have worked is I removed underscores from the axis variable names and also added “titlefont = f”, this seems to have made the variable a list, otherwise with just “title = x_var” it wasnt considering it as list. Weird but thats what I noticed.
So now I am able to display the selected x_var as title of xaxis but it is not showing yaxis for some reason.
Sorry to keep coming back with petty issues.
x_var <- as.character(input$hist_var)
grp_var <- as.character(input$hist_splitvar)
grpitem_var <- as.character(input$hist_splitvaritem)
newdt <- dplyr::filter(dt,dt[,get('grp_var')]==grpitem_var)
xhp <- list(title = grpitem_var,titlefont = f)
yhp <- list(title = "Counts",titlefont = f)
ybp <- list(title = grp_var,titlefont = f)
histogram <- plot_ly(newdt,x = ~newdt[,get('x_var')],type="histogram") %>%
layout(xaxis = xhp, yaxis = yhp)
boxplot <- plot_ly(newdt,x =~newdt[,get('x_var')], type = "box", boxpoints = "outliers", jitter = 0.3) %>%
layout(yaxis = ybp)
subplot(boxplot,histogram,nrows = 2, shareX = TRUE) %>% layout(showlegend = FALSE)