简体   繁体   English

按形状、颜色和填充绘制的 R 散点图

[英]R scatter plot by shape, colour and fill

I'm very new to R and I'm trying to build a scatter plot that codes my data according to shape, colour and fill.I want 5 different colours, 3 different shapes, and these to be either filled or not filled (in an non filled point, I would still want the shape and the colour).我对 R 很陌生,我正在尝试构建一个散点图,根据形状、颜色和填充对我的数据进行编码。我想要 5 种不同的颜色、3 种不同的形状,这些要么填充要么不填充(在一个未填充的点,我仍然想要形状和颜色)。 My data looks basically like this:我的数据基本上是这样的:

blank.test <- read.table(header=T, text="Colour Shape Fill X13C X15N
1       B     B    A   16   10
2       D     A    A   16   12
3       E     A    B   17   14
4       C     A    A   14   18
5       A     A    B   13   18
6       C     B    B   18   13
7       E     C    B   10   12
8       E     A    B   11   10
9       A     C    B   14   13
10      B     A    A   11   14
11      C     B    A   11   10
12      E     B    A   11   19
13      A     B    A   10   18
14      A     C    B   17   16
15      E     B    A   16   13
16      A     C    A   16   14")

If I do this:如果我这样做:

ggplot(blank.test, aes(x=X13C, y=X15N,size=5)) + 
                    geom_point(aes(shape=Shape,fill=Fill,color=Colour))

I get no filled or unfilled data points我没有得到填充或未填充的数据点

在此处输入图片说明

I did a little a little research and it looked like the problem was with the symbols themselves, which cannot take different settings for line and fill;我做了一点研究,看起来问题出在符号本身,不能对线和填充进行不同的设置; it was recommended I used shapes pch between 21 and 25建议我使用 21 到 25 之间的形状 pch

But if I do this:但如果我这样做:

ggplot(blank.test, aes(x=X13C, y=X15N,color=(Colour), shape=(Shape),fill=(Fill),size=5)) + 
                 geom_point() + scale_shape_manual(values=c(21,22,25))`

I still don't get what I want我还是没有得到我想要的

在此处输入图片说明

I also tried playing around with scale_fill_manual without any good result.我也尝试过使用scale_fill_manual没有任何好的结果。

I don't think you can use fill for points.我不认为你可以使用填充点。 What I would do is create an interaction between fill and shape and use this new factor to define your shape and fill/open symbols我要做的是创建填充和形状之间的交互,并使用这个新因素来定义您的形状和填充/打开符号

blank.test$inter <- with(blank.test, interaction(Shape,  Fill))

and then for your plot I would use something like that然后对于你的情节,我会使用类似的东西

ggplot(blank.test, aes(x=X13C, y=X15N)) + 
                    geom_point(aes(shape=inter,color=Colour)) + scale_shape_manual(name="shape", values=c(0,15,1, 16, 2, 17)) + scale_color_manual(name="colour", values=c("red","blue","yellow", "green", "purple"))

I can get the plot to work just fine, but the legend seems to absolutely insist on being black for fill .我可以让情节正常工作,但传说似乎绝对坚持为fill黑色。 I can't figure out why.我不明白为什么。 Maybe someone else has the answer to that one.也许其他人有答案。

The 5 being on the legend is cause by having it inside the aes , where only elements that change with your data belong.图例上的5是由于将其放在aes引起的,其中只有随数据变化的元素才属于。

Here is some example code:下面是一些示例代码:

ggplot(blank.test, aes(x = X13C, y = X15N, color = Colour, shape = Shape, fill = Fill)) + 
  geom_point(size = 5, stroke = 3) + 
  scale_shape_manual(values=c(21,22,25)) +
  scale_color_brewer(palette = "Set2") +
  scale_fill_brewer(palette = "Set1") +
  theme_bw()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM