如何将R语言abacus_plot的Y轴改为从上到下降序排列
调整glatos包abacus图Y轴排序方向的方法
方法一:修改数据中纬度列的因子水平顺序
abacus_plot会依据location_col的因子水平顺序排列Y轴,只需将deploy_lat转换为因子并指定降序水平,就能让高纬度值显示在Y轴顶部:
library(glatos) # 将deploy_lat转为因子,按降序设置水平 detections_w_events$deploy_lat <- factor( detections_w_events$deploy_lat, levels = sort(unique(detections_w_events$deploy_lat), decreasing = TRUE) ) # 重新绘制abacus图 abacus_plot(detections_w_events, location_col= 'deploy_lat', ylab = 'Latitude', main='Detections by Station', x_res = "12 weeks", x_format = "%b-%y", col = detections_w_events$color)
方法二:绘图后反转Y轴(基于base plot特性)
若不想修改原始数据,可通过隐藏默认Y轴、手动绘制反转轴的方式实现:
library(glatos) # 绘制abacus图,隐藏默认Y轴 abacus_plot(detections_w_events, location_col= 'deploy_lat', ylab = 'Latitude', main='Detections by Station', x_res = "12 weeks", x_format = "%b-%y", col = detections_w_events$color, yaxt = 'n') # 获取纬度值并降序排序 lat_levels <- sort(unique(detections_w_events$deploy_lat), decreasing = TRUE) # 绘制反转后的Y轴 axis(side = 2, at = 1:length(lat_levels), labels = lat_levels, las = 1) # 调整Y轴范围实现反转 ylim(c(length(lat_levels), 1))
内容的提问来源于stack exchange,提问作者Jackie Reuder
相关产品推荐
相关产品推荐

