You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询:使用scale_color_brewer为何未改变geom_dotplot点颜色

问题描述

使用ggplot2绘制geom_dotplot时,已添加scale_color_brewer(palette = "Paired")指定配色,但图中点的颜色未发生改变。

相关信息:

  • 脚本:
psbsrd <- read_csv("psbs rd.csv")

psbsrd$gen=factor(psbsrd$gen,levels=c("WT","PsbS","T259A","T259D","T259E","T259S","S265-","T79A/S104A/T259A","T79D/S104E/T259D"))

ggplot(psbsrd, aes(x=gen, y=rd,color=gen)) + 
  scale_color_brewer(palette = "Paired")+
   geom_dotplot(binaxis='y', stackdir='center',
               stackratio=1, dotsize=0.7)+theme(text = element_text(size = 20))+theme(legend.position = "none")+ 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))+
  labs(x= "",  y="PsbS relative density")
  • 数据框:
data.frame(psbsrd)
   id              gen        rd
1   1               WT 1.0000000
2   2             PsbS 1.2211420
3   3             PsbS 1.5918041
4   4             PsbS 2.1668718
5   5             PsbS 2.6198157
6   6             PsbS 1.5571007
7   7             PsbS 1.0520551
8   8             PsbS 0.4648043
9   9             PsbS 1.9454836
10 10            S265- 1.5497361
11 11            S265- 3.2732440
12 12            S265- 4.3479497
13 13            S265- 4.8951613
14 14            S265- 6.2919204
15 15            S265- 7.7251320
16 16 T79A/S104A/T259A 0.1784396
17 17 T79A/S104A/T259A 0.5977111
18 18 T79A/S104A/T259A 0.5307654
19 19 T79A/S104A/T259A 3.0679723
20 20 T79D/S104E/T259D 1.2229263
21 21            T259A 0.9812587
22 22            T259A 0.9647552
23 23            T259A 0.9001399
24 24            T259A 1.6814516
25 25            T259A 2.3471329
26 26            T259A 2.6215385
27 27            T259D 2.9000576
28 28            T259D 2.8604196
29 29            T259D 3.3292308
30 30            T259D 3.3822005
31 31            T259D 2.9608392
32 32            T259D 2.6813986
33 33            T259D 2.6746853
34 34            T259D 1.6584615
35 35            T259D 2.9927273
36 36            T259E 1.4003248
37 37            T259E 1.6272838
38 38            T259E 1.8465286
39 39            T259E 3.5475230
40 40            T259E 2.2281770
41 41            T259E 2.1733658
42 42            T259S 4.4847350
43 43            T259S 4.9366626
44 44            T259S 2.2005684
问题原因与解决方法
  • 原因:geom_dotplot默认用fill参数控制点的填充颜色,你在aes里指定的color=gen仅控制点的边框颜色,默认边框极细,所以看不到明显变化。
  • 解决方法:
    1. 将aes中的color=gen改为fill=gen;
    2. 把scale_color_brewer替换为scale_fill_brewer(palette = "Paired");
    3. 若需要区分点的边框,可额外指定color参数(比如固定为黑色)。

修改后的脚本示例:

psbsrd <- read_csv("psbs rd.csv")

psbsrd$gen=factor(psbsrd$gen,levels=c("WT","PsbS","T259A","T259D","T259E","T259S","S265-","T79A/S104A/T259A","T79D/S104E/T259D"))

ggplot(psbsrd, aes(x=gen, y=rd, fill=gen)) + 
  scale_fill_brewer(palette = "Paired")+
  geom_dotplot(binaxis='y', stackdir='center',
               stackratio=1, dotsize=0.7, color="black")+ # 可选:添加黑色边框区分点
  theme(text = element_text(size = 20))+
  theme(legend.position = "none")+ 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))+
  labs(x= "",  y="PsbS relative density")

内容的提问来源于stack exchange,提问作者wychin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 19:01:09