Install categorical axis labels with weights "free" ggplot2

I am trying to set labels on a categorical axis in a faceted plot using the ggplot2 (1.0.1) package in R (3.1.1) with scales = "free". If I draw without manually setting the tick marks along the axis, they are displayed correctly (first plot), but when I try to set the marks (second plot), only the first n marks are used on both faces (not in sequence as in the original marks).

Here's a reproducible piece of code to illustrate the problem:

foo <- data.frame(yVal=factor(letters[1:8]), xVal=factor(rep(1:4,2)), fillVal=rnorm(8), facetVar=rep(1:2,each=4))
## axis labels are correct
p <- ggplot(foo) + geom_tile(aes(x=xVal, y=yVal, fill=fillVal)) + facet_grid(facetVar ~ ., scales='free')
print(p)
## axis labels are not set correctly
p <- p + scale_y_discrete(labels=c('a','a','b','b','c','d','d','d'))
print(p)

      

I note that I cannot set the shortcuts in the data.frame correctly as they are not unique. Also, I know that I can accomplish this using the arr.grid method, but it requires "manually" aligning the plots if there are different length labels, etc. Also, I would like to have facet labels included in the graph, which is not an option available with arr.grid's solution. Also, I haven't tried browsing yet. Maybe this is the solution, but I was hoping that for this plot you get more grand look and it seems to be more like grid.arrange.

It seems to me that this is a bug, but I am open to explaining how this could be a "feature". I also hope there can be a simple solution to this problem that I haven't thought of yet!

+3


source to share


2 answers


The easiest way is to create another column in your dataset with the correct transformation. It would also be easier to check and manipulate. If you insist on manual change:

You can't just set the labels directly, as it recycles (I think) a vector of labels for each face. Instead, you need to customize the transformation using the appropriate breaks and labels:



p <- p + scale_y_discrete(labels = c('1','2','3','4','5','6','7','8'), breaks=c('a','b','c','d','e','f','g','h'))
print(p)

      

Any y-axis value a

will now be replaced by 1

, b

by, 2

and so on. You can play around with the label values ​​to see what I mean. Just make sure that every factor value you have is also represented in the breaks argument.

+2


source


I think I actually have a solution. My problem was that my labels were wrong because, as someone said above, it looks like the label vector has been reworked. This line of code gave me the wrong labels.

ggplot(dat, aes(x, y))+geom_col()+facet_grid(d ~ t, switch = "y", scales = "free_x")+ylab(NULL)+ylim(0,10)+geom_text(aes(label = x))

      

However, when geom_text

was moved to facet_grid

, the code below gave me the correct labels.



ggplot(dat, aes(x, y))+geom_col()+geom_text(aes(label = x))+facet_grid(d ~ t, switch = "y", scales = "free_x")+ylab(NULL)+ylim(0,10)

      

There's a good chance, I may have misunderstood the problem above, but I definitely solved my problem, so hopefully this is helpful to someone!

0


source







All Articles