我正在尝试更改图例项目的显示顺序.我花了大约一个小时在这里,没有结果.
这是一个示例设置:
library(ggplot2)
set.seed(0)
d <- data.frame(x = runif(3), y = runif(3), a = c('1', '3', '10'))
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的众多事情之一:
ggplot(d, aes(x = x, y = y)) +
geom_point(size=7, aes(color = a, order = as.numeric(a)))
Run Code Online (Sandbox Code Playgroud)
(当然,我天真的希望是传奇项目将以数字顺序显示:1,3,10.)
MrF*_*ick 20
ggplot通常会根据因子对您的因子值进行排序levels().你最好确保这是你想要的顺序,否则你将在R中使用很多功能,但是你可以通过操纵颜色比例来手动改变它:
ggplot(d, aes(x = x, y = y)) +
geom_point(size=7, aes(color = a)) +
scale_color_discrete(breaks=c("1","3","10"))
Run Code Online (Sandbox Code Playgroud)
图例标签的顺序可以通过重新排序列a中的值并将其更改为系数来进行操作:d$a <- factor(d$a, levels = d$a)
所以你的代码看起来像这样
library(ggplot2)
set.seed(0)
d <- data.frame(x = runif(3), y = runif(3), a = c('1', '3', '10'))
d$a <- factor(d$a, levels = d$a)
ggplot(d, aes(x = x, y = y)) +
geom_point(size=7, aes(color = a))
Run Code Online (Sandbox Code Playgroud)
请注意,比图例中的现在要多:1是红色,3是绿色,10是蓝色
| 归档时间: |
|
| 查看次数: |
20814 次 |
| 最近记录: |