r - 如何使用 plotly 在散点图中同时应用颜色/形状/大小?

标签 r ggplot2 plotly

我正在尝试创建(在 plotly 中)一个散点图,它通过两种(或三种)美学——颜色、形状、大小来区分同一系列的点。最终目标是能够使用三种美学中的任何一种来通过图例打开/关闭点组。这适用于一种美学。

[添加于2016-06-20] 扩展所需的交互行为:这个想法是,一旦显示了图形,就能够通过单击任何图例来切换点组。例如(在下面的示例数据中),如果我要单击 y在图例中,它将隐藏/显示所有系列中的第 4、5 和 10 点。如果有点击A ,然后切换点 #1、2 和 8。
作为一个现实生活中的用例——想想债券价格,横轴是到期日,纵轴是价格。债券的特点是来源国、信用等级和发行规模。因此,如果我点击信用评级“A”,我希望隐藏所有 A 级问题,无论其规模和原产国如何。目前,它们仅隐藏在与评级相关的跟踪中。轨迹中反射(reflect)其他属性(大小和国家/地区)的点仍然显示。
鉴于下面的详细答案,我倾向于将此作为功能请求发布到 plotly的网站。

我已经为 plotly 提出了问题。 ,但如果这种行为可以在 R 的另一个包/库中以相对较低的痛苦程度(意味着没有自定义 JavaScript 等)实现,我也会接受它作为答案。
[结束编辑]

静态部分很容易在 ggplot2 中完成但我无法在 plotly 中重新创建它(用于交互性),即使使用 ggplotly() .不确定这是否可能,但我想我会问。下面的示例数据和代码。

(可能与 Using 2+ legends from R to plotly / plot.ly 有关)

生成一些虚拟数据:

library(data.table)
library(plotly)
library(ggplot2)


DT <- data.table(
    x = c(1:10), y = 1:10/2,
    gr1 = c("A", "A", "B", "C", "D", "D", "B", "A", "E", "E"),
    gr2 = c("x", "x", "x", "y", "y", "z", "z", "x", "x", "y"),
    gr3 = c(1,2,2,1,3,4,1,2,2,1)
)
ggplot()版本看起来像这样,并且是我想在 plotly 中得到的:
p <- ggplot(data = DT) + geom_point(aes(x = x, y = y, color = gr1, shape = gr2, size = gr3))
p

图例中有三组标准,点有不同的颜色、形状和大小。
ggplot version

调用ggplotly(p)生成一堆警告:
Warning messages:
1: In if (s == Inf) { :
  the condition has length > 1 and only the first element will be used
2: In if (s == Inf) { :
  the condition has length > 1 and only the first element will be used
3: In if (s == Inf) { :
  the condition has length > 1 and only the first element will be used
4: In if (s == Inf) { :
  the condition has length > 1 and only the first element will be used
5: In if (s == Inf) { :
  the condition has length > 1 and only the first element will be used
6: In if (s == Inf) { :
  the condition has length > 1 and only the first element will be used
7: In if (s == Inf) { :
  the condition has length > 1 and only the first element will be used
8: In if (s == Inf) { :
  the condition has length > 1 and only the first element will be used

并产生这个数字:

ggplotly version

尝试使用 plot_ly() ,我得到以下信息:
plot_ly(data = DT, x = x, y = y, color = gr1, symbol = gr2, type = "scatter", mode = "markers", marker = list(size = 10 * gr3)) # size is multiplied by 10, in plotly it is in pixels

plot_ly version

问题在图的中间最为明显——不是彩色十字,而是几种不同颜色的形状相互叠加。由于这是一个点,我期待一个单一的颜色形状,如 ggplot .在 plotly 中,“颜色”、“符号”和“大小”参数是否创建了新的轨迹?

我对 plotly 还是很陌生,所以我可能会遗漏一些明显的东西。

以上是使用 R 3.2.2 完成的。在 Windows 下,使用 plotly_2.0.16ggplot2_2.0.0 .

最佳答案

不幸的是,plotly 不会自动给出这种行为。但是,它可以通过单独指定每个点的颜色、形状和大小来完成——使用 colors = , size = , 和 symbols =论据。这允许控制点的绘制方式,但不会获得您想要的图例。所以我们使用showlegend = FALSE在主图中并通过添加另外三个(不可见)轨迹来构建图例,这些轨迹仅用于生成图例项。

请注意,我们还需要在这里应用一个技巧。要获得显示颜色或大小的图例,可以使用参数 visible = "legendonly"它创建了一个图例条目,而不会在图表上过度绘制额外的点。但这不适用于形状。结合visible = "legendonly"symbols =似乎有一个错误,将错误的项目放入图例中。因此,要为形状创建图例条目,您可以将它们绘制在平流层中一个它们永远不可见的位置(这里我使用 x=y=1e6)并设置 x 和 y 轴限制以将它们排除在外观点。

DT <- data.table(
  x = c(1:10), y = 1:10/2,
  gr1 = as.factor(c("A", "A", "B", "C", "D", "D", "B", "A", "E", "E")),
  gr2 = as.factor(c("x", "x", "x", "y", "y", "z", "z", "x", "x", "y")),
  gr3 = c(1,2,2,1,3,4,1,2,2,1)
)
shapes <- c("circle", "square", "diamond","cross", "x","square-open","circle-open","diamond-open")
DT$shapes <- shapes[DT$gr1]
DT$col <- rainbow(3)[DT$gr2]
DT$size <- DT$gr3*10

plot_ly() %>%
  add_trace(data = DT, x = x, y = y, type = "scatter", mode = "markers", 
            color=gr2, colors=col,
            marker = list(size = size, symbol=shapes), showlegend=F) %>%
  add_trace(data = DT, x = x, y = y, type = "scatter",mode = "markers", 
            color= factor(gr2), colors=col, 
            visible="legendonly", showlegend=T, legendgroup="color",
            marker = list(size = 14)) %>%
  add_trace(data = DT, x = x, y = y, type = "scatter",mode = "markers", 
            color=factor(gr3), colors="#000000", 
            marker = list(size = size),
            visible="legendonly", showlegend=T, legendgroup="size") %>%
  add_trace(data = DT, x = 1e6, y = 1e6, type = "scatter", mode = "markers", 
            color=factor(gr1), colors="#000000", 
            marker = list(size=14, symbol=shapes),
            showlegend=T, legendgroup="shape") %>%
  layout(legend=list(traceorder="grouped+reversed", tracegroupgap =30),
         xaxis=list(range=c(0,12)),
         yaxis=list(range=c(0,6)))

enter image description here

关于r - 如何使用 plotly 在散点图中同时应用颜色/形状/大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34582584/

相关文章:

r - 具有逆倾向处理权重的 Cox 回归

r - 每个 bin 的均值直方图

c++ - 编译 RInside 代码时出错

r - R中三角形区域的双重积分

r - 如何在 R 中使用曲线()对图形进行着色

r - 用 R (ggplot) 绘制二维箱线图

python - 如何使用 slider 向 Plotly 条形图添加交互式文本?

r - Plotly 等高线图行为

python - 如何从 Python 中并排绘制多个饼图?

r - 将字符矩阵转换为数字矩阵