Making ggplot boxplot but using logic to select certain cells within a column?

library(gapminder)
gapminder <- as.data.frame(gapminder)
gapminder %>% 
  ggplot(aes(x=continent,y=lifeExp, fill=continent)) +
  geom_boxplot() + geom_jitter(width=0.1,alpha=0.2) 

Using the above code from a ggplot tutorial found online, I obtain a df using data from the gapminder package. It looks like this :
image
and the resulting plot works too :

How can I plot only the continents Africa and Asia ?

The dplyr package has the filter function for just this purpose.

library(gapminder)
library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
gapminder <- as.data.frame(gapminder)
gapminder %>% 
  filter(continent %in% c("Africa", "Asia")) %>% 
  ggplot(aes(x=continent,y=lifeExp, fill=continent)) +
  geom_boxplot() + geom_jitter(width=0.1,alpha=0.2)

Created on 2020-04-27 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

OK I fixed it by explicitly referencing continent twice :

newdata <- subset(gapminder, continent=="Asia" | continent =="Africa")

Strangely I cannot use dplyr with my current application, due to it masking certain features of a couple of other packages I have loaded.

Is there a way I can subset 'Asia' and 'Africa' using the subset function? If I try something like this with one variable, Asia, it works :

newdata <- subset(gapminder, continent=="Asia")

Then I can make the plot based off this. However, I can't seem to do it for two variables - this does not work :

newdata <- subset(gapminder, continent=="Asia" | "Africa")

Also, I'm not sure if I should be using the logical operator | or &.

Had no idea this was possible - you saved the day. Thanks!

You can also call dplyr::filter() without loading the package.

library(gapminder)
library(ggplot2)
library(magrittr)

gapminder <- as.data.frame(gapminder)

#Don't load dplyr but call the function from the package
gapminder %>% 
  dplyr::filter(continent %in% c("Africa", "Asia")) %>% 
  ggplot(aes(x=continent,y=lifeExp, fill=continent)) +
  geom_boxplot() + geom_jitter(width=0.1,alpha=0.2)
1 Like