r语言 - 如何更改一个图形中绘图的顺序



在这里你可以看到我的代码的切口。

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"))

最新更新