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

Time:04-20

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.

CodePudding user response:

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

CodePudding user response:

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

  • Related