ytk
ytk

Reputation: 2827

coord_flip() mixing up axis lables?

I am trying to build a horizontal bar chart.

library(ggplot2)
library(plyr)

salary <- read.csv('September 15 2015 Salary Information - Alphabetical.csv', na.strings = '')
head(salary)

salary$X <- NULL
salary$X.1 <- NULL

salary$Club <- as.factor(salary$Club)
levels(salary$Club)

salary$Base.Salary <- gsub(',', '', salary$Base.Salary)
salary$Base.Salary <- as.numeric(as.character(salary$Base.Salary))
salary$Base.Salary <- salary$Base.Salary / 1000000

salary <- ddply(salary, .(Club), transform, pos = cumsum(Base.Salary) - (0.5 * Base.Salary))
ggplot(salary, aes(x = Club, y = Base.Salary, fill = Base.Salary)) +
  geom_bar(stat = 'identity') +
  ylab('Base Salary in millions of dollars') + 
  theme(axis.title.y = element_blank()) +
  coord_flip() + 
  geom_text(data = subset(salary, Base.Salary > 2), aes(label = Last.Name, y = pos))

(credits to this thread: Showing data values on stacked bar chart in ggplot2 for the text position calculation)

and the resulting plot is this: enter image description here

I was thoroughly confused for a while, because I was using xlab to specify the label, and theme(axis.title.y = element_blank()) to hide the y label. However, this didn't work, and I got it to work by changing it to ylab. This seems rather confusing, is it intended?

Upvotes: 2

Views: 2332

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145745

This seems rather confusing, is it intended?

Yes.

Rather than using theme() to hide the y label, I think

labs(x = "My x label",
     y = "") 

is more straightforward.

When you flip x and y, they take their labels with them. If this weren't the case, a graph compared with and without coordinate flip would have incorrect axis labels in one of the two cases - which seems confusing and inconsistent. As-is, the labels will be correct always (with and without coord_flip).

Theming, on the other hand, is applied after-the-fact.

Upvotes: 3

Related Questions