anrisakaki96
anrisakaki96

Reputation: 313

How do I only plot values over a certain number in ggplot?

my dataframe currently looks like this

N = 61
MChange <- rnorm(N)
FChange <- rnorm(N) 
Industry <- sample(N)
industry020406 <- data.frame(Industry, MChange, FChange)

Using the following code:

ggplot(industry020406, aes(reorder(Industry, MChange), MChange)) +
  geom_col() +
  labs(x = "Industry",
       y = "Tariff Cut (Percentage Points)") +
  theme(axis.text.x = element_text(angle = 90))+
  coord_flip()

my barchart looks like this:

enter image description here

Since it is difficult to identify changes that are significant, is there a way I can filter out MChange that is between -1 and 1 within the ggplot function?

Thank you.

Upvotes: 1

Views: 1042

Answers (2)

Darren Tsai
Darren Tsai

Reputation: 35584

You can do the filtering in xlim():

x.lab <- with(industry020406, reorder(Industry, MChange)[MChange >= -1 & MChange <= 1])

ggplot(industry020406, aes(reorder(Industry, MChange), MChange)) +
  geom_col() +
  coord_flip() +
  xlim(sort(x.lab))

enter image description here

Upvotes: 0

Allan Cameron
Allan Cameron

Reputation: 173978

Yes, just filter it as normal, e.g. with dplyr::filter

ggplot(dplyr::filter(industry020406, MChange < 1 & MChange > - 1),
       aes(reorder(Industry, MChange), MChange)) +
  geom_col() +
  labs(x = "Industry",
       y= "Tariff Cut (Percentage Points)") +
  theme(axis.text.x = element_text(angle = 90))+
  coord_flip()

enter image description here

Upvotes: 1

Related Questions