R ggplot2 |绘制方向方向的 Lat Long 坐标数据和轴中的 ID
R ggplot2 | Plotting direction-wise Lat Long coordinate data and IDs in axis
当包含 week_days
的数据框绘制在 ggplot2 上或在 table 中可视化时,星期几从星期五开始按字母顺序 axis/table 从左到右排列。这可以通过设置 factor(week_days, levels= c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
手动配置。 (可能有更好的方法,我不知道。)我在绘制 coordinate_IDs
时尝试找出类似的解决方案,其中包含沿轴方向的纬度和经度。
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)
大家可以看到,最东端的ID是5,最西端的是1,东边依次是6、3、4、8、9、2、7。还有,有几对ID非常接近。
test_df %>%
ggplot() +
geom_point(aes(x = ID, y = Value))
在 ggplot2 上绘图时,x 轴上的 ID 值在东西方向上从 9 变为 1。
但我希望它在轴上从右到左遵循 5、6、3、4、8、9、2、7、1 模式——像这样:
这是我为上述情节所做的尝试:
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))
我不想通过在沿着走廊绘制 100 个 ID 时设置级别来手动执行此操作。所以,也许写一个函数会是一种更有效的方法。我所能想到的只是一个伪代码,但可能有更简单的方法来解决这个问题?
getDirectionalLevels <- function(test_df, direction){
grab(Lat, Long) %>%
mutate(id_direction = sort(direction)) %>%
mutate(ID = factor(ID, levels = id_direction))
}
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)
您似乎想根据 Long
值对 ID
进行排序。
test_df %>%
mutate(ID = factor(ID, levels = test_df[order(test_df$Long), "ID"])) %>%
ggplot() +
geom_point(aes(x = ID, y = Value))
或:
test_df %>%
mutate(ID = factor(ID, levels = test_df %>% arrange(Long) %>% select(ID) %>% unlist())) %>%
ggplot() +
geom_point(aes(x = ID, y = Value))
两个return:
当包含 week_days
的数据框绘制在 ggplot2 上或在 table 中可视化时,星期几从星期五开始按字母顺序 axis/table 从左到右排列。这可以通过设置 factor(week_days, levels= c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
手动配置。 (可能有更好的方法,我不知道。)我在绘制 coordinate_IDs
时尝试找出类似的解决方案,其中包含沿轴方向的纬度和经度。
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)
大家可以看到,最东端的ID是5,最西端的是1,东边依次是6、3、4、8、9、2、7。还有,有几对ID非常接近。
test_df %>%
ggplot() +
geom_point(aes(x = ID, y = Value))
在 ggplot2 上绘图时,x 轴上的 ID 值在东西方向上从 9 变为 1。
但我希望它在轴上从右到左遵循 5、6、3、4、8、9、2、7、1 模式——像这样:
这是我为上述情节所做的尝试:
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))
我不想通过在沿着走廊绘制 100 个 ID 时设置级别来手动执行此操作。所以,也许写一个函数会是一种更有效的方法。我所能想到的只是一个伪代码,但可能有更简单的方法来解决这个问题?
getDirectionalLevels <- function(test_df, direction){
grab(Lat, Long) %>%
mutate(id_direction = sort(direction)) %>%
mutate(ID = factor(ID, levels = id_direction))
}
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)
您似乎想根据 Long
值对 ID
进行排序。
test_df %>%
mutate(ID = factor(ID, levels = test_df[order(test_df$Long), "ID"])) %>%
ggplot() +
geom_point(aes(x = ID, y = Value))
或:
test_df %>%
mutate(ID = factor(ID, levels = test_df %>% arrange(Long) %>% select(ID) %>% unlist())) %>%
ggplot() +
geom_point(aes(x = ID, y = Value))
两个return: