删除 R Plotly 中未使用的子图 [英] Removing Unused Subplot in R Plotly

查看:59
本文介绍了删除 R Plotly 中未使用的子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 plotly 时(在 R 中),合并子图后,会留下一个未使用的空白子图.我已经使用下面的 ggplot2 数据集 mpg 重新创建了这个问题.

When using plotly (in R), after combining subplots there remains an unused and blank subplot. I've recreated the issue using the ggplot2 dataset mpg below.

library(dplyr)
library(ggplot2)
library(plotly)


audi <- mpg %>%
    filter(manufacturer == "audi")
chevy <- mpg %>%
    filter(manufacturer == "chevrolet")



fig1 <- plot_ly(audi, x = ~hwy, y = ~year, name = "", type = 'scatter',
             mode = "markers", marker = list(color = "blue", symbol = 'x-dot'))
fig2 <- plot_ly(chevy, x = ~hwy, y = ~year, name = "", type = 'scatter',
             mode = "markers", marker = list(color = "red", symbol = 'circle'))
fig <- subplot(fig1, fig2)
fig <- fig %>% subplot(shareX = TRUE,shareY = TRUE,which_layout = "merge")
fig <- fig %>% layout(
    title = "Audi and Chevy",
    xaxis = list(title = "Highway MPG"),
    yaxis = list(title = "Year"),
    margin = list(l = 100)
)

我能找到的唯一解决方案是修改使用过的子图的宽度,但这会在右侧留下很多未使用的空白区域,并导致标题远离右侧(因为它调整到已使用和未使用的子图的中心).

The only solution I've been able to find is tinkering with the width of the used subplot, but this leaves quite a bit of unused white space on the right and causes the title to be far off to the right (as it adjusts into the center of the used and unused subplots).

有没有办法删除未使用的子图?如果没有,有没有办法组织/子集数据框,以便首先只需要使用一个图?

Is there a way to remove the unused subplot? If not, is there a way to organize/subset the dataframe such that only one plot needs to be used in the first place?

谢谢!

推荐答案

您可以根据制造商列分配颜色:

You can assing the colours based on the manufacturer column:

data.subs <- mpg %>%
  filter(manufacturer == "audi" | manufacturer == "chevrolet")

fig <- plot_ly(data.subs, x = ~hwy, y = ~year, name = "", type = 'scatter',mode = "markers", 
marker = list(color = factor(data.subs$manufacturer, labels=c("red","blue")), symbol = 'circle'), text=factor(data.subs$manufacturer, labels=c("audi","chevy")),hoverinfo='text'))
fig <- fig %>% layout(
  title = "Audi and Chevy",
  xaxis = list(title = "Highway MPG"),
  yaxis = list(title = "Year"),
  margin = list(l = 100)
)

fig

这使得生成多个子图变得不必要.

This makes generating multiple subplots unnecessary.

这篇关于删除 R Plotly 中未使用的子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆