How to change the position of plotly figures using ggplotly - r

I am trying to remove the legend title using ggplotly without success. I'm sure there is an easy fix, but I cannot find the documentation for it - and removing the legend title (or changing the positioning) using ggplot fails to work. See e.g.:
# Creating the ggplot:
a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T),
y = mpg,fill = interaction(cyl, carb, lex.order = T))) +
geom_boxplot() + theme(legend.title=element_blank())
a # No Legend Title
# plotly puts back the legend title
ggplotly(a)
Any ideas how to change / remove the title of the graph? Should it be done using ggplotly or ggplot?

You can use the labs function:
a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T),
y = mpg,fill = interaction(cyl, carb, lex.order = T))) +
geom_boxplot()
# update the legend
a <- a + labs(fill= "New Legend")
a
# to remove the label and update the axis texts use:
a <- a+labs(fill= "",x="Cyc/Carb",y="Miles/(US) gallon")
ggplotly(a)

Related

create single ggplot figure with a standalone ggplot and a ggplot created with facet_wrap()?

I have my an empty panel in my facetted ggplot. I would like to insert my standalone plot into this. Is this possible? See below for example code.
I found a possible solution Here, but can't get it to 'look nice'. To 'look nice' I want the standalone plot to have the same dimensions as one of the facetted plots.
library(ggplot2)
library(plotly)
data("mpg")
first_plot = ggplot(data = mpg, aes(x = trans, y = cty)) +
geom_point(size= 1.3)
facet_plot = ggplot(data = mpg, aes(x = year, y = cty)) +
geom_point(size = 1.3) +
facet_wrap(~manufacturer)
facet_plot # room for one more panel which I want first_plot to go?
# try an merge but makes first plot huge, compared with facetted plots.
subplot(first_plot, facet_plot, which_layout = 2)
Besides the options to manipulate the gtable or using patchwork one approach to achieve your desired result would be via some data wrangling to add the standalone plot as an additional facet. Not sure whether this will work for your real data but at least for mpg you could do:
library(ggplot2)
library(dplyr)
mpg_bind <- list(standalone = mpg, facet = mpg) %>%
bind_rows(.id = "id") %>%
mutate(x = ifelse(id == "standalone", trans, year),
facet = ifelse(id == "standalone", "all", manufacturer),
facet = forcats::fct_relevel(facet, "all", after = 1000))
ggplot(data = mpg_bind, aes(x = x, y = cty)) +
geom_point(size = 1.3) +
facet_wrap(~facet, scales = "free_x")

Outliers appearing in box plot when I use plotly with ggplot2 (R)?

I have a problem with outliers appearing when they shouldn't when I use plotly, and I'm wondering how to stop this from happening. I'll use the mtcars dataset as an example.
I'll set an obvious outlier:
mtcars[1,1] = 60
The first plot, with outliers included:
p <- ggplot(mtcars) +
+ geom_boxplot(
+ aes(x = cyl, y = mpg, group = cyl))
Now I make a plot with outliers removed:
p <- ggplot(mtcars) +
geom_boxplot(
aes(x = cyl, y = mpg, group = cyl),
outlier.shape = NA)
Now I convert the plot to ggplotly and save
p <- plotly::ggplotly(p)
The outliers are showing. Does anyone know how I can get around this problem / have a solution when using ggplotly?
Found a solution:
for (i in 1:3) {
p$x$data[[i]]$marker <- list(opacity = 0)
}

Size Scale Disappears ggplotly

When I try to use ggplotly(), I lose the legend that explains the color scale. If I use color instead of size the legend appears. How do I get the legend to appear when using size?
library(dplyr)
library(plotly)
plot <- mtcars %>%
ggplot(aes(x = mpg, y = cyl, size = disp, frame = hp)) +
geom_point()
ggplotly(plot) %>%
layout(showlegend = T)
I think the issue is that using as size a numeric variable such as disp will create a very large legend e.g.:
p <- ggplot(data=mtcars, aes(x = mpg, y = cyl, colour=factor(disp), size = factor(disp))) +
geom_point()
p <- ggplotly(p)
p
PS: I'm not sure you can use frame and still have the desired legend using ggplotly.
Option 2: Use directly plot_ly e.g.:
plot_ly(mtcars,x = ~ mpg, y= ~ cyl, size = ~disp , type='scatter', mode = 'markers', frame = ~disp) %>%
layout(showlegend = T)

plotly does not show titles of multiple plots in R

I am trying to layout 2 plots together using ggplot2 and plotly. Here's what I tried:
library(ggplot2)
library(plotly)
mt_mpg <- ggplot(data = mtcars)+
geom_boxplot(aes(x = as.factor(cyl), y = mpg))+
ggtitle("mpg vs cyl")
mt_disp <- ggplot(data = mtcars)+
geom_boxplot(aes(x = as.factor(cyl), y = disp))+
ggtitle("disp vs cyl")
subplot(mt_mpg, mt_disp)
Everything works great but the title of the combined plot only contains "disp vs cyl". I want to include both titles on the top of their corresponding plots. But I don't see any option in subplot() command to do so. Any ideas how this can be fixed? Thanks.
one way is to use facet_wrap instead of ggtitle. For example:
df <- mtcars
df$lab1 <- 'mpg vs cyl'
df$lab2 <- 'disp vs cyl'
mt_mpg <- ggplot(df)+
geom_boxplot(aes(x = as.factor(cyl), y = mpg))+
facet_wrap(~lab1)
mt_disp <- ggplot(df)+
geom_boxplot(aes(x = as.factor(cyl), y = disp))+
facet_wrap(~lab2)
subplot(mt_mpg, mt_disp)
Cheers,
Branden

fill Interaction not working with ggplot2 and plotly

I want to add interaction to the fill argument of my plotly figure, as e.g. answered here.
However, although the ggplot figure comes out correctly, the plotly does not. I am using the following versions and show a MWE below:
ggplot2: version 2.0.0.9000, plotly: version 2.0.19
library(plotly)
g <- ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = interaction(factor(cyl),carb))) + geom_boxplot()
(gg <- ggplotly(g))
Any ideas why g and gg differs above?
Not a complete solution. Include the interaction to your x term:
# use lex.order to obtain correct ordered levels
a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T), y = mpg,fill = interaction(cyl, carb, lex.order = T))) +
geom_boxplot()
# check the plot
a
# plotly
ggplotly(a)
actually there is an alternative that will give you exactly what you want.
mtcars$intec <- interaction(factor(cyl),carb)
mtcars %>%
plot_ly(x = cyl, y = mpg, type = "box", color = as.factor(intec), fill=as.factor(intec)) %>%
layout(boxmode = "group")

Resources