对于geom_boxplot和geom_bar的排列/排序,可以使用ggplot2包中的arrange()函数和reorder()函数来实现。
library(ggplot2)
# 创建一个数据框
df <- data.frame(x = c(rep("A", 10), rep("B", 10), rep("C", 10)),
y = rnorm(30))
# 对x列进行排序
df$x <- factor(df$x, levels = c("C", "A", "B"))
# 使用arrange()函数对x列进行排序
df <- arrange(df, x)
# 绘制geom_boxplot,并按照x列进行排列/排序
ggplot(df, aes(x = x, y = y)) +
geom_boxplot()
library(ggplot2)
# 创建一个数据框
df <- data.frame(x = c(rep("A", 10), rep("B", 10), rep("C", 10)))
# 使用table()函数计算每个类别的频数
count <- table(df$x)
# 使用reorder()函数对x列进行排序
df$x <- reorder(df$x, count)
# 绘制geom_bar,并按照x列进行排列/排序
ggplot(df, aes(x = x)) +
geom_bar()
这些示例代码中,我们首先创建了一个数据框df,然后使用arrange()函数(对geom_boxplot)或reorder()函数(对geom_bar)对x列进行排序,最后使用ggplot()函数绘制图形。
下一篇:按照另一个2D列表筛选2D列表