有voronoi图并需要将它放在状态图中

时间:2014-03-06 14:29:08

标签: r ggplot2 voronoi

我在本网站上有人向我提供了以下代码,谢谢大家! 我需要将这个图表纳入德克萨斯州!到目前为止,我已经发布了我的代码,是否可以将其置于某个状态?

n <- c(30.60688, 30.28370, 31.08425, 29.29955, 32.71078)
k <- c(-96.35286, -97.73405, -97.34860, -94.79447, -97.36118)
mat <- cbind(n, k)
df <- as.data.frame(mat)
names(df) <- c('x','y')

# triangulate
xrng <- expand_range(range(df$x), .05)
yrng <- expand_range(range(df$y), .05)
deldir <- deldir(df, rw = c(xrng, yrng))

# voronoi
map('state', 'texas')
qplot(x, y, data = df)  +
  geom_segment(
    aes(x = x1, y = y1, xend = x2, yend = y2), size = .25,
    data = deldir$dirsgs, linetype = 2
  ) + 
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0))

1 个答案:

答案 0 :(得分:0)

做了一些研究......

library(ggplot2)
allStates <- map_data('state')
texas <- subset(allStates, region == 'texas')
head(texas)

# triangulate
n <- c(30.60688, 30.28370, 31.08425, 29.29955, 32.71078)
k <- c(-96.35286, -97.73405, -97.34860, -94.79447, -97.36118)
mat <- cbind(n, k)
df <- as.data.frame(mat)
names(df) <- c('x','y')

xrng <- expand_range(range(df$x), .05)
yrng <- expand_range(range(df$y), .05)
deldir_ds <- deldir(df, rw = c(xrng, yrng))$dirsgs

# voronoi
ggplot(texas, aes(x = long, y = lat))  + 
  geom_polygon(fill = 'grey25') +
  geom_segment(data = deldir_ds,
           aes(x = y1, y = x1, xend = y2, yend = x2), 
           size = .25, linetype = 2, color = rgb(229/255, 229/255, 229/255)) + 
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0)) +
  theme(panel.grid = element_blank())

enter image description here