问题描述
我正在使用 ggplot 绘制散点图.我想要具有特定颜色和填充的点(例如在 plot
、colour="blue"、fill="cyan4"
中),但我不能不知道怎么做.到目前为止,我所拥有的是:
I'm doing an scatter plot using ggplot. I would like to have points with a particular colour and fill (in plot
, colour="blue", fill="cyan4"
, for ex.) but I can't find how. What I have to this point is:
ggplot(df, aes(own,method)) +
panel.configuration +
scale_shape_identity() + #to use the 'plot' shape format.
geom_point(aes(color = factor(label)), position = "jitter",size=3) +
(在之前的 geom_point
中,我尝试添加 shape=21
,就像我在 plot
中所做的那样)
(In previous geom_point
I tried adding shape=21
as I would have done in plot
)
scale_colour_manual(values=c("A"="chocolate3","B"="cyan4")) +
scale_fill_manual(values=c("A"="green", "B"="red")) + #DOES NOTHING...
xlim(7,47) + ylim(7,47)+ ... etc.
这是我在没有shape=21"的情况下得到的
This is what i get without "shape=21"
这就是我添加shape=21"时得到的结果.在这两种情况下,它都会忽略 scale_fill
This is what I get when I add "shape=21". In both cases it ignores scale_fill
我还尝试在 geom_point 中添加 fill=c("blue","red")
,但 R 抱怨:错误:设置美学的长度不兼容:形状、大小、填充".
I also tried adding fill=c("blue","red")
in geom_point, but R complains: "Error: Incompatible lengths for set aesthetics: shape, size, fill".
有什么关于如何获得它的建议吗?我的代码中的 scale_fill
有什么问题?
Any suggestions about how to get it? What is wrong with scale_fill
in my code?
非常感谢!
数据 (df) 看起来像:
Data (df) looks like:
21 15 A
24 16 A
24 17 A
28 14 A
24 15 A
22 15 A
20 18 A
24 18 A
34 9 B
38 12 B
41 19 B
42 13 B
36 12 B
40 17 B
41 14 B
37 12 B
40 13 B
37 15 B
35 15 B
推荐答案
您必须使用 21 到 25
的形状.这些是具有 colour
和 fill
属性的那些:
You'll have to use shapes from 21 to 25
. These are the ones that have colour
and fill
properties:
ggplot(df, aes(own, method)) +
geom_point(colour="white", shape=21, size = 4,
aes(fill = factor(label))) +
scale_fill_manual(values=c("blue", "cyan4"))
如果您还希望 colour
有不同的颜色,那么:
If you want different colours for colour
as well, then:
ggplot(df, aes(own, method)) +
geom_point(aes(colour=factor(label),
fill = factor(label)), shape=21, size = 4) +
scale_fill_manual(values=c("blue", "cyan4")) +
scale_colour_manual(values=c("white", "black"))
这篇关于ggplot中geom_point(scale_colour_manual)中的填充和边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!