在圆圈中传播重叠点 - R.

时间:2015-10-15 20:59:10

标签: r overlap points scatter jitter

我在R中有许多重叠的点 - >我的想法是创建一个新的坐标列,我将它们展开成一个圆圈。

我不想抖动;它看起来很丑陋并且具有误导性 - >它使观众认为数据实际上就是这样,而不是像刚才那样提供可见性。

我认为一个圆圈或向日葵或星星传播看起来不错,这就是我想做的事情。由于地理预测的原因,我认为我的工作效果不佳:

Before

After

示例坐标

(INPUT):

Latitude    Longitude
51.52328    -0.1570965
51.52328    -0.1570965
51.52328    -0.1570965
51.52328    -0.1570965
51.52328    -0.1570965

输出:

new_lat new_lng
51.50815    -0.1545583
51.53691    -0.1620067
51.51205    -0.1501359
51.53138    -0.1656516
51.51884    -0.1475074

我的代码:

#http://geepeeex.com/LongitudesAndLatitudes.htm
#UK (122/78)
radius_size = 0.001
lat_radius_size = radius_size*(122/78)
many_stations$new_lat <- many_stations$Latitude
many_stations$new_lng <- many_stations$Longitude

for (i in unique(many_stations$Station)) {
# Get group-length = N
group_length = length(which(many_stations$Station == i))
#Cos/Sin take degrees not radians
circle_chunk = (360/group_length)
angle = circle_chunk
# If duplicates:
  if(group_length>1) {
    print(paste('group_length: ',group_length))
    # Loop within the group
    for (j in which(many_stations$Station == i)) {
      print(paste('row: ',j))

      many_stations[j,]$new_lng <- many_stations[j,]$Longitude + sin(angle)*radius_size
      many_stations[j,]$new_lat <- many_stations[j,]$Latitude + cos(angle)*lat_radius_size

      angle = angle + circle_chunk
    }
  }
}

2 个答案:

答案 0 :(得分:2)

正如我在评论中提到的那样

## convert polar to cartesian
p2c <- function(radius, theta, deg = FALSE) {
  if (deg)
    theta <- theta * (pi / 180)
  list(x = radius * cos(theta),
       y = radius * sin(theta))
}

## convert cartesian to polar
c2p <- function(x, y, deg = FALSE) {
  list(radius = sqrt(x ** 2 + y ** 2),
       theta = atan2(y, x) * if (deg) 180 / pi else 1)
}

## convert to polar, add rad to radius and spread points, convert back
pdodge <- function(x, y, rad = 1) {
  stopifnot((lx <- length(x)) == length(y))
  p <- c2p(x, y)
  p <- within(p, {
    radius <- radius + rad
    theta  <- theta + rescaler(seq.int(lx + 1), c(0,359))[-(lx + 1)]
  })
  p2c(p$radius, p$theta, TRUE)
}

rescaler <- function(x, to = c(0, 1), from = range(x, na.rm = TRUE))
  (x - from[1]) / diff(from) * diff(to) + to[1]

set.seed(1)
par(mfrow = c(2,2), mar = c(5,5,2,1), las = 1)
pts <- rep(0, 10)
pl <- function(...) plot(..., xlim = c(-.5,.5), ylim = c(-.5,.5))

pl(pts, pts)
pl(jitter(pts), pts)
# pl(pts, jitter(pts))
pl(jitter(pts), jitter(pts))

pts <- pdodge(pts, pts, rad = .15)
pl(pts$x, pts$y)

enter image description here

答案 1 :(得分:0)

原来我只是忘记转换为弧度,因此下面的工作(rawr&#39的方法也适用于我 - 所以谢谢你!)

radius_size = 0.001
many_stations$new_lat <- many_stations$Latitude
many_stations$new_lng <- many_stations$Longitude

for (i in unique(many_stations$Station)) {
# Get group-length = N
group_length = length(which(many_stations$Station == i))
circle_chunk = (360/group_length)
angle = circle_chunk
# If duplicates:
  if(group_length>1) {
    print(paste('group_length: ',group_length))
    # Loop within the group
    for (j in which(many_stations$Station == i)) {
      print(paste('row: ',j))

      many_stations[j,]$new_lng <- many_stations[j,]$Longitude + sin((pi/180)*angle)*radius_size
      many_stations[j,]$new_lat <- many_stations[j,]$Latitude + cos((pi/180)*angle)*radius_size

      angle = angle + circle_chunk
    }
  }
}