Rcoding
Rcoding

Reputation: 357

Reorder ggplot2 barplot in R

I have dat which contains products' features and sentiment scores.

TAL = c('Samsung','Apple','LG','Sonic','Motorola','Samsung','Apple','LG','Sonic','Motorola','Samsung','Apple','LG','Sonic','Motorola','Samsung','Apple','LG','Sonic','Motorola','Samsung','Apple','LG','Sonic','Motorola','Samsung','Apple','LG','Samsung','Apple','LG','Samsung','Apple','LG','Samsung','Apple')
FEL = c('color','price','name','brand','sound','technology','general','height','width','color','price','name','brand','sound','technology','general','height','width','color','price','name','brand','sound','technology','general','height','width','color','price','name','brand','sound','technology','general','height','width')
POLAR = c(10,-5,5,-8,6,3,5,10,-5,5,-8,6,3,5,10,-5,5,-8,6,3,5,10,-5,5,-8,6,3,5,10,-5,5,-8,6,3,5,10)
dat = data.frame(TAL,FEL,POLAR)

And, I made a plot with ggplot2 with this code below

sentim <- ggplot(dat, aes(x=reorder(FEL,-POLAR), y=POLAR, fill= POLAR > 0)) +
  geom_bar(stat = "identity", show.legend = F)+
  xlab("Feature") +
  ylab("Sentiment score") +
  facet_grid(. ~ TAL, scales = "free") +
  theme(panel.background = element_blank(),
        plot.background = element_blank(),
        panel.grid.major.x = element_line(size=0.1, colour = 'grey', linetype=3),
        panel.grid.minor.x = element_line(size=0.1, colour = 'grey', linetype=3),
        panel.spacing = unit(1, 'lines'),
        axis.line = element_line(size=0.6, colour = 'black'),
        axis.text = element_text(colour = 'black'),
        axis.ticks = element_line(colour = 'black'))
sentim

Even though I used reorder, I just had this picture.

enter image description here

Although I like the overall outfit, I want those features to be in order using somthing like decreasing = T and just to show Top3 products with Top4 features of the highest or lowest sentiment scores - POLAR

enter image description here

Something like a picture above, I want to order features but just to show a few of them for each product.

Upvotes: 0

Views: 504

Answers (1)

Haboryme
Haboryme

Reputation: 4761

You could do:

dat$temp_var <- paste(dat$TAL, dat$FEL) #creates a secondary variable
library(dplyr)
#get the sum for each TAL-FEL group of POLAR and sort by decreasing order
dat=dat%>%group_by(TAL,FEL)%>%mutate(tot=sum(POLAR))%>%arrange(tot)

ggplot(dat, aes(reorder(temp_var, tot), POLAR,fill= POLAR > 0))+
  geom_bar(stat = "identity", show.legend = F)+
  facet_wrap(~TAL,scales="free_x")+
   scale_x_discrete(labels = setNames(as.character(dat$FEL), dat$temp_var))

Gives:
enter image description here

#If you want to focus on what's bad and need work you can filter out what you considering uninteresting, here if total of POLAR>5.
dat_bad=dat%>%group_by(TAL,FEL)%>%mutate(tot=sum(POLAR))%>%arrange(tot)%>%filter(tot<5)

ggplot(dat_bad, aes(reorder(temp_var, tot), POLAR,fill= POLAR > 0))+
  geom_bar(stat = "identity", show.legend = F)+
  facet_wrap(~TAL,scales="free_x")+
  scale_x_discrete(labels = setNames(as.character(dat$FEL), dat$temp_var))

Gives:
enter image description here

And credit to Axeman for the nice function to rename the labels.

Upvotes: 2

Related Questions