R子地块中每个地块附近的图例

R子地块中每个地块附近的图例,r,plotly,subplot,R,Plotly,Subplot,在我的R-Shining应用程序中,我使用plotly中的subplot函数来绘制两个图形(一个在另一个下面)。图例在两个图中都很常见,这意味着我只有一列,所有图例都组合在一起 我想在子图中分别显示每个图形附近的图例。我怎样才能得到它 p1 <- iris%>% group_by(Species)%>% plot_ly(x=~Sepal.Length, color= ~Species, legendgroup=~Species)%>% add_markers(y=

在我的R-Shining应用程序中,我使用plotly中的subplot函数来绘制两个图形(一个在另一个下面)。图例在两个图中都很常见,这意味着我只有一列,所有图例都组合在一起

我想在子图中分别显示每个图形附近的图例。我怎样才能得到它

p1 <-
 iris%>%
 group_by(Species)%>%
 plot_ly(x=~Sepal.Length, color= ~Species, legendgroup=~Species)%>%
 add_markers(y= ~Sepal.Width)
p2 <-
 iris%>%
 group_by(Species)%>%
 plot_ly(x=~Sepal.Length, color= ~Species, legendgroup=~Species)%>%
 add_markers(y= ~Sepal.Width)

 subplot(p1,p2, nrows = 2, shareX = T, shareY = F, titleX = T, titleY = T)
p1%
组别(种类)%>%
绘图(x=~萼片长度,颜色=~种,legendgroup=~种)%>%
添加_标记(y=~萼片宽度)
p2%
组别(种类)%>%
绘图(x=~萼片长度,颜色=~种,legendgroup=~种)%>%
添加_标记(y=~萼片宽度)
子批次(p1、p2、nrows=2、shareX=T、shareY=F、titleX=T、titleY=T)

我想要的布局如下:

根据,到目前为止,这还无法实现。这远不是一个完美的黑客,但如果你愿意,像这样的东西可能是可行的-

使用
n
标识着色变量中唯一元素的数量

library(plotly)
n <- 3L
clrs <- colorRampPalette(
  c(rgb(r = 198, g = 89, b = 17, maxColorValue = 255), '#267dff', 'black')
)(n)
annotations <- c('setosa', 'versicolor', 'virginica')
y_annotation <- seq(0.4, 0.6, length.out = n)
p1 <-
  iris %>%
  group_by(Species) %>%
  plot_ly(x = ~Sepal.Length, color = ~Species, showlegend = FALSE, colors = clrs) %>%
  add_markers(y= ~Sepal.Width) %>%
  layout(margin = list(r = 100))
for (i in seq_along(annotations)) {
  p1 <- add_annotations(
    p = p1, text = annotations[i], font = list(color = clrs[i]), 
    x = 1.05, y = y_annotation[i], xref = 'paper', yref = 'paper', 
    showarrow = FALSE, xanchor = 'left'
  )
}
p2 <-
  iris%>%
  group_by(Species) %>%
  plot_ly(x = ~Sepal.Length, color = ~Species, showlegend = FALSE, colors = clrs) %>%
  add_markers(y = ~Sepal.Width) %>%
  layout(margin = list(r = 100))
for (i in seq_along(annotations)) {
  p2 <- add_annotations(
    p = p2, text = annotations[i], font = list(color = clrs[i]), 
    x = 1.05, y = y_annotation[i], xref = 'paper', yref = 'paper', 
    showarrow = FALSE, xanchor = 'left'
  )
}
subplot(p1, p2, nrows = 2, shareX = T, shareY = F, titleX = T, titleY = T)
library(plotly)
n%
添加标记(y=~萼片宽度)%>%
布局(边距=列表(r=100))
对于(i在序号(注释)中){

p2这是无法实现的,如下所述: