2

I have a data frame lot_main that looks like this:enter image description here

I want to make a bar plot with columns reorders according to the wordcount.

library(ggplot2)
library(viridis)
lotr_main %>% ggplot(aes(x = Character, y = wordcount, fill = Film)) + 
geom_bar(stat="identity",position = "dodge") +
coord_flip() +
scale_fill_viridis("Film",discrete = TRUE, option = "C")

The plot I got: enter image description here

What I want is for each character, the bars are reorders with the longest on the top and shortest at the bottom. The orders of the bars don't need to be the same for each character.

Rann
  • 315
  • 1
  • 4
  • 11
  • 1
    Welcome to stackoverflow (SO)! It's more likely that we will be able to help you if you make a minimal reproducible example to go along with your question. Something we can work from and use to show you how it might be possible to solve your problem. You can have a look at [this SO post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example in R. Furthermore, it would also be helpful if you outline what you have already tried. – Eric Fail Nov 28 '15 at 02:13

2 Answers2

3

You essentially want to fill by one thing, and order by another. A solution is thus to pry them apart and create a separate 'order' variable. Of note, I don't know if sorting your bars by value instead of having the same sequence each 'group' makes your plot more understandable.....

create some data:

library(data.table)


set.seed(123)
dat <- expand.grid(group=LETTERS[1:3],
                   subgroup=LETTERS[1:3])
dat$value <- runif(nrow(dat))
setDT(dat)

Create the order variable:

dat[,order:=order(value),by=group]

Create the plot

p1 <- ggplot(dat, aes(x=group,y=value, fill=subgroup,group=order))+
  geom_bar(aes(group=order),position="dodge", stat="identity") +
  coord_flip()
p1

enter image description here

Heroka
  • 12,889
  • 1
  • 28
  • 38
0

Here's a start. Found data and inspiration here (code below)

LoTRdata

LoTRdata <- structure(list(Film = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 3L, 
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("The Fellowship Of The Ring", 
"The Return Of The King", "The Two Towers"), class = "factor"), 
    Race = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 
    3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("Elf", "Hobbit", 
    "Man"), class = "factor"), Gender = structure(c(1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
    ), .Label = c("Female", "Male"), class = "factor"), Words = c(1229L, 
    971L, 14L, 3644L, 0L, 1995L, 331L, 513L, 0L, 2463L, 401L, 
    3589L, 183L, 510L, 2L, 2673L, 268L, 2459L)), .Names = c("Film", 
"Race", "Gender", "Words"), class = "data.frame", row.names = c(NA, 
-18L))


LoTRdataOrder <- LoTRdata[order(LoTRdata$Words, LoTRdata$Film) , ]

# install.packages("ggplot2", dependencies = TRUE)
require(ggplot2)

p <- ggplot(LoTRdataOrder, aes(x = Race, y = Words, fill = Film))
p + geom_bar(stat = "identity", position = "dodge") +
  coord_flip() + guides(fill = guide_legend())
Eric Fail
  • 8,191
  • 8
  • 72
  • 128