首页 > 解决方案 > 使用 ggplot 对两个变量进行排序并重新排序

问题描述

标签: rggplot2

解决方案


We can use paste

ggplot(df_full_short,aes(x=reorder(unit, paste(var1, var2)), y=dev)) + 
      ...

Or may be change the unit levels based on the 'var1', 'var2'

library(dplyr)
df_full_short %>%
         arrange(var1, var2) %>%
         mutate(unit = factor(unit, levels = unique(unit))) %>%
          ggplot(aes(x = unit, y = dev)) + 
           ...

推荐阅读