简体   繁体   中英

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

在此处输入图像描述

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

在此处输入图像描述

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

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:

在此处输入图像描述

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:

在此处输入图像描述

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:

在此处输入图像描述

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:

在此处输入图像描述

Very odd.

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

EG

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM