R汇集了传统的情节和ggplot2 [英] R put together traditional plot and ggplot2

查看:195
本文介绍了R汇集了传统的情节和ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张图,这张图是用 ggplot2 来描绘的:

  w <-ggplot()+ 
geom_polygon(data = dep_shp.df,aes(x = long,y = lat,group = group,fill = classJenks))+

#scale_fill_gradient(limits = c(40,100))+
labs(title =Classification de la proportion de producteurs pardépartements
\\\
par la methode de jenks(2008))+
theme_bw()+
coord_equal()

code> classIntervals 来自 classInt 库。



我想把这2个图表放在一起。我试过:

  vplayout<  -  function(x,y)viewport(layout.pos.row = x,layout。 pos.col = y)
grid.newpage()
pushViewport(viewport(layout = grid.layout(1,2)))

#creation
print (u,vp = vplayout(1,1))
print(v,vp = vplayout(1,2))

grid.arrange

  grid.arrange(plot1,plot2,ncol = 2)

但这些都不起作用。 p>

解决方案

该方法在网格视口中的嵌入基本图形绘图部分中进行了描述的 gridBase vignette
$ b

gridBase 包中包含用于为基图的绘图区域设置合理参数的函数。所以我们需要这些包:

 库(grid)
库(ggplot2)
库(gridBase)

下面是一个ggplot示例:



<$ p $ ($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

在设置 par 之前,诀窍似乎是调用 plot.new ,否则它有责任感到困惑和不正确的荣誉设置。您还需要设置 new = TRUE ,这样当您调用 plot 时,新页面不会启动。

 #创建图形窗口和布局
plot.new()
grid.newpage()
pushViewport (viewport(layout = grid.layout(1,2)))

#Draw ggplot
pushViewport(viewport(layout.pos.col = 1))
print(a_ggplot ,newpage = FALSE)
popViewport()

#Draw bsae plot
pushViewport(viewport(layout.pos.col = 2))
par(fig = gridFIG (),new = TRUE)
with(cars,plot(speed,dist))
popViewport()


I have 2 graphs, a map plotted with ggplot2 like this:

w<-ggplot()+
  geom_polygon(data=dep_shp.df, aes(x=long,y=lat,group=group,fill=classJenks))+

  #   scale_fill_gradient(limits=c(40, 100))+
  labs(title ="Classification de la proportion de producteurs par départements
       \n par la methode de jenks (2008)")+
  theme_bw()+
  coord_equal()

and a graph as object of type classIntervals from the classInt library.

I would like to put together this 2 graphs. I have tried:

vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))

#creation
print(u, vp = vplayout(1, 1))
print(v, vp = vplayout(1, 2))

And something with grid.arrange

grid.arrange(plot1, plot2, ncol=2)

but none of these work.

解决方案

The method is described in the Embedding base graphics plots in grid viewports section of the gridBase vignette.

The gridBase package contains functions to set sensible parameters for the plotting region of the base plot. So we need these packages:

library(grid)
library(ggplot2)
library(gridBase)

Here's an example ggplot:

a_ggplot <- ggplot(cars, aes(speed, dist)) + geom_point()

The trick seems to be to call plot.new before you set par, otherwise it's liable to get confused and not correctly honour the settings. You also need to set new = TRUE so a new page isn't started when you call plot.

#Create figure window and layout
plot.new()
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))

#Draw ggplot
pushViewport(viewport(layout.pos.col = 1))
print(a_ggplot, newpage = FALSE)
popViewport()

#Draw bsae plot
pushViewport(viewport(layout.pos.col = 2))
par(fig = gridFIG(), new = TRUE)
with(cars, plot(speed, dist))
popViewport()

这篇关于R汇集了传统的情节和ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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