0

My data is a table() output on four variables (data is below).

I want to plot N per V3, filled by V1 and faceted by V2. But before, V1 shall be ordered by V4.

temp$V1 <- reorder(temp$V1, temp$V4)
ggplot(temp, aes(x=V3, y=N, fill=V1)) +
  geom_bar(binwidth = 10, stat="identity") +
  ylab("") + xlab("") +
  facet_grid(. ~ V2) +
  scale_fill_discrete(name = "Journal") +
  theme(legend.title = element_text(colour="black", size=14),
        legend.direction="horizontal",
        legend.position="bottom",
        strip.text.x = element_text(size = 14),
        axis.text = element_text(colour="black", size=10),
        axis.ticks.x = element_blank(),
        panel.grid.minor.x=element_blank(),
        panel.grid.major.x=element_blank()
  )

But the bar elements are still not ordered according my desired rule for my output looks like

enter image description here

The order should be JBF, JFI, JMCB, JF, JFE, RFS. What am I doing wrong? Are there too many dimensions?

Here is the data:

    V1    V2    V3     V4   N
    JBF  1998   with    0   97
    JFI  1998   with    0   21
    JMCB 1998   with    0   62
    JBF  1999   with    0   93
    JFI  1999   with    0   20
    JMCB 1999   with    0   37
    JBF  2000   with    0   112
    JFI  2000   with    0   29
    JMCB 2000   with    0   65
    JBF  2009   with    0   247
    JFI  2009   with    0   40
    JMCB 2009   with    0   126
    JBF  2010   with    0   299
    JFI  2010   with    0   27
    JMCB 2010   with    0   110
    JBF  2011   with    0   311
    JFI  2011   with    0   34
    JMCB 2011   with    0   120
    JBF  1998   without 0   29
    JMCB 1998   without 0   9
    JBF  1999   without 0   62
    JMCB 1999   without 0   16
    JBF  2000   without 0   37
    JFI  2000   without 0   2
    JMCB 2000   without 0   20
    JBF  2009   without 0   158
    JFI  2009   without 0   2
    JMCB 2009   without 0   23
    JBF  2010   without 0   153
    JFI  2010   without 0   2
    JMCB 2010   without 0   28
    JBF  2011   without 0   133
    JFI  2011   without 0   1
    JMCB 2011   without 0   12
    JF   1998   with    1   133
    JFE  1998   with    1   91
    RFS  1998   with    1   44
    JF   1999   with    1   134
    JFE  1999   with    1   101
    RFS  1999   with    1   72
    JF   2000   with    1   147
    JFE  2000   with    1   101
    RFS  2000   with    1   74
    JF   2009   with    1   154
    JFE  2009   with    1   181
    RFS  2009   with    1   284
    JF   2010   with    1   128
    JFE  2010   with    1   179
    RFS  2010   with    1   218
    JF   2011   with    1   103
    JFE  2011   with    1   249
    RFS  2011   with    1   189
    JF   1998   without 1   12
    JFE  1998   without 1   2
    JF   1999   without 1   18
    JFE  1999   without 1   2
    RFS  1999   without 1   3
    JF   2000   without 1   8
    JFE  2000   without 1   6
    RFS  2000   without 1   2
    JF   2009   without 1   6
    JFE  2009   without 1   4
    RFS  2009   without 1   4
    JFE  2010   without 1   9
    RFS  2010   without 1   8
    JF   2011   without 1   3
    JFE  2011   without 1   7
    RFS  2011   without 1   9

Editor: output of dput(temp)

structure(list(V1 = structure(c(3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 1L, 3L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 6L, 4L, 5L, 6L, 4L, 5L, 
6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 6L, 4L, 
5L, 6L, 4L, 5L, 6L, 4L, 5L, 4L, 5L, 6L, 4L, 5L), .Label = c("JMCB", 
"JFI", "JBF", "JFE", "RFS", "JF"), class = c("ordered", "factor"
)), V2 = c("1998", "1998", "1998", "1999", "1999", "1999", "2000", 
"2000", "2000", "2009", "2009", "2009", "2010", "2010", "2010", 
"2011", "2011", "2011", "1998", "1998", "1999", "1999", "2000", 
"2000", "2000", "2009", "2009", "2009", "2010", "2010", "2010", 
"2011", "2011", "2011", "1998", "1998", "1998", "1999", "1999", 
"1999", "2000", "2000", "2000", "2009", "2009", "2009", "2010", 
"2010", "2010", "2011", "2011", "2011", "1998", "1998", "1999", 
"1999", "1999", "2000", "2000", "2000", "2009", "2009", "2009", 
"2010", "2010", "2011", "2011", "2011"), V3 = c("with", "with", 
"with", "with", "with", "with", "with", "with", "with", "with", 
"with", "with", "with", "with", "with", "with", "with", "with", 
"without", "without", "without", "without", "without", "without", 
"without", "without", "without", "without", "without", "without", 
"without", "without", "without", "without", "with", "with", "with", 
"with", "with", "with", "with", "with", "with", "with", "with", 
"with", "with", "with", "with", "with", "with", "with", "without", 
"without", "without", "without", "without", "without", "without", 
"without", "without", "without", "without", "without", "without", 
"without", "without", "without"), V4 = c("0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"), 
    N = c(97L, 21L, 62L, 93L, 20L, 37L, 112L, 29L, 65L, 247L, 
    40L, 126L, 299L, 27L, 110L, 311L, 34L, 120L, 29L, 9L, 62L, 
    16L, 37L, 2L, 20L, 158L, 2L, 23L, 153L, 2L, 28L, 133L, 1L, 
    12L, 133L, 91L, 44L, 134L, 101L, 72L, 147L, 101L, 74L, 154L, 
    181L, 284L, 128L, 179L, 218L, 103L, 249L, 189L, 12L, 2L, 
    18L, 2L, 3L, 8L, 6L, 2L, 6L, 4L, 4L, 9L, 8L, 3L, 7L, 9L)), .Names = c("V1", 
"V2", "V3", "V4", "N"), class = c("data.table", "data.frame"), row.names = c(NA, 
-68L), .internal.selfref = <pointer: 0x318e998>)
MERose
  • 4,048
  • 7
  • 53
  • 79
  • It seems that you want to reorder the factor like this; `temp$V1 <- factor(temp$V1, levels = c("JBF", "JFI", "JMCB", "JF", "JFE", "RFS"))`. – jazzurro Jan 15 '15 at 00:39
  • 1
    I cannot reproduce this problem: when I read in your table and run your code, the labels are in exactly the order you want. Could you provide the result of `dput(temp)`? – David Robinson Jan 15 '15 at 00:44
  • You should write `ordered = TRUE` as well to @jazzurro's answer like `temp$V1 <- factor(temp$V1, levels = c("JBF", "JFI", "JMCB", "JF", "JFE", "RFS"), ordered=T)` – Koundy Jan 15 '15 at 04:45
  • I had a similar issue recently and could use `reorder` as shown [here](http://stackoverflow.com/a/9231857/3521006). – talat Jan 15 '15 at 06:58
  • After printing temp over and over again, I realized that the order of temp was the problem, not the plotting procedure nor the order of `temp$V1`. Answer is below. Nonetheless, I added the output of `dput(temp)` to help future readers. Thank you all. – MERose Jan 15 '15 at 15:09

1 Answers1

0

The only command missing was to redefined temp before plotting:

temp <- temp[order(temp$V1), ]

Because in this case, temp is really ordered and not just virtually.

MERose
  • 4,048
  • 7
  • 53
  • 79