3

I am trying to create a double-sided bar plot like in this answer, but I want to order bars by date and change the x-axis labels to other values (not dates). My data set looks like:

         date          rival goals misses
1  2015-07-19         Динамо     2      1
2  2015-07-26           Урал     4      1
3  2015-08-01          Терек     3      0
4  2015-08-09            Уфа     1      0
5  2015-08-15      Краснодар     0      2
6  2015-08-24          Рубин     3      1
7  2015-08-29 Крылья Советов     1      3
8  2015-09-12           ЦСКА     2      2
9  2015-09-20          Амкар     1      1
10 2015-09-26        Спартак     2      2
11 2015-10-03         Ростов     3      0
12 2015-10-17         Кубань     2      2
13 2015-10-24           Анжи     5      1
14 2015-10-31       Мордовия     0      0
15 2015-11-08      Локомотив     0      2
16 2015-11-21           Урал     3      0
17 2015-11-28          Терек     1      4
18 2015-12-03            Уфа     1      1

You can download it by this link. I use next code to make chart:

x = read.csv("data/2015-2016.csv", stringsAsFactors = F)
x$date = as.Date(x$date, "%d.%m.%Y")

goalsToMisses = data.frame(
  group = c(rep("Goals", nrow(x)), rep("Misses", nrow(x))),
  date = rep(x$date, 2),
  x = rep(x$rival, 2),
  y = c(x$goals, - x$misses),
  stringsAsFactors = F
)

ggplot(goalsToMisses, aes(x = reorder(x, date), y = y, fill = group)) + 
  geom_bar(stat="identity", position="identity") +
  ylim(- max(x$goals), max(x$goals)) +
  scale_y_continuous(breaks = seq(- max(x$goals), max(x$goals), 1)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5, size = 15))

And I get this: enter image description here

But the ordering is wrong; it should be as in initial data set, from top to bottom. How I can fix it?

Update: The main difference from this question is that I use a two-sided bar plot and goalsToMisses data frame twice more than the initial data frame.

Also, I can't set levels on rival factor because rival column contains duplicates and I got duplicated levels in factors are deprecated error.

Community
  • 1
  • 1
zeleniy
  • 2,232
  • 19
  • 26
  • 1
    just set the order of your factor (rival) levels – MLavoie Dec 07 '15 at 18:22
  • using goalsToMisses$rival <- factor(goalsToMisses$rival, levels = c("Динамо", "Урал"...... "Уфа")) – MLavoie Dec 07 '15 at 18:25
  • And specify whatever labels you want either as the names of the factor levels or `scale_x_discrete(labels = c("label1", "custom label 2", ....)) – Gregor Thomas Dec 07 '15 at 19:08
  • Guys, thank you for responses, but no one doesn't help me. Please see update in the bottom of the post. – zeleniy Dec 08 '15 at 08:21
  • Do you really want to plot the dates on the x axis and just put the name of the rival team as tick labels instead of the actual date? If not, what do you want to do with the rival team that was played twice? – aosmith Dec 15 '15 at 22:49

1 Answers1

1

Should to add tricky factor with right levels ordering:

x = read.csv("data/2015-2016.csv", stringsAsFactors = F)
x$date = as.Date(x$date, "%d.%m.%Y")

goalsToMisses = data.frame(
  group = c(rep("Goals", nrow(x)), rep("Misses", nrow(x))),
  x = paste(rep(x$rival, 2), "\n(", rep(x$date, 2), ")", sep=""),
  y = c(x$goals, - x$misses),
  stringsAsFactors = F)

matches = unique(goalsToMisses$x)
goalsToMisses$x = factor(goalsToMisses$x, levels = matches[order(x$date)])

ggplot(goalsToMisses, aes(x = x, y = y, fill = group)) +
  geom_bar(stat="identity", position="identity") +
  ylim(- max(x$goals), max(x$goals)) +
  scale_y_continuous(breaks = seq(- max(x$goals), max(x$goals), 1)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5, size = 15))

enter image description here

zeleniy
  • 2,232
  • 19
  • 26
  • I agree that it's appropriate to keep the dates on the graphic. You could also have solved the problem by using `date`, a factor with levels in the order of the dataset, as the x axis variable and simply replacing the tick labels with `x$rival` in `scale_x_discrete`. – aosmith Dec 16 '15 at 16:21