0

I am putting together a geom_density to express drug use in a population along with an "Age" distribution. I would like to omit the "No" variable response (which is embedded with other answers in the column of N =597) from being expressed in the chart.

I've looked around at Boolean syntax possibilities but found no clear grammar examples to fit this context. I reviewed ? help documentation and could not locate a ready example on how to omit values from a chart.

drugs <- Substance_use_self_report

ggplot(drugs, aes(Age, fill = drug_of_choice)) +
  geom_density(alpha = 0.25)

In the Drug_of_choice column of my dataset, the field is overfitted with No response cells. I would like to omit those cells from being expressed in the chart. (So we can see what drugs people like, without the "no" blob in there!) :)

Sina
  • 270
  • 1
  • 23
  • 1
    Welcome to SO. Please take a look at [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – markus Jun 06 '19 at 22:01
  • `ggplot(drugs[drug_of_choice!="No",], ...` – phalteman Jun 06 '19 at 23:08

1 Answers1

0

You can do some filtering and pipe right into ggplot()

set.seed(1234)
drugs <- 
  tibble(
    Age = sample(25:50, 60, replace = TRUE),
    drug_of_choice = c(rep("No", 20), sample(letters[1:3], 40, replace = TRUE))
  )

drugs %>% 
  filter(drug_of_choice != "No") %>% 
  ggplot(aes(Age, fill = drug_of_choice)) +
  geom_density(alpha = 0.25)

yake84
  • 3,004
  • 2
  • 19
  • 35