简体   繁体   中英

Plotly R subpanel per group /factor

I am trying to find an example how to create a plotly plot where there is a subpanel created for each factor in one column, and the y axis is a value column, and x for instance the date or rowname

For instance if we would take the mtcars data frame, group by cyl to create 1 subpanel per cyl value, plot mpg as y axis and rowname as x axis

variable nr of factors In addition, in my intended case I do not know how many unique factors there will be because that depends on the model that creates my data in the shiny app before the plot stage.

I saw this example, but that plots a panel per variable column rather than a panel per factor:

p <- economics %>%
  tidyr::gather(variable, value, -date) %>%
  transform(id = as.integer(factor(variable))) %>%
  plot_ly(x = ~date, y = ~value, color = ~variable, colors = "Dark2",
          yaxis = ~paste0("y", id)) %>%
  add_lines() %>%
  subplot(nrows = 5, shareX = TRUE)

If anyone knows a way to get me started or has an existing example on how to do this I would be very grateful

EDIT I tried to build this code but it seems to plot all values in each panel

   mydf <- mtcars[ ,names(mtcars)[which(names(mtcars) %in%  c('cyl', 'mpg', 'hp'))]]
plot_ly(data = mydf, x =~hp, y=~mpg, type = 'scatter')


myplotlysub <- function(dat) {
  plot_ly(data = mydf, x =~hp, y=~mpg, type = 'scatter', mode = 'markers')
}

p <- mydf %>%
  group_by(cyl) %>%
  do(plot = myplotlysub(.)) %>%
  subplot(nrows = 1) %>%
  layout(
    showlegend = TRUE)
p

面板但不纠正

Using ggplotly

One possible way to do the task is to leverage ggplot2 's facet_grid & then convert the ggplot object into a plotly one using plotly::ggplotly . The ggplotly function isn't always great, but if you're familiar with ggplot2 then it's a quick way to get a plotly plot. Note since the object returned from ggplotly is a plotly object we can further modify it with plotly functions.

library(ggplot2)
library(plotly)

# move rownames to a column for use in `aes()`
my_mtcars = mtcars
my_mtcars$model = rownames(my_mtcars)

# create ggplot
p = ggplot(my_mtcars, aes(x=model, y=mpg)) +
  geom_bar(stat='identity') +
  # facet by cyl (drop unused factors in a facet using free_x)
  facet_grid(. ~ cyl, scales = "free_x") +
  labs(x='') + 
  # rotate and size x tick marks
  theme(axis.text.x = element_text(angle = 90, 
                                   hjust = 1,
                                   size=5))
# add plotly-ness
ggplotly(p) %>% 
  layout(title="MPG by CYL")

在此处输入图片说明

Using subplot

You can alternatively use plotly::subplot as pointed out in this SO Q/A . However, the answer uses purrr 's formula syntax which seems to clash with plotly's use of the ~ , and causes an error. To run w/o error you can avoid using ~var plotly syntax or use lapply instead of purrr::map .

A downside of subplot compared to facet_grid + ggplotly is that you end up with varying scaled y-axes; this is something that you'll have to manually address if you want to avoid confusion.

library(plotly)
library(purrr)

# using purrr::map
mtcars %>% 
  split(mtcars$cyl) %>% 
  map(~{
    plot_ly(data = .x, 
            x = rownames(.x), 
            y = .x$mpg, 
            type = "bar")
  }) %>% 
  subplot(margin = .05)

# using lapply
mtcars %>% 
  split(mtcars$cyl) %>% 
  lapply(function(x) {
    plot_ly(data = x, 
            x = rownames(x), 
            y = ~mpg, 
            type = "bar")
  }) %>% 
  subplot(margin = .05)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM