Home > Software engineering >  I want to exclude an observation / tuple from my ggplots. I only want to show each country and not t
I want to exclude an observation / tuple from my ggplots. I only want to show each country and not t

Time:12-17

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

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

CodePudding user response:

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.

  • Related