Ordering the axis labels in geom_tile

后端 未结 5 1117
离开以前
离开以前 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 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

提交回复
热议问题