JackWassik
JackWassik

Reputation: 75

Color and shape coding within ggplot

Working with a chemical dateset and what I want to do is to color code the geom_points by the depth at which they were sampled from and then make the shape based on when it was sampled from. I also want to add a thin black border on all the geom_points in order to distinguish them.

Here is a sample table:

    ID   Depth(m)   Sampling Date   Cl    Br
    1       1             May       4.0   .05
    2       1             June      5.0   .07
    3       2             May       6.0   .03
    4       2             June      7.0   .05
    5       3             May       8.0   .01
    6       3             June      9.0   .03
    7       4             May       10.0  .00
    8       4             June      11.0  .01

I am trying to use the code

    graph <- df %>%
    ggplot(aes(x = Cl, y = Br, fill = Depth, shape = Sampling Date), color = black) +
    geom_point(shape = c(21:24, size = 4) +
    labs(x = "Cl", y = "Br")

    graph

But everytime I do this it just fills in the shape black ignoring the color specification. Also I need to use the shapes 21:25 but everytime I try to specify the number of shapes it always says that it doesn't match the number of variables within my dataset.

Upvotes: 0

Views: 75

Answers (1)

tjebo
tjebo

Reputation: 23727

Your code is somewhat filled with ... challenges. Remove all spaces! That makes your life easier. Also add shape aes to geom_point and specify the shapes with a scale call.

library(ggplot2)
df <- read.table(text = "ID   Depth   SamplingDate   Cl    Br
    1       1             May       4.0   .05
    2       1             June      5.0   .07
    3       2             May       6.0   .03
    4       2             June      7.0   .05
    5       3             May       8.0   .01
    6       3             June      9.0   .03
    7       4             May       10.0  .00
    8       4             June      11.0  .01", header = T)


  ggplot(df, aes(x = Cl, y = Br, fill = Depth, shape = SamplingDate)) +
  geom_point(aes(shape = SamplingDate), size = 4) +
    scale_shape_manual(values = 21:24)

Created on 2020-07-30 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions