如何更改一张图中绘图的顺序?

How to change order of plots within one graph?

在这里你可以看到我的代码的截图。

ratiox = seq(0, 3, 0.05)
x = rnorm(61, 55000, 3)
y1 = rep(200, times = 61)

l = cbind(ratiox, x, y1)
l = as.data.frame(l)
l$y1 = as.character(l$y1)

y1 = rep(100, times = 61)

m = cbind(ratiox, x, y1)
m = as.data.frame(m)
m$y1 = as.character(m$y1)

y1 = rep(50, times = 61)
n = cbind(ratiox, x, y1)
n = as.data.frame(n)
n$y1 = as.character(n$y1)

y1 = rep(25, times = 61)

o = cbind(ratiox, x, y1)
o = as.data.frame(o)
o$y1 = as.character(o$y1)

total = rbind(l,m,n,o)
View(total)

ggplot(total, aes(x = ratiox, y = y1, height = x/100000, fill = y1)) + 
  geom_ridgeline() + scale_fill_manual(values = c("green", "gray", "lightblue", "red", "blue", "black")) + xlab("Emission ratio") + 
  ylab("") + theme_classic(base_size = 25) 

最后图表的顺序不符合我的预期。我希望各种浓度 (y1) 的顺序对应于输入的顺序。含义:200、100、50 和 25。我希望我的变量 y1 保持为一个字符。我不想将其转换为数字变量。

将您的 y1 变量转换为一个因子,并根据需要对级别进行排序。

total$y1 <- factor(total$y1, levels = c("200", "100", "50", "25"))