Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent facet labeller behaviour when using characters to facet #1070

Closed
naught101 opened this issue Mar 19, 2015 · 3 comments
Closed

Comments

@naught101
Copy link

library(data.table)
library(ggplot2)

# Labeller function
iris_lblr <- function(var, value) {
    labels <- c('setosa'='SET', 'versicolor'='VERS', 'virginica'='VIRG')
        if (var=='Species') {
            return ( labels[value])
        }
}

# using data.table just for ease of filtering/mutation
dt <- data.table(iris)

# This plot works fine - short caps labels are applied correctly
ggplot(dt, aes(x=Sepal.Length, y=Petal.Length)) + geom_point() + facet_grid(.~Species, labeller=iris_lblr)

# this plot also has the correct CAPS labels applied, and the middle facet is missing
ggplot(dt[Species!='versicolor'], aes(x=Sepal.Length, y=Petal.Length)) + geom_point() + facet_grid(.~Species, labeller=iris_lblr)

dt[, Species:=as.character(Species)]

# This plot shows "VERS" where it should show "VIRG". 
ggplot(dt[Species!='versicolor'], aes(x=Sepal.Length, y=Petal.Length)) + geom_point() + facet_grid(.~Species, labeller=iris_lblr)
@hadley
Copy link
Member

hadley commented Jun 10, 2015

Could you please create a minimal reprex without data table?

@naught101
Copy link
Author

library(dplyr)
library(ggplot2)

# Labeller function
iris_lblr <- function(var, value) {
    labels <- c('setosa'='SET', 'versicolor'='VERS', 'virginica'='VIRG')
    if (var=='Species') {
        return ( labels[value])
    }
}

# This plot works fine - short caps labels are applied correctly
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length)) + geom_point() + facet_grid(.~Species, labeller=iris_lblr)

# this plot also has the correct CAPS labels applied, and the middle facet is missing
ggplot(iris %>% filter(Species!='versicolor'), aes(x=Sepal.Length, y=Petal.Length)) + geom_point() + facet_grid(.~Species, labeller=iris_lblr)

df <- iris %>% mutate(Species=as.character(Species))

# This plot shows "VERS" where it should show "VIRG". 
ggplot(df %>% filter(Species!='versicolor'), aes(x=Sepal.Length, y=Petal.Length)) + geom_point() + facet_grid(.~Species, labeller=iris_lblr)

@hadley
Copy link
Member

hadley commented Jul 23, 2015

Hmmm, that's because value is a factor, not a character vector, as you expect. I'm probably missing a stringsAsFactors = FALSE somewhere.

@hadley hadley closed this as completed in ac40d16 Jul 23, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants