3

これはggplotにあまりにも多くを求めているのではないかと思います。facet_gridを使用して、関連する地理的領域にいくつかのグラフをプロットしようとしています。結果は順調に見えますが、スケールを正しく設定し、未使用のグリッドボックスを削除する際にいくつかの問題があります。誰かがそれを調整して機能させる方法を知っているのだろうか?

この例は、組み込みの米国の州のデータセットに基づいています。

どんな援助もありがたいです!

    # My failed attempt to regional summary graphs plotted over the region centroid (to overlay onto map):
# example based on the States dataset

require(ggplot2)

# Approx centroid coordinates for the US regions - I created these as the information is missing from the dataset
lat <- c(32L, 42L, 42L, 32L, 42L, 42L, 43L, 32L, 32L, 32L, 42L, 42L, 43L, 43L, 43L, 43L, 32L, 32L, 43L, 32L, 43L, 43L, 43L, 32L, 43L, 42L, 43L, 42L, 43L, 43L, 42L, 43L, 32L, 43L, 43L, 32L, 42L, 43L, 43L, 32L, 43L, 32L, 32L, 42L, 43L, 32L, 42L, 32L, 43L, 42L)
lon <- c(-88L, -114L, -114L, -88L, -114L, -114L, -73L, -88L, -88L, -88L, -114L, -114L, -91L, -91L, -91L, -91L, -88L, -88L, -73L, -88L, -73L, -91L, -91L, -88L, -91L, -114L, -91L, -114L, -73L, -73L, -114L, -73L, -88L, -91L, -91L, -88L, -114L, -73L, -73L, -88L, -91L, -88L, -88L, -114L, -73L, -88L, -114L, -88L, -91L, -114L)

region.centres <- data.frame(lon = lon, lat = lat)

state.x77 <- data.frame(state.x77)
state.x77$State <- row.names(state.x77)
region <- data.frame(Region = as.character(state.region))
data <- data.frame(c(data.frame(state.x77), region, region.centres))

# I can plot a random summary graph for each region:

p <- ggplot(data, aes(Income, colour = 'red')) +
  geom_density(alpha = 0.2)

# But trying to plot onto centroid nearly works but results in loss of geographical positional accuracy
# and includes various graphs that fill the empty grid slots but are not necessary
p + facet_grid(lat ~ lon, space = "free", drop=T)

# How can I get uniform discrete graphs to sit over the centroid for each region?
4

1 に答える 1

0

おそらく、アノテーションはファセットよりもユースケースに適しているでしょう。独自のコードの後に​​次のことを試してください。注釈には多くの手動操作が必要ですが、作業を完了する必要があります。

library(maps)
states = data.frame(map("state", plot=FALSE)[c("x", "y")])
data.south = subset(data, lon == -88 & lat == 32)
overlay = qplot(data.south$Income, geom="density", color="red")
plot = (ggplot(states, aes(x, y))
        + geom_path()
        + annotation_custom(grob=ggplotGrob(overlay),
                            xmin=-88-2,
                            xmax=-88+2,
                            ymin=32-6,
                            ymax=32+6))

print(plot)

annotation_customggplot20.9.0で追加されました。見る

http://cloud.github.com/downloads/hadley/ggplot2/guide-col.pdf

于 2012-08-30T13:06:54.640 に答える