R plotly - 绘制分组线 [英] R plotly - Plotting grouped lines

查看:179
本文介绍了R plotly - 绘制分组线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我确实知道plotly库有一个ggplotly函数,我们可以用ggplotly函数来实现这个功能。我可以使用封装原生ggplot命令,但我想学习如何使用本机plotly命令绘制类似图。



我的问题是我似乎无法制作以ggplot2的方式绘制分组线。



使用ggplot2的默认mpg数据集的基础数据



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

|制造商| class |模特|
|:------------ |:---------- | ------:|
| audi | compact | 15 |
| audi | midsize | 3 |
|雪佛兰| 2seater | 5 |
| chevrolet | midsize | 5 |
|雪佛兰| suv | 9 |
| dodge | minivan | 11 |
|闪避|皮卡| 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 |
|路虎| 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 |
|丰田|皮卡| 7 |
| toyota | suv | 8 |
| volkswagen | compact | 14 |
| volkswagen | midsize | 7 |
| volkswagen | subcompact | 6 |



示例1:这可以工作



<$ p $ %>
>%
$%
$($)$%
$ =〜class,y =〜models,type =scatter,mode =lines + marker,color =〜manufacturer)



示例2:但是这只返回一个空格

与示例1的区别在于我试图按类而不是制造商进行分组。

  mpg%>%
group_by(manufacturer,class)%>%
汇总= n())%>%
plot_ly(x =〜manufacturer,y =〜models,type =scatter,mode =lines + marker,color =〜class)



示例3:这是ggplot2版本,我想如何绘制它



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

我怎样才能让Example 2看起来像Example 3?

$ b $在 plotly 中奇怪的是,您执行 dplyr code> group_by 很重要(我不应该这样想)。也许这是一个错误,或许某种我不知道的方式。

此时 plotly 很年轻,并且充满了像这样的意外错误,所以要谨慎期待 plotly 是完全替代 ggplot2 ,它现在还没有接近,尽管它有一些很酷的



因此,您得到您想要的:

  library(dplyr)
library(plotly)
mpg%>%
group_by(class,manufacturer)%>%
summary(models = n())%> ;%
plot_ly(x =〜制造商,y =〜型号,group =〜class,
type =scatter,color =〜class,mode =lines + markers)

收益率:



你试过的东西会让你空白:

  library(dplyr)
图书馆(情节)
mpg%>%
group_by(manufacturer,class)%>%
汇总(models = n())%>%
plot_ly(x =〜制造商,y =〜models,group =〜class,
类型=分散,color =〜class,mode =lines + markers)

孤儿有些奇怪的原因:



以及这里是你的ggplot版本供参考:

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


I am migrating over from ggplot2 to plotly, in order to take advantage of the interactive features they offer.

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.

My problem is that I can't seem to make plotly draw grouped lines the way ggplot2 does.

Base Data, using the default mpg data set from ggplot2

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

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

Difference with Example 1 is that I'm trying to group by class instead of manufacturer.

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

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?

解决方案

Oddly enough in plotly the order that you do the dplyr group_by matters (it should not I would think). 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.

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:

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

这篇关于R plotly - 绘制分组线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆