Home > database >  R ggplot2 | Plotting direction-wise Lat Long coordinate data and IDs in axis
R ggplot2 | Plotting direction-wise Lat Long coordinate data and IDs in axis

Time:10-16

When data frame containing week_days are plotted on ggplot2 or visualized in a table, the week days go from left to right on the axis/table alphabetically, starting from Friday. This can be manually configured by setting factor(week_days, levels= c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"). (There might be a better way that I don't know.) I'm trying figure out a similar solution when plotting coordinate_IDs that contain latitudes and longitudes in a direction-wise manner along the axis.

install.packages("pacman")
pacman::p_load(tidyverse, leaflet)

ID <- as.factor(as.character(1:9))
Lat <- as.numeric(c("33.55302", "33.55282", "33.55492", "33.55498", "33.55675", "33.55653", "33.55294", "33.55360", "33.55287"))
Long <- as.numeric(c("-112.0910", "-112.0741", "-112.0458", "-112.0459", "-112.0414", "-112.0420", "-112.0869", "-112.0526", "-112.0609"))
Value <- 11:19

test_df <- data.frame(ID, Lat, Long, Value)

levels(test_df$ID)

test_df %>% 
    leaflet() %>% 
    addTiles() %>% 
    addMarkers(., lng = Long, lat = Lat,
               label = ~ID)

As you can see, the ID on the eastern-most end is 5, the western-most 1, with 6, 3, 4, 8, 9, 2, and 7 in between from the east. Also, a few pairs of the IDs are very close to each other.

test_df %>% 
    ggplot()   
    geom_point(aes(x = ID, y = Value))

When plotting on ggplot2, the ID values on the x-axis go from 9 to 1 in east-west direction.

enter image description here

But I want this to follow the 5, 6, 3, 4, 8, 9, 2, 7, 1 pattern in the axis from right to left—something like this:

enter image description here

Here's what I tried for the aforementioned plot:

test_df %>% 
    mutate(ID = factor(ID, levels = c("1","7","2","9","8","4","3","6","5"))) %>% 
    ggplot()   
    geom_point(aes(x = ID, y = Value))

I don't want to do this manually by setting the levels when plotting, say, 100 IDs along a corridor. So, perhaps writing a function would be a more efficient approach. All I could come up with is a pseudo-code, but there might be a much simpler way to approach this problem?

getDirectionalLevels <- function(test_df, direction){
    grab(Lat, Long) %>% 
        mutate(id_direction = sort(direction)) %>% 
        mutate(ID = factor(ID, levels = id_direction))
}

CodePudding user response:

ID <- as.factor(as.character(1:9))
Lat <- as.numeric(c("33.55302", "33.55282", "33.55492", "33.55498", "33.55675", "33.55653", "33.55294", "33.55360", "33.55287"))
Long <- as.numeric(c("-112.0910", "-112.0741", "-112.0458", "-112.0459", "-112.0414", "-112.0420", "-112.0869", "-112.0526", "-112.0609"))
Value <- 11:19

test_df <- data.frame(ID, Lat, Long, Value)

It seems as if you wanted to sort the ID along the Long value.

test_df %>% 
  mutate(ID = factor(ID, levels = test_df[order(test_df$Long), "ID"])) %>% 
  ggplot()   
  geom_point(aes(x = ID, y = Value))

Or:

test_df %>% 
  mutate(ID = factor(ID, levels = test_df %>% arrange(Long) %>% select(ID) %>% unlist())) %>% 
  ggplot()   
  geom_point(aes(x = ID, y = Value))

Both return:

enter image description here

  • Related