简体   繁体   English

R plotly - 绘制分组线

[英]R plotly - Plotting grouped lines

I am migrating over from ggplot2 to plotly, in order to take advantage of the interactive features they offer. 我正在从ggplot2迁移到plotly,以便利用它们提供的交互功能。

I do realize that the plotly library has a ggplotly function I can use to encapsulate native ggplot commands, but I wanted to learn how to plot similar graphs using native plotly commands. 我确实知道plotly库有一个ggplotly函数我可以用来封装原生的ggplot命令,但我想学习如何使用native natively命令绘制类似的图。

My problem is that I can't seem to make plotly draw grouped lines the way ggplot2 does. 我的问题是,我似乎无法按照ggplot2的方式绘制分组线。

Base Data, using the default mpg data set from ggplot2 基本数据,使用ggplot2中的默认mpg数据集

mpg %>%
  group_by(manufacturer, class) %>%
  summarise(models=n())

|manufacturer |class      | models|
|:------------|:----------|------:|
|audi         |compact    |     15|
|audi         |midsize    |      3|
|chevrolet    |2seater    |      5|
|chevrolet    |midsize    |      5|
|chevrolet    |suv        |      9|
|dodge        |minivan    |     11|
|dodge        |pickup     |     19|
|dodge        |suv        |      7|
|ford         |pickup     |      7|
|ford         |subcompact |      9|
|ford         |suv        |      9|
|honda        |subcompact |      9|
|hyundai      |midsize    |      7|
|hyundai      |subcompact |      7|
|jeep         |suv        |      8|
|land rover   |suv        |      4|
|lincoln      |suv        |      3|
|mercury      |suv        |      4|
|nissan       |compact    |      2|
|nissan       |midsize    |      7|
|nissan       |suv        |      4|
|pontiac      |midsize    |      5|
|subaru       |compact    |      4|
|subaru       |subcompact |      4|
|subaru       |suv        |      6|
|toyota       |compact    |     12|
|toyota       |midsize    |      7|
|toyota       |pickup     |      7|
|toyota       |suv        |      8|
|volkswagen   |compact    |     14|
|volkswagen   |midsize    |      7|
|volkswagen   |subcompact |      6|

Example 1: This works 示例1:这有效

mpg %>%
  group_by(manufacturer, class) %>%
  summarise(models=n()) %>%
  plot_ly(x=~class, y=~models, type="scatter", mode="lines+marker", color=~manufacturer)

Example 2: But this returns only a blank plot 示例2:但这只返回一个空白图

Difference with Example 1 is that I'm trying to group by class instead of manufacturer. 与示例1的不同之处在于我正在尝试按类而不是制造商进行分组。

mpg %>%
  group_by(manufacturer, class) %>%
  summarise(models=n()) %>%
  plot_ly(x=~manufacturer, y=~models, type="scatter", mode="lines+marker", color=~class)

Example 3: This is the ggplot2 version of how I would like it plotted 示例3:这是我希望如何绘制的ggplot2版本

mpg %>%
  group_by(manufacturer, class) %>%
  summarise(models=n()) %>%
  ggplot(aes(x=manufacturer, y=models, group=class, color=class)) +
    geom_line() +
    theme_minimal()

How could I make Example 2 look like Example 3? 我怎么能使例2看起来像例3?

Oddly enough in plotly the order that you do the dplyr group_by matters (it should not I would think). 在奇怪的是plotly你做的顺序dplyr group_by事项(它不应该,我觉得)。 Perhaps this is a bug, perhaps some kind of feature in some way I don't know about. 也许这是一个错误,也许是某些我不知道的特征。

At this point plotly is young, and full of unexpected "bugs" like this, so be very cautious about expecting plotly to be a complete replacement for ggplot2 , it is not even close at the moment, although it has some cool features for sure. 在这一点上, plotly很年轻,并且充满了意想不到的“错误”,所以要非常谨慎地期望plotly完全替代ggplot2 ,它现在还不是很接近,尽管它确实有一些很酷的功能。

So this gets you what you want: 所以这可以得到你想要的东西:

library(dplyr)
library(plotly) 
mpg %>%
  group_by(class,manufacturer) %>%
  summarise(models=n()) %>%
  plot_ly(x=~manufacturer, y=~models, group=~class,
                           type="scatter",color=~class, mode="lines+markers")

Yielding: 产量: 在此输入图像描述

Where as what you tried gets you a blank : 你尝试过的地方让你一片空白:

library(dplyr)
library(plotly) 
mpg %>%
  group_by(manufacturer,class) %>%
  summarise(models=n()) %>%
  plot_ly(x=~manufacturer, y=~models, group=~class,
                           type="scatter",color=~class, mode="lines+markers")

orphans the lines for some odd reason: 由于一些奇怪的原因孤儿线:

在此输入图像描述

And here is your ggplot version for reference: 这是你的ggplot版本供参考:

mpg %>%
  group_by(manufacturer, class) %>%
  summarise(models=n()) %>%
  ggplot(aes(x=manufacturer, y=models, group=class, color=class)) +
  geom_line() + geom_point() +
  theme_minimal()

在此输入图像描述

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

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