我想在3页的PDF文件中安排3x3网格布局.我想在三个页面中的任意网格位置绘图.我知道如何使用某些选项来安排多个子图layout=c(3,3)
.我可以弄清楚如何使用网格包在单个绘图上排列3x3布局,然后决定选择要使用的绘图.但是,我无法弄清楚如何在3页上布置3x3网格,然后选择要绘制的网格.
我希望grid.newpage()
能解决我的问题,如下所示:
library(grid)
pdf(file="griddtest.pdf",paper="letter")
vp1 <- viewport(x = 0, y = 0.5, w = 0.5, h = 0.5, just = c("left", "bottom"),
name = "vp1")
vp2 <- viewport(x = 0, y = 0.5, w = 0.5, h = 0.5, just = c("left", "bottom"),
name = "vp2")
pushViewport(vp1)
grid.text("Some drawing in graphics region 1 on page 1",y = 0.8)
grid.newpage()
pushViewport(vp2)
grid.text("Some drawing in graphics region 2 on page 2",y = 0.8)
dev.off()
Run Code Online (Sandbox Code Playgroud)
但这只是生成第二页(我猜'新页'会覆盖旧页面,而不是制作新页面).
任何帮助将不胜感激!
我想在现有情节的一个小区域中拟合整个格子图.
xyplot(decrease ~ treatment, OrchardSprays, groups = rowpos, type = "a")
pushViewport(viewport(.2, .7, .2, .2))
grid.rect(gp=gpar(fill="white"))
Run Code Online (Sandbox Code Playgroud)
给我这个:
因此视口被推动.但是当再次调用晶格函数时,它会使用整个器件,就像调用grid.newpage()
:
xyplot(decrease ~ treatment, OrchardSprays, groups = rowpos, type = "a")
Run Code Online (Sandbox Code Playgroud)
有没有办法将晶格图限制在设备上的预定义区域,就像上面的例子一样?
我需要使用ggplot2中的facet_wrap()将希腊字母放入facet标签中.我发现了一个描述facet_grid()的链接.我使用以下代码将其应用于我的数据:
levels(parameters) <- c(expression(alpha), expression(beta))
p + facet_grid(.~parameters, labeller = label_parsed)
Run Code Online (Sandbox Code Playgroud)
这很有效,完全符合我的要求.但是,我需要使用facet_wrap()(为两个参数获取单独的y轴,并在不同的列和行中绘制更多参数).我尝试了以下方法:
p + facet_wrap(.~parameters, labeller = label_parsed) , or
p + facet_wrap(.~parameters)
Run Code Online (Sandbox Code Playgroud)
但是这没有用,因为facet_wrap中没有"贴标机"功能.如何使用网格完成?
是否有可能"强制" grid
包装中的R基础图grid.newpage
?例如,这工作正常:
library(grid)
grid.newpage()
vp1 <- viewport(x=0,y=0.5,width=0.5, height=0.5, just = c("left", "bottom"))
vp2 <- viewport(x=0.5,y=0,width=0.5, height=0.5, just = c("left", "bottom"))
pushViewport(vp1)
grid.rect()
grid.text("vp1", 0.5, 0.5)
upViewport()
pushViewport(vp2)
grid.rect()
grid.text("vp2", 0.5, 0.5)
Run Code Online (Sandbox Code Playgroud)
.
但如果我尝试这样的事情:
grid.newpage()
vp1 <- viewport(x=0,y=0.5,width=0.5, height=0.5, just = c("left", "bottom"))
vp2 <- viewport(x=0.5,y=0,width=0.5, height=0.5, just = c("left", "bottom"))
pushViewport(vp1)
grid.rect()
print(plot(1,2))
grid.text("vp1", 0.5, 0.5)
upViewport()
pushViewport(vp2)
grid.rect()
print(plot(1,2))
Run Code Online (Sandbox Code Playgroud)
R基础情节刚刚过度骑行grid.newpage
.使用par(new=T)
也无济于事.
我只是想制作一个简单的研究区域地图,其中还包含我正在工作的州(北卡罗来纳州)的插图.我想将插图地图转换为grob对象以在主要研究区域地图中绘制它,然后使用ggsave将地图保存为图像,pdf等.我正在使用shapefile作为我的实际地图,但我会告诉你我正在尝试使用map_data:
library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)
library(gridExtra)
library(grid)
# get the NC data:
states <- map_data("state")
nc_df <- subset(states, region == "north carolina")
# study area map:
nc_base <- ggplot() +
geom_polygon(data = nc_df, aes(x = long, y = lat, group = group), fill="grey", color="black") +
coord_fixed(xlim=c(-80, -77.5), ylim=c(33.5, 34.9), ratio = 1.3) +
theme_bw()
nc_base
# inset map:
insetmap<-ggplot() +
geom_polygon(data = nc_df, aes(x = long, y = lat, group = group), fill="grey", color="black") + # get the state border …
Run Code Online (Sandbox Code Playgroud) 我想ggplot2
从列表中创建多个绘图,grid.arrange
但是在按行排列之前按行排列.
gg_list1 <- list(qplot(mpg, disp, data = mtcars),
qplot(hp, wt, data = mtcars),
qplot(qsec, wt, data = mtcars))
gg_list2 <- list(qplot(mpg, disp, data = mtcars),
qplot(hp, wt, data = mtcars),
qplot(qsec, wt, data = mtcars))
Run Code Online (Sandbox Code Playgroud)
我知道我可以这样做:
do.call(grid.arrange,c(gg_list1,gg_list2 , ncol = 2, nrow = 3))
Run Code Online (Sandbox Code Playgroud)
但它从上到下从左到右填充.
我试过这个:
do.call(grid.arrange, c(gg_list1, arrangeGrob(gg_list2, nrow = 3), ncol = 2))
Run Code Online (Sandbox Code Playgroud)
但是得到 Error: length(widths) == ncol is not TRUE
有任何想法吗?
我正在尝试创建一个新的geom,它将根据飓风数据创建一个风半径图表.
运行此操作所需的数据来自以下内容:
storm_observation <- data_frame(longitude = c(-89.6, -89.6, -89.6),
latitude = c(29.5, 29.5, 29.5),
wind_speed = c("34", "50", "64"),
ne = c(200, 120, 90),
nw = c(100, 75, 60),
se = c(200, 120, 90),
sw = c(150, 75, 60))
Run Code Online (Sandbox Code Playgroud)
我创建新Geom的代码包含在下面,但是抛出了与fontsize相关的奇怪错误:
check.length(gparname)出错:'gpar'元素'fontsize'的长度不能为0
我试图在default_aes
和gpar()
函数中包含fontsize ,但它仍然导致相同的错误.任何帮助,将不胜感激.注:此要求tidyr
,dplyr
和geosphere
包.
GeomHurricane <- ggproto("GeomPolygon", Geom,
required_aes = c("x", "y", "r_ne", "r_se", "r_nw", "r_sw",
"fill", "colour"),
default_aes = aes(scale_radii = 0.8, alpha = 0.8, linetype = 1, size = …
Run Code Online (Sandbox Code Playgroud) 我希望在多面地图的绘图区域之外添加比例尺和指北针。作为示例,请考虑以下多面地图图,如 Max Marchi 的博客文章(链接)所示:
# Load the data
airports <- read.csv("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat", header = FALSE)
colnames(airports) <- c("ID", "name", "city",
"country", "IATA_FAA", "ICAO", "lat", "lon",
"altitude", "timezone", "DST")
routes <- read.csv("https://github.com/jpatokal/openflights/raw/master/data/routes.dat", header = FALSE)
colnames(routes) <- c("airline", "airlineID",
"sourceAirport", "sourceAirportID",
"destinationAirport", "destinationAirportID",
"codeshare", "stops", "equipment")
# Getting the data ready for plotting
# * For a detailed explanation on setting up the data I suggest consulting Max Marchi's post:
# http://www.milanor.net/blog/maps-in-r-plotting-data-points-on-a-map/
library(plyr)
departures <- ddply(routes, .(sourceAirportID), "nrow")
names(departures)[2] <- …
Run Code Online (Sandbox Code Playgroud) 这看起来类似于@baptiste在2014年试图解决的问题.我正在重新审视我在6月写回的代码,其中涉及创建三个ggplotGrobs并将它们与cbind的调用相结合.现在,此代码失败并显示消息:"mmm中的错误<每个:未实现这些类型的比较."
我认为这将特定于我的特定应用程序,但我能够制作一个非常简单,可重现的示例.即使cbind是在两个相同的ggplotGrobs上执行的,这段代码仍然会失败.
library(ggplot2)
library(gtable)
# Make some plots
pl1 <- ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point()
pl2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point()
# Convert to grobs
pl1_grob <- ggplotGrob(pl1)
pl2_grob <- ggplotGrob(pl2)
# Bind them together -- Error!
combined_grob <- cbind(pl1_grob, pl2_grob)
Run Code Online (Sandbox Code Playgroud)
错误和相关回溯在这里:
> combined_grob <- cbind(pl1_grob, pl2_grob)
Error in mmm < each : comparison of these types is not implemented
> traceback()
8: comp(x_val, y_val)
7: unit(comp(x_val, y_val), …
Run Code Online (Sandbox Code Playgroud) 如何使用ggarrange
?
根据我拥有的六个图(p1 到 p6),我认为以下方法可行:
p6 <- p6 + theme(legend.title = element_text(size = 15, face = "bold")
Run Code Online (Sandbox Code Playgroud)
以下ggarrange
用于组合六个图:
p <- ggarrange(p1, p2, p3, p4, p5, p6,
common.legend = TRUE,
legend = "bottom",
labels = c("1", "2", "3", "4", "5", "6"),
# font.label = list(size = 10, color = "green"),
nrow = 2, ncol = 4
)
Run Code Online (Sandbox Code Playgroud)
然而,这根本不会改变常见的传说。