Ordering the axis labels in geom_tile

后端 未结 5 1118
离开以前
离开以前 2020-12-28 09:31

I have a data frame containing order data for each of 20+ products from each of 20+ countries. I have put it in a highlight table using ggplot2 with code simila

相关标签:
5条回答
  • 2020-12-28 09:48

    maybe this StackOverflow question can help:

    Order data inside a geom_tile

    specifically the first answer by Brandon Bertelsen:

    "Note it's not an ordered factor, it's a factor in the right order"

    It helped me to get the right order of the y-axis in a ggplot2 geom_tile plot.

    0 讨论(0)
  • 2020-12-28 09:52

    As smillig says, the default is already to order the axes alphabetically, but the y axis will be ordered from the lower left corner up.

    The basic rule with ggplot2 that applies to almost anything that you want in a specific order is:

    • If you want something to appear in a particular order, you must make the corresponding variable a factor, with the levels sorted in your desired order.

    In this case, all you should need to do it this:

    mymelt$variable <- with(mymelt,factor(variable,levels = rev(sort(unique(variable)))))
    

    which should work regardless of whether you're running R with stringsAsFactors = TRUE or FALSE.

    This principle applies to ordering axis labels, ordering bars, ordering segments within bars, ordering facets, etc.

    For continuous variables there is a convenient scale_*_reverse() but apparently not for discrete variables, which would be a nice addition, I think.

    0 讨论(0)
  • 2020-12-28 09:56

    Another possibility is to use fct_reorder from forecast library.

    library(forecast)
    mydf %>%
    pivot_longer(cols=c('all regions', 'americas', 'europe')) %>% 
      mutate(name1=fct_reorder(name, value, .desc=FALSE)) %>% 
      ggplot( aes(x = industry, y = name1, fill = value)) +
      geom_tile() + geom_text(aes( label = value))
    
    0 讨论(0)
  • 2020-12-28 10:00

    The y-axis on your chart is also already ordered alphabetically, but from the origin. I think you can achieve the order of the axes that you want by using xlim and ylim. For example:

    ggplot(mymelt, aes(x = industry, y = variable, fill = value)) +
        geom_tile() + geom_text(aes(fill = mymelt$value, label = mymelt$value)) +
        ylim(rev(levels(mymelt$variable))) + xlim(levels(mymelt$industry))
    

    will order the y-axis from all regions at the top, followed by americas, and then europe at the bottom (which is reverse alphabetical order, technically). The x-axis is alphabetically ordered from all industries to steel with cars in between.

    enter image description here

    0 讨论(0)
  • 2020-12-28 10:05

    Maybe a little bit late,

    with(mymelt,factor(variable,levels = rev(sort(unique(variable)))))
    

    this function doesn't order, because you are ordering "variable" that has no order (it's an unordered factor).

    You should transform first the variable to a character, with the as.character function, like so:

    with(mymelt,factor(variable,levels = rev(sort(unique(as.character(variable))))))
    
    0 讨论(0)
提交回复
热议问题