B.Quaink
B.Quaink

Reputation: 574

R ggplot2: continous scale based on index

I have a large dataframe (94k+ values) with lat/lon values. Short example:

df <- data.frame(lat = c(50, 60, 70, 80, 90), 
                 lon = c(25, 28, 30, 32, 35))

But because the original dataframe is so large, I want to fill in the colour of the plotted points (with geom_point) based on the index value. So it shows the path of the observations over time with a gradient.

Current ggplot:

ggplot(data = df) +
  geom_point(aes(x = lon, y = lat)

Upvotes: 0

Views: 174

Answers (2)

TarJae
TarJae

Reputation: 78927

Are you looking for such a solution?

df <- data.frame(lat = c(50, 60, 70, 80, 90), 
                 lon = c(25, 28, 30, 32, 35))

library(tidyverse)
df <- df %>% 
  mutate(id_as_index = row_number())

ggplot(data = df, aes(x = lon, y = lat, color=factor(id_as_index))) +
  geom_point() + 
  theme_bw ()

enter image description here

Upvotes: 0

Elle
Elle

Reputation: 1008

To make an index:

df$index <- 1:nrow(df)

Graph:

ggplot(data = df) + geom_point(aes(x = lon, y = lat, colour = index))

Upvotes: 0

Related Questions