Question

I have code below given to me by someone on this site, and thank you all for that! I need to incorporate this diagram into the great state of Texas! I have my code posted so far, is it a stretch to put it in a state?

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))
Était-ce utile?

La solution

Did some research...

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top