Indicate three diferent values in a scatterplott using colors

Viewed 57

I have the following task from my professor:

Use ggplot2 and create another scatterplot of the variablesact_sm and pop_sm. This time the actors from the US (3), UK (4), and the transnational actors (888) should be indicated with different colors. Make sure there is a legend to the plot. Label the axes reasonably.

I have the following data.frame named nodes:

actor_type actor_ctr actor_pos pop_sm act_sm        str
1            3         4         2    799   1087  1.3604506
102         NA       999        NA     78     90  1.1538462
103         NA       999        NA     78     90  1.1538462
104          3         4         2    441     NA         NA
105          3         4         2   1049    789  0.7521449
106          4        75         1    264    948  3.5909091
107          4        75         1    264    948  3.5909091
4            3         4         2    799   1087  1.3604506
5            3         4         2    799   1087  1.3604506
6            3         4         2    799   1087  1.3604506
7            4         3        NA    158    212  1.3417722
8            4         3        NA    158    212  1.3417722
    ...

I want to create a scatter plot from the values of act_sm and pop_sm in which I want to indicate actor_ctr 3, 4 and 888 with different colored dots each - all the other values from actor_ctr should have one color. I have tried it like that but it is not working:

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

Furthermore I do not know how to insert a legend.

1 Answers

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

Related