简体   繁体   中英

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).

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(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:

图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:

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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