简体   繁体   English

根据字符值更改散点图 plot 中的一个点或几个点的形状 R

[英]Changing the shape of one point or few points in a scatter plot in R based off of character values

I would like to have a scatter plot where different shape sizes show the different localities (only two localities).我想要一个散点图 plot,其中不同的形状大小显示不同的地点(只有两个地点)。

The data is presented here:数据显示在这里:

Sample Name εNd stratigraphic column    sample number
B8  -4.887223024    8.34    ND
B7B8    -5.723139056    8.27    ND
MO2400  -6.639290394    7.2 M
SHARK 2 -6.349256446    7   M
M.FISH  -6.100998712    7.1 M
MO2376  -7.689046402    -6  M
ABOVE UPPER -8.306593254    -6.53   ND
NIO 38  -8.90688489 -8.07   ND
NIO 39  -7.243909917    -8.16   ND
T-4 TO T-5  -7.700379025    -12 ND
T-2 TO T-3  -8.532019275    -12.47  ND
T1 TO T0    -6.894687774    -13.1   ND

I have tried using this code:我试过使用这段代码:

ggplot(data=nd, aes(x=nd$εNd, y= nd$`stratigraphic column`)) + geom_point(size=4, shape=factor(nd$`sample number`))+ggtitle(expression(epsilon*"Neodymium in Manitoba Escarpment"))+xlab(~epsilon*"Nd")

And it gives me this graph:它给了我这张图:

图1

I didn't like how the shapes looked, and had no legend.我不喜欢这些形状的外观,也没有图例。 So I tried going a different route using the group in ggplot:所以我尝试使用 ggplot 中的组走不同的路线:

ggplot(data=nd, aes(x=εNd, y= `stratigraphic column`, group='sample number')) + geom_point(aes(shape='sample number'), size=4)+ scale_shape_manual(values=c(16,17))+ ggtitle(expression(epsilon*"Neodymium in Manitoba Escarpment")) + xlab(~epsilon*"Nd")+ylab("Stratigraphic Position")

And I got this as an output graph:我得到了这个 output 图表:

图2

This one does not change the shapes based off of location, but has a legend.这个不会根据位置改变形状,但有一个图例。

I have tried changing my data type for the 'sample number' column from character to factor, and that didn't help.我曾尝试将“样本编号”列的数据类型从字符更改为因子,但这没有帮助。

Thanks in advance.提前致谢。

You need to map your sample number to the shape aesthetic:你需要 map 你的样品编号到形状美学:

ggplot(nd, aes(εNd, `stratigraphic column`)) + 
  geom_point(size = 4, aes(shape = `sample number`)) +
  ggtitle(expression(epsilon*"Neodymium in Manitoba Escarpment")) +
  xlab(~epsilon*"Nd") +
  theme_light()

在此处输入图像描述


Data in reproducible form as taken from question从问题中获取的可重现形式的数据

nd <- structure(list(`Sample Name` = c("B8", "B7B8", "MO2400", "SHARK 2", 
"M.FISH", "MO2376", "ABOVE UPPER", "NIO 38", "NIO 39", "T-4 TO T-5", 
"T-2 TO T-3", "T1 TO T0"), eNd = c(-4.887223024, -5.723139056, 
-6.639290394, -6.349256446, -6.100998712, -7.689046402, -8.306593254, 
-8.90688489, -7.243909917, -7.700379025, -8.532019275, -6.894687774
), `stratigraphic column` = c(8.34, 8.27, 7.2, 7, 7.1, -6, -6.53, 
-8.07, -8.16, -12, -12.47, -13.1), `sample number` = c("ND", 
"ND", "M", "M", "M", "M", "ND", "ND", "ND", "ND", "ND", "ND")), 
class = "data.frame", row.names = c(NA, -12L))

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

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