作者:且羞且笑且动心细 | 来源:互联网 | 2022-10-23 12:52
以下R片段
library(ggplot2)
library(reshape2)
data <- data.frame(
item = c('foo', 'bar', 'baz'),
val_1 = c( 7 , 9 , 3 ),
val_2 = c( 1 , 2 , 3 )
);
data
data$tot = data$val_1 + data$val_2;
data.molten = melt(data);
ggplot(
data = data.molten,
aes(x = variable, y = item )) +
geom_tile(aes(fill = value)) +
geom_text(aes(label = value))
产生
是否有可能tot
按降序对它进行排序,使得with的行在bar
顶部,baz
在底部。
1> joran..:
添加此行:
data$item <- reorder(data$item,data$tot)
在melt
ing 之前。
关于此主题的规范StackOverflow问题在这里,答案基本上始终是“按所需顺序设置因子水平”,但实际操作方式可能因情况而异,但超出了范围。一个StackOverflow答案。