簡體   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