I want to create a plot where the two facets have (i) different x-axes and (ii) the order or the x-axis is maintained from the order of occurrence in the dataframe.
Sample code:
part <- as.factor(c("A","A","A","B","B","B"))
word <- c("dog", "cat", "fish", "hamster", "dog", "eagle")
order <- c(1,2,3,4,5,6)
val <- c(1,2,3,3,2,1)
df <- as.data.frame(cbind(part,order,word,val))
ggplot(df, aes(x=word, y=val))+
facet_wrap(part, scales = "free_x")+
geom_point()
Which produces
But, the order isn't the same as what's specified in the word variable. The order of words should be "dog", "cat", "fish" for Part A and "hamster", "dog", "eagle" for Part B.
I can get the correct order when I
But I want the corresponding words to be on the x-axis... What am I doing wrong?
I have checked out other related solutions like this one, but 'scales = "free_x"' doesn't do the trick by itself. Also, if I change "word" into an ordered factor, it runs into issues too, since "dog" is the first word in Part A but the second word in Part B.