要按特定值对ggplot堆叠条形图进行排序,可以使用以下步骤:
library(ggplot2)
library(dplyr)
df <- data.frame(Category = c("A", "B", "C", "D"),
Value1 = c(10, 15, 12, 8),
Value2 = c(5, 8, 10, 6))
df <- arrange(df, desc(Value1))
ggplot(df, aes(x = Category, y = Value1, fill = Category)) +
geom_col()
完整的代码示例如下:
library(ggplot2)
library(dplyr)
df <- data.frame(Category = c("A", "B", "C", "D"),
Value1 = c(10, 15, 12, 8),
Value2 = c(5, 8, 10, 6))
df <- arrange(df, desc(Value1))
ggplot(df, aes(x = Category, y = Value1, fill = Category)) +
geom_col()
运行以上代码将按Value1的值对条形图进行排序。
下一篇:按特定值对数组进行排序和关联