Indicate three diferent values in a scatterplott using colors

A few problems with your code.

  1. Missing a close-paren after filter(...c().

  2. Either use nodes %>% filter(actor_ctr...) or filter(nodes, actor_ctr...), not both.

  3. Don’t reference nodes in the call to ggplot. There are times when you can (and should) reference an external dataframe in ggplot layers, this is not one of them. Here’s what is actually being interpreted:

    nodes %>%
      filter(actor_ctr %in% c(3,4,888)) %>%
      ggplot(data = ., mapping = nodes, aes(act_sm, pop_sm)) +
      geom_point(aes(x= act_sm, y= pop_sm, colour = actor_ctr), size=2)
    

    where . refers to the 8 rows of data after filter(...), and nodes refers to your frame before the pipeline started. While . is not formally assigned to a variable, it is still the state of the data at that point in the pipe. Here, it’s 8 rows with all of the same columns, but it’s not hard to come up with examples where . and origdata have completely different shape and properties.

    Further, because of the positional args there, mapping= should certainly not be your nodes dataframe.

  4. Your color aesthetic is based on a number, so ggplot2 is going to infer that it is continuous, whereas I believe it is in fact ordinal or categorical (discrete). This can often be fixed using factor(actor_type).

Let’s try to fix all of those:

nodes %>%
  filter(actor_ctr %in% c(3, 4, 888)) %>%
  ggplot(aes(act_sm, pop_sm)) + 
  geom_point(aes(x = act_sm, y = pop_sm, colour = factor(actor_ctr)), size = 2)

ggplot2, discrete color aesthetic

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top