Tag: factor

  • Change the label of legend not by changing…

    hitta singlar gratis Change the label of legend not by changing the data.

    # data grp <- gl(n=4,k=20,labels=c("group a","group b","group c", "group d")) value <- runif(n=80, min=10, max=150) outcome <- cut(value,2) data <- data.frame(grp,value,outcome) # Option 1 # breaks should be exactly the same as the levels of the factor ggplot(data, aes(grp, fill=outcome)) + geom_bar() +xlab("group") + ylab("number of subjects") + scale_fill_discrete("Serologic response", breaks=c("(12.1,79.7]","(79.7,147]"), # should be the same as the levels of the factor labels=c("double negative", "positive for a and/or b") ) # Option 2 # simpler because factor() takes care of the levels of the factor ggplot(data, aes(grp, fill=factor(outcome,labels=c("double negative","positive for a")))) + geom_bar() +xlab("group") +ylab("number of subjects") + labs(fill="Serologic response")

    http://stackoverflow.com/questions/7323191/how-do-i-manually-change-the-key-labels-in-a-legend-in-ggplot2

  • R factors levels labels # sample data originally…

    R factors, levels, labels

    # sample data originally numbers
    # 1 = male, 2 = female
    
    people.n <- c(1, 1, 2, 2, 2)
    people.c <- c("male", "male", "female", "female", "female")
    
    # create a factor
    people.n.f <- factor(people.n, levels = c(1, 2), labels = c("male", "female"))
    people.c.f <- factor(people.c, levels = c("male", "female"), labels = c("male", "female"))
    
    # if you want to change the order of the levels, 
    people.n.f <- factor(people.n, levels = c(2, 1), labels = c("female", "male"))
    
    # levels follows alphabetical order if not specified
    # compare this with people.c.f
    factor(people.c)
    
    # without specifying levels and labels
    # the original data will be used
    factor(people.n)
    
    # once used to create a factor, levels and labels are merged.
    # there is no labels attribute in factor.
    # correct
    levels(people.c.f)
    levels(people.c.f) <- c("man", "woman")
    people.c.f
    
    #wrong
    labels(people.c.f)
    labels(people.c.f) <- c("man", "woman")
    

    Conclusion
    By default, factor() order the levels alphabetical order or numerical order.
    If you want to change the sorting order of levels manually, you need to set it when the factor is created.
    It can not be changed later, even though the levels can be changed.

    When creating a factor, the data and the order should be matched as the example.

    factor(people.c, levels = c("male", "female"), labels = c("male", "female")) 
              ^ ---- same data ---------^    ^-------------same order -----^
    

  • Object in R Factor can be used for sequ …

    Object in R
    Factor can be used for sequence data.

    From sequence characters
    seq <- c("A", "C", "C", "T", "G") seq.factor <- factor(seq, levels=c("A", "G", "T", "C") From numbers to sequences > seq.num <- c(1,2,3,1,1,3,2,1,4,1,3,2,1) > seq.num.factor <- factor(seq.num, levels=c(1,2,3,4), labels=c("A","G","T","C") )