Vivvi
Vivvi

Reputation: 43

r filter() issue: plotly vs ggplot

I am starting to learn interactive data viz and basic data analysis with R (mainly plotly). I am having an issue while using the dplyr function filter() while plotting with plotly in R. here is an example using the gapminder dataset:

library(gapminder)


# filter by year and continent
gapminder_2002_asia <- gapminder %>%
  filter(year== 2002 & continent == "Asia")
# plot gpd/capita bar chart using plotly
gapminder_2002_asia %>%
  plot_ly() %>%
  add_bars(x= ~country, y = ~gdpPercap, color = ~country)

this is the results: all the world countries present in the initial data set are on the x axis: plotly graph as image

enter image description here

On the other hand, if just make a static graph with ggplot, I only have the asian countries appearing on the x axis:

gapminder_2002_asia %>%
  ggplot(aes(country, gdpPercap, fill = country)) +
  geom_col()

ggplot graph

enter image description here

I really do not understand how this is happening as they both come from the same df..

Upvotes: 2

Views: 715

Answers (2)

Duck
Duck

Reputation: 39595

The reason is that plotly is taking all the levels inside the country variable while ggplot2 only takes the available values in your dataset. So, to get same results, yu can use this:

library(plotly)
library(ggplot2)
#Plotly
gapminder_2002_asia %>%
  plot_ly() %>%
  add_bars(x= ~country, y = ~gdpPercap, color = ~country)

Output:

enter image description here

And with ggplot2:

#ggplot2
gapminder_2002_asia %>%
  ggplot(aes(country, gdpPercap, fill = country)) +
  geom_col()+
  scale_x_discrete(limits=levels(gapminder_2002_asia$country))+
  theme(axis.text.x = element_text(angle=90))

Output:

enter image description here

Update: In order to get the same output in plotly you could use something like this, which will be similar to your ggplot2 initial code for plotting:

#Plotly 2
gapminder_2002_asia %>%
  mutate(country=as.character(country)) %>%
  plot_ly() %>%
  add_bars(x= ~country, y = ~gdpPercap, color = ~country)

Output:

enter image description here

The key to tackle is the factors in your dataset.

Another option can be fct_drop() from forcats (many thanks and credit to @user2554330):

library(forcats)
#Plotly 2
gapminder_2002_asia %>%
  mutate(country=fct_drop(country)) %>%
  plot_ly() %>%
  add_bars(x= ~country, y = ~gdpPercap, color = ~country)

Output:

enter image description here

Upvotes: 1

Leo Kiernan
Leo Kiernan

Reputation: 66

Very odd.

As an alternative while you debug that code, why not try using ggplotly()?

E. G.

p <- gapminder_2002_asia %>%
ggplot(aes(country, gdpPercap, fill = country)) +
geom_col()

plotly::ggplotly(p)

I'd be curious which version of the plot came out the far end!

Upvotes: 1

Related Questions