R plotly - row grouping

I am moving from ggplot2 to intricate to take advantage of the proposed interactive features.

I understand that the plotly library has a ggplotly function that I can use to encapsulate my own ggplot commands, but I wanted to learn how to build similar graphs using my own plotly commands.

My problem is that I cannot make graphically draw grouped lines the way ggplot2 does.

Basic data using the default mpg dataset 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: It 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 only returns an empty chart

The difference with example 1 is that I am 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 version of ggplot2, as I would like it to be built

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

How can I make example 2 look like example 3?

+5
source share
1 answer

Oddly enough, in plotly order you do dplyr group_by matters (I shouldn't think). Perhaps this is a mistake, perhaps some feature that I do not know about.

At this moment, plotly young and full of unexpected "errors" like this, so be very careful, expecting that plotly will become a complete replacement for ggplot2 , it is not even close at the moment, although it has some interesting functions.

So, you get 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: enter image description here

Where, as you tried, you get a space:

 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") 

orphaned strings for some odd reason:

enter image description here

And here is your version of ggplot 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() 

enter image description here

+2
source

Source: https://habr.com/ru/post/1267210/


All Articles