繁体   English   中英

删除 R Plotly 中未使用的子图

[英]Removing Unused Subplot in R Plotly

使用 plotly(在 R 中)时,在组合子图后,仍然有一个未使用和空白的子图。 我使用下面的 ggplot2 数据集mpg重新创建了该问题。

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)
)

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

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

谢谢!

您可以根据制造商列评估颜色:

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

这使得不需要生成多个子图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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