0

地図上の指定した場所に ggplot グラフを配置したいと考えています。ggplot2私がパッケージを選んだのは、 よりもよく知っているからですgrid。誰かがgridそのようなタスクに使用する方法の小さな例で私を助けてくれれば、そのような答えにも感謝します.

簡単な例を次に示します。

# create base plot
g <- ggplot(data.frame(x=c(-104,-94), y=c(33,38)), aes(x=x, y=y)) + 
  geom_blank()

# create theme
tm <- theme(axis.title = element_blank(),
            axis.text = element_blank(),
            axis.ticks = element_blank(),
            axis.line = element_blank(),
            panel.background = element_blank(),
            panel.grid = element_blank(),
            panel.border = element_rect(color="grey", fill=NA),
            title = element_text(size=5))

# create two plot which should be placed on the base plot
p1 <- ggplot(data.frame(x=c(-104,-94), y=c(33,38)), aes(x=x, y=y)) + 
  geom_point() + tm
p2 <- ggplot(data.frame(x=c(-100,-98), y=c(34,37)), aes(x=x, y=y)) + 
  geom_point() + tm

# place them using annotation_custom() function
a1 <- annotation_custom(grob = ggplotGrob(p1), 
                        xmin = -104, xmax = -102,
                        ymin = 33, ymax = 35)
a2 <- annotation_custom(grob = ggplotGrob(p2), 
                        xmin = -100, xmax = -98,
                        ymin = 35, ymax = 37)

# draw
g + a1
g + a2
g + a1 + a2

ここに画像の説明を入力 ここに画像の説明を入力

しかし、最初のプロットのみが挿入されg + a1 + a2た最初の画像を取得する場合。p1どうしたの?を使用して 2 つ以上のプロットを描画する方法はannotation_custom()?

4

1 に答える 1

0

It's a weird bug that I noticed recently; for some grobs that ggplot2 considers similar, the position can be ignored and they end up superposed:

myGrob <- rectGrob(gp=gpar(fill="red", alpha=0.5), name="whatever")
myGrob2 <- rectGrob(gp=gpar(fill="blue", alpha=0.5))

# this is fine
qplot(1:10, 1:10) + theme(plot.margin=unit(c(0, 3, 0, 0), "cm")) +
  annotation_custom(myGrob, xmin=5, xmax=6, ymin=3.5, ymax=5.5) +
  annotation_custom(myGrob2, xmin=8, xmax=12, ymin=3.5, ymax=5.5) 

# using twice the same grob, they just sit on top of each other
p <- qplot(1:10, 1:10) + theme(plot.margin=unit(c(0, 3, 0, 0), "cm")) +
  annotation_custom(myGrob, xmin=5, xmax=6, ymin=3.5, ymax=5.5) +
  annotation_custom(myGrob, xmin=8, xmax=12, ymin=3.5, ymax=5.5) 

g <- ggplotGrob(p)
grid.ls(g$grobs[[4]]) # two of them

I have no idea what's causing this, but presumably it's related to your problem.

于 2013-07-22T10:57:06.387 に答える