Color points on a map concerning different values in a column

Hello :slight_smile:

In my dataset about violences against civilians there are many columns : one column is called ACTOR1 and there are many values concerning the a lot of different actors reported in this specific column ( for exemple RUF ; AFRC; Militia…etc).

I would like to know how to plot point on a map with differents colors because basically all my points are black on my map and I would like that points are colored concerning the type of actors (listed in the column ACTOR1)

Thank you very much in advance :slight_smile:

Normally it works :slight_smile:

df <- read_xlsx("sll-ledsierre-leonelocalsource1991-2001.xlsx")

map <- qmap("Sierraleone" , zoom = 8)

only violence against civilians

df1<- df

install.packages("dplyr")
library(dplyr)
install.packages("magrittr")
library(magrittr)

df1 <- filter(df1, df1$EVENT_TYPE == "Violence against civilians")

#plot the crime points on top
map + geom_point(data = df1,
aes(x = LONGITUDE, y = LATITUDE), color = "black", size=1, alpha=0.5)

You just have to map that variable to the colour aesthetic, with something like this

geom_point(data = df1, aes(x = LONGITUDE, y = LATITUDE, colour = ACTOR1), size=1, alpha=0.5)

If you need more specific help, please provide a minimal REPRoducible EXample (reprex) including sample data on a copy/paste friendly format as explained in the link provided.

thanks you, so I have to create before the variable ACTOR1 ?

I don't understand, you have said that ACTOR1 is already present in your dataset, why you would have to create it?

As I said before, please provide a reproducible example so we can give you better help.

Sorry :s I never learned this software I try to understand...
i get this message now

Error: Don't know how to add ggplot(data = df1, aes(x = LONGITUDE, y = LATITUDE)) to a plot

ACTOR1 LATITUDE LONGITUDE

1 RUF: Revolutionary United Front 8.11 -11.6
2 RUF: Revolutionary United Front 8.11 -11.6
3 RUF: Revolutionary United Front 7.96 -11.7
4 RUF: Revolutionary United Front 7.96 -11.7
5 RUF: Revolutionary United Front 7.96 -11.7
6 RUF: Revolutionary United Front 7.96 -11.7

library("readxl", lib.loc = "C:\Users\Student\Downloads\sll-ledsierre-leonelocalsource1991-2001.xlsx")
library("ggplot2")
library("ggmap")

qmap("Sierraleone" , zoom = 8)

df <- read_xlsx("sll-ledsierre-leonelocalsource1991-2001.xlsx")

map <- qmap("Sierraleone" , zoom = 8)

only violence against civilians

df1<- df

install.packages("dplyr")
library(dplyr)
install.packages("magrittr")
library(magrittr)

df1 <- filter(df1, df1$EVENT_TYPE == "Violence against civilians")

map + ggplot(data = df1, aes(x = LONGITUDE, y = LATITUDE)) +
geom_point(alpha = 0,5, size = 1) +
aes(colour=ACTOR1)

thank you for your help and patience it's nice :slight_smile:

Have you tried to follow the guide I gave you about how to make a reprex? By not providing one you are making unnecessarily difficult to help you.

This is the best I can do for you with the info you are providing.

# Sample data on a copy/paste friendly format
df <- data.frame(stringsAsFactors=FALSE,
                 ACTOR1 = c("RUF: Revolutionary United Front",
                            "RUF: Revolutionary United Front", "Another Actor",
                            "RUF: Revolutionary United Front", "Another Actor",
                            "RUF: Revolutionary United Front"),
                 LATITUDE = c(8.11, 8.2, 7.96, 8, 8.2, 8.3),
                 LONGITUDE = c(-11.6, -11.8, -11.7, -11.7, -11.7, -11.7)
)

# Library calls
library(ggmap)

# Relevant code
register_google(Sys.getenv("google_key")) # You have to replace this with your own google API key

map <- qmap("Sierraleone", zoom = 8)

map +
    geom_point(data = df, aes(x = LONGITUDE, y = LATITUDE, colour = ACTOR1), size=3, alpha=0.5) +
    theme(legend.position = "bottom")

Created on 2019-06-24 by the reprex package (v0.3.0)

1 Like

It's ok I get my map thank to your help, even if it was a little bit complicated to follow replex procédures :stuck_out_tongue:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.