我有一个如下创建的因数:
myfactor<-c("A","B","C","D")
myfactor<-factor(myfactor)
我将该因子用于value
列的多个数据集:
factor(datasetA$value,levels=myfactor)
factor(datasetB$value,levels=myfactor)
数据集A
Type count variable value
1: 1 235 1 A
2: 2 31 1 A
3: 3 28 1 B
4: 4 113 1 B
5: 5 40 1 C
数据集B
Type count variable value
1: 1 235 1 B
2: 2 31 1 B
3: 3 28 1 B
4: 4 113 1 B
5: 5 40 1 C
我的问题
当我使用命令绘制数据集A时
ggplot(data=datasetA, aes(x=as.factor(variable),y=count,fill = value)) +
geom_bar(position = "stack", stat = "identity", width = 1) +
scale_fill_manual(values=c("#4ceb34","#ebd034","#eb34dc","#34ebc3","#3452eb","#eb3434"))
我得到以下图例:
当我绘制数据集B时,图例如下所示
我想在色阶和颜色之间建立绑定:A应该是绿色,B应该是黄色... 我该怎么办?
答案 0 :(得分:0)
技巧是将 var s = new Test();
s.SelectProperty(r => r.Brand)
.AddExpression(x => x == "Test")
.AddExpression(r => r == "Test2")
.Build(new Product())
.IsSatisfied();
分配给命名向量:
values
请注意,不必强迫myfactor <- c("A", "B", "C", "D", "E", "F")
vals <- setNames(c("#4ceb34","#ebd034","#eb34dc","#34ebc3","#3452eb","#eb3434"), myfactor)
进行分解,需要的是为其指定名称的颜色。
然后,在图中使用图层
myvector