在ggplot2之上覆盖基础R图形 [英] Overlay base R graphics on top of ggplot2

查看:157
本文介绍了在ggplot2之上覆盖基础R图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ggplot中有一个阴谋,我希望覆盖我用R代码创建的地图图例。我不知道如何在ggplot上叠加基本的R图形,并且会很感激的帮助。

I have a plot in ggplot and I wish to overlay a map legend that I have created with base R code. I do not know how to overlay base R graphics on top of a ggplot and would be grateful for assistance.

目前我有一个ggplot图例,如下所示:

Currently I have a ggplot legend that looks like this:

有几件事我不喜欢这个传说,我想改变(这导致我认为使用基本R图形来做这件事情会更容易)。

There are several things that I do not like about this legend that I would like to change (and that resulted in me thinking it would be easier to resort to base R graphics to do so).

特别是,我希望消除图例中框之间的空白区域,并且希望在框之间添加刻度线。我还希望在分隔第一个和第二个盒子的第一个刻度线下面放置5;分隔第二个和第三个盒子的刻度线下方的10;分隔第三个和第四个盒子的刻度线下面有20。我还希望使图例中的框与我的情节(我使用stat_bin2d图层)中的箱之一相同。

In particular, I wish to eliminate the white space between the boxes in the legend and I wish to add tick marks between the boxes too. I also wish to put "5" beneath the first tick mark separating the first and second boxes; "10" beneath the tick mark separating the second and third boxes; and "20" beneath the tick mark separating the third and forth boxes. I also wish to make the boxes in the legend the same size as one of the "bins" in my plot (I use the stat_bin2d layer).

相关代码:

Relevant code:

ggplot()+
stat_bin2d(restaurants.df,binwidth=c(1500,2500), alpha=0.6,aes(x=X,y=Y,fill=cut(..count.., c(0,5,10,20,Inf))))+
scale_fill_manual("No. of Restaurants",labels=c("<5","5-10","10-20",">20"),values=cols, guide = guide_legend(direction = "horizontal", title.position = "top",                                      ticks=TRUE,label.position="bottom")) + 
theme(legend.background = element_rect(colour = "black"),
    legend.key = element_rect(color='black'))


推荐答案

@ baptiste的评论让我感兴趣试图创造一个将成为传奇的情节。这是我的尝试。我使用 geom_tile 创建一个将成为图例的图。 OP没有提供样本数据,所以我使用内置的 mtcars 数据创建了一个图表,只是为了让图标紧挨着。然后我使用> grid.arrange 创建最终的剧情加图例布局。

@baptiste's comment got me interested in trying to create a plot that would become the legend. Here's my attempt. I use geom_tile to create a plot that will become the legend. The OP didn't provide sample data so I've created a plot using the built-in mtcars data, just to have something to put the legend next to. Then I use grid.arrange to create the final plot-plus-legend layout.

library(ggplot2)
library(grid)
library(gridExtra) 

## Create legend

# Fake data
dat = data.frame(x=1:4, y="Group", col=factor(1:4))

# Create a plot that will become the legend
legend = ggplot(dat, aes(x,y, fill=col)) + 
  geom_tile(show.legend=FALSE) +
  scale_x_continuous(breaks=c(1.5,2.5,3.5), labels=c(5,10,20)) +
  scale_fill_manual(values=c("yellow","orange","red","darkred")) +
  labs(y="", x="") +
  ggtitle("No. of Restaurants") +
  theme_bw() +
  theme(panel.border=element_blank(),
        axis.ticks.y=element_blank(),
        axis.text.y=element_blank())

## Create a plot to put next to the legend
p1 = ggplot(mtcars, aes(mpg, wt)) + 
  geom_point() +
  theme(plot.margin=unit(c(0,0,0,0)))

# Arrange plot and legend
grid.arrange(p1, arrangeGrob(rectGrob(gp=gpar(col=NA)), 
                             legend,
                             rectGrob(gp=gpar(col=NA)),
                             heights=c(0.42,0.16,0.42)), 
             widths=c(0.8,0.2))

这篇关于在ggplot2之上覆盖基础R图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆