R plotly - 绘制分组线 [英] R plotly - Plotting grouped lines
问题描述
我正在从 ggplot2 迁移到 plotly,以便利用它们提供的交互功能.
I am migrating over from ggplot2 to plotly, in order to take advantage of the interactive features they offer.
我确实意识到 plotly 库有一个 ggplotly 函数,我可以使用它来封装原生 ggplot 命令,但我想学习如何使用原生 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.
我的问题是我似乎无法像 ggplot2 那样绘制分组线.
My problem is that I can't seem to make plotly draw grouped lines the way ggplot2 does.
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|
示例 1:可行
mpg %>%
group_by(manufacturer, class) %>%
summarise(models=n()) %>%
plot_ly(x=~class, y=~models, type="scatter", mode="lines+marker", color=~manufacturer)
示例 2:但这只会返回一个空白图
与示例 1 的不同之处在于我尝试按类而不是制造商进行分组.
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)
示例 3:这是我想要的 ggplot2 版本
mpg %>%
group_by(manufacturer, class) %>%
summarise(models=n()) %>%
ggplot(aes(x=manufacturer, y=models, group=class, color=class)) +
geom_line() +
theme_minimal()
如何让示例 2 看起来像示例 3?
How could I make Example 2 look like Example 3?
推荐答案
奇怪的是在 plotly
你执行 dplyr
group_by
的顺序很重要(我不应该这么想).也许这是一个错误,也许是某种我不知道的某种功能.
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.
此时 plotly
还很年轻,并且充满了像这样的意想不到的错误",所以要非常谨慎地期望 plotly
完全替代 ggplot2
,虽然它肯定有一些很酷的功能,但目前还没有接近.
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")
产量:
你的尝试让你一片空白:
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:
这是你的 ggplot 版本供参考:
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屋!