Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to exclude an observation / tuple from my ggplots. I only want to show each country and not the world total

ggplot(trade,aes(x='',y=value,fill=factor(countries)))+
  geom_bar(width=1,stat='identity')+
  coord_polar(theta = 'y',start=0)+
  labs(title = 'Dairy products imported',y='Dollar amount in 1000\'s' )

I do not want the world portion to show on my pie chart, however i do not
want to remove it completely from my data frame because it is relevant for my other charts.

Pie chart result including world data

Sample data

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1                     World 2010 463000
2                    France 2010 145000
3               New Zealand 2010 191000
4  United States of America 2010 126000
5                   Denmark 2010      0
6                   Ireland 2010      0
7       Trinidad and Tobago 2010      0
8                     World 2011 525000
9                    France 2011 266000
10              New Zealand 2011 122000

>Solution :

You need to filter() out the observations where countries is world. Using dplyr, pipe that into your ggplot call.

trade %>% 
  filter(countries != "World") %>% 
  ggplot(aes(x='',y=value,fill=factor(countries)))+
  geom_bar(width=1,stat='identity')+
  coord_polar(theta = 'y',start=0)+
  labs(title = 'Dairy products imported',y='Dollar amount in 1000\'s' )

A base R version would be

ggplot(trade[trade$countries!="World",], aes(x='',y=value,fill=factor(countries)))+
  geom_bar(width=1,stat='identity')+
  coord_polar(theta = 'y',start=0)+
  labs(title = 'Dairy products imported',y='Dollar amount in 1000\'s' )

You simply are filtering/subsetting data using a logical operation where != means not equal to.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading