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.
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|
mpg %>%
group_by(manufacturer, class) %>%
summarise(models=n()) %>%
plot_ly(x=~class, y=~models, type="scatter", mode="lines+marker", color=~manufacturer)
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)
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?
To create multiple line charts on the same plot with plotly graph objects, all you have to do is add another trace to the plot. If you look closely, you can see that I have also added the name parameter to add the labels for the legend and also explicitly added the mode='lines' to tell plotly that i want a line chart.
Using color_discrete_sequence Another method you can use to customize a line plot's color is the color_discrete_sequence parameter. This allows you to pass a list of color values that are assigned to each plot type. This should set the color in the list as the default line color for your plot.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With