我知道这个问题一直在asked/answered其他地方,但我非常困惑如何/在哪里应用代码来重新排序我的y轴上的小平面网格中的变量。
数据:
df <- data.frame(
type = c("Small", "X-large", "Medium", "Large", "Small", "X-large", "Medium", "Large", "Small", "X-large", "Medium", "Large"),
group = c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C"),
value = c(22, 40, 31, 60, 26, 24, 22, 18, 30, 70, 60, 50)
)图:
plot <- ggplot(df, aes(y=type, size = 15)) + facet_grid(group ~ ., scales="free_y", space="free_y")
plot <- plot + geom_point(aes(x=value),
size=3)
plot

我希望每个面的y轴上的变量从上到下以不同的顺序(小,中,大,x-大)排列(而不是当前的顺序:x-大,小,中,大)。我如何改变这一点?我知道我的答案应该是这样的:df$new = factor(df$type, levels=c("Small","Medium","Large","X-large"), labels=c("Small","Medium","Large","X-large")),但我不确定应该把它放到我的图形代码中的哪里。我试着把它放在‘类型’的地方,但是没有用…任何帮助我们都将不胜感激!
发布于 2021-07-06 11:24:44
这是一个重复的问题,因此它将被关闭,但这个示例应该可以帮助您理解问题:
library(tidyverse)
df <- data.frame(
type = c("Small", "X-large", "Medium", "Large", "Small", "X-large", "Medium", "Large", "Small", "X-large", "Medium", "Large"),
group = c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C"),
value = c(22, 40, 31, 60, 26, 24, 22, 18, 30, 70, 60, 50)
)
df$type <- factor(df$type, levels = c("X-large", "Large", "Medium", "Small"))
ggplot(df, aes(y=type, size = 15)) +
facet_grid(group ~ ., scales = "free_y", space = "free_y")+
geom_point(aes(x = value), size = 3)

https://stackoverflow.com/questions/68264120
复制相似问题