我想在几个ggplot对象的中心放置一个注释.
我研究过并发现了一些类似的问题,例如:ggplot2中geom_text的相对定位?
到目前为止,我发现的唯一答案是操纵绝对范围(例如",y = ymax/2").
我想在打印到.pdf之前在循环中添加注释图层.我可以使用+/- Inf将注释放在角落中,如下所示:
plot.list<-list()
g<- qplot(1,1)
plot.list[[length(plot.list)+1]]<-g
plot.list[[length(plot.list)+1]]<-g
pdf("MyReport.pdf"
,width = 14
,height=8.5
,paper="a4r")
for(i in 1:length(plot.list)){
print(plot.list[[i]]+
annotate("text",x=Inf,y=Inf,hjust=1,vjust=1
,label="PLEASE DO NOT DISTRIBUTE"
,fontface="bold",color="darkred",alpha=0.3))
}
dev.off()
Run Code Online (Sandbox Code Playgroud)
如何将注释放在中心而不是角落?
我试图使用grobs和gtable将4(ggplot2)图排列成2x2网格.我不知道如何设置宽度,也不知道非1xn或nx1排列.
使用此代码:
data(iris)
a <- ggplot(iris, aes(x=Species, y=Petal.Width)) + geom_boxplot(color="black") + ylab(expression(Foo~Bar~(g~cm^{-3})))
b <- ggplot(iris, aes(x=Species, y=Petal.Length*100)) + geom_boxplot(color="black") + ylab("foobar (mm)")
c <- ggplot(iris, aes(x=Species, y=Sepal.Width)) + geom_boxplot(color="black") + ylab("foobar (%)")
d <- ggplot(iris, aes(x=Species, y=log10(Sepal.Length))) + geom_boxplot(color="black") + ylab("foobar (cm)")
plots <- list(a,b,c,d)
grobs = lapply(plots, ggplotGrob)
g = do.call(rbind, c(grobs, size="first"))
g$widths = do.call(unit.pmax, lapply(grobs, "[[", "widths"))
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)
如果我将grid.arrange用于两列,则在4个图上,这些图的宽度不同.
如何将图表绑定到4 x 4排列的gtable中?
# I thought maybe I could cbind, then rbind, but this does not …Run Code Online (Sandbox Code Playgroud) 以下代码用于工作但不再适用.有人知道发生了什么事吗?必须对基础gtable代码进行一些更改.
require(cowplot) # for plot_grid()
require(grid) # for unit_max()
# make two plots
plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() + facet_grid(. ~ Species) + stat_smooth(method = "lm") +
background_grid(major = 'y', minor = "none") +
panel_border()
plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size=2.5)
g.iris <- ggplotGrob(plot.iris) # convert to gtable
g.mpg <- ggplotGrob(plot.mpg) # convert to gtable
iris.widths <- g.iris$widths[1:3] # extract the first three widths,
# corresponding to left margin, y …Run Code Online (Sandbox Code Playgroud) 这是一些生成具有两组方面的图形的最小代码.
library("ggplot2", quietly = TRUE, warn.conflicts = FALSE)
library("RColorBrewer", quietly = TRUE, warn.conflicts = FALSE)
val.a <- rnorm(10)
val.b <- rnorm(10)
val.c <- c("A","B","A","A","B","B","B","B","A","B")
val.d <- c("D","D","E","D","E","E","E","D","D","E")
val.e <- rnorm(10)
maya <- data.frame(val.a,val.b,val.c,val.d,val.e)
ggplot(maya, aes(x=val.a, y=val.b)) +
geom_point(shape=20,size=3, aes(colour=val.e)) +
facet_grid(val.c~val.d) +
xlab("Leonardo") + ylab("Michaelangelo") +
scale_colour_gradientn(colours=brewer.pal(9,"YlGnBu"), name="Splinter")
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何添加整个构面标签,以便名称Donatello和Raphael位于顶部和右侧轴上.
我在SO上看到了一些类似的解决方案,但我无法对代码进行反复无常的研究.请问你能否解答我的难题?
类似的问题在这里,但如果我有两个以上的方面,它就失败了.标签显示在图表内的某处.有没有办法让这个工作适用于一般情况?
所以我在上面的链接上尝试了rawr的解决方案,最终在多个列的同一个地方.这是更新为rawr解决方案的代码,但是它产生了意外的标签(对我来说因为我不理解解决方案).
library("ggplot2", quietly = TRUE, warn.conflicts = FALSE)
library("RColorBrewer", quietly = TRUE, warn.conflicts = FALSE)
val.a <- rnorm(20)
val.b <- rnorm(20)
val.c <- c("A","B","C","D","E","F","G","H","I","J")
val.d <- c("A","B","C","D","E","F","G","H","I","J")
val.e …Run Code Online (Sandbox Code Playgroud) 为什么下图不显示6 个面板中的数字(g; 指定通过textGrob(label=g)))?如果我只使用 text grob,这也有效,但是 text grob 和矩形 grob 似乎并不那么容易。不幸的是,帮助页面gtable_add_grob没有提供很多帮助...
require(gtable)
base <- gtable(widths=unit(rep(1, 2), "null"),
heights=unit(rep(1, 3), "null"))
g <- 1
for(i in 1:3) {
for(j in 1:2) {
base <- gtable_add_grob(base, list(rectGrob(gp=gpar(fill="#FF000088")), textGrob(label=g)), i, j)
g <- g+1
}
}
grid.draw(base)
Run Code Online (Sandbox Code Playgroud) 我正在学习操纵ggplot物体gtable.[这是我提出的一个相关问题:用网格和gtable拆解ggplot ]
目前的问题是:
下面是一些代码和数字.
# Data
df <- structure(list(Year = c(1950, 2013, 1950, 2013), Country = structure(c(1L,
1L, 2L, 2L), .Label = c("France", "United States"), class = "factor"),
Category = c("Hourly minimum wage", "Hourly minimum wage",
"Hourly minimum wage", "Hourly minimum wage"), value = c(2.14,
9.43, 3.84, 7.25), variable = c("France (2013 euros)",
"France (2013 euros)", "United States (2013 dollars)", "United States (2013 dollars)"
), Unit = c("2013 euros", "2013 euros", "2013 dollars", …Run Code Online (Sandbox Code Playgroud) 对于出版物,我需要在现有图中添加第二个y轴.我遇到过如何做到这一点的方法(https://rpubs.com/kohske/dual_axis_in_ggplot2).但是,我并不太了解编码.我找不到一种方法来使它成为正确的y轴也显示,而不是顶部边框.我的编码中缺少什么?这是我的虚拟数据:
df1 <- structure(list(month = structure(1:12, .Label = c("Apr", "Aug",
"Dec", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Nov", "Oct",
"Sep"), class = "factor"), RI = c(0.52, 0.115, 0.636666666666667,
0.807, 0.66625, 0.34, 0.143333333333333, 0.58375, 0.173333333333333,
0.5, 0.13, 0), sd = c(0.327566787083184, 0.162634559672906, 0.299555225848813,
0.172887246493199, 0.293010848165827, 0.480832611206852, 0.222785397486759,
0.381610777775321, 0.219393102292058, 0.3, 0.183847763108502,
0)), .Names = c("month", "RI", "sd"), class = "data.frame", row.names = c(NA,
-12L))
df2<-structure(list(month = structure(c(5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L), .Label …Run Code Online (Sandbox Code Playgroud) 为什么linesGrob未在下图中绘制线(通孔)?
require(gtable)
base <- gtable(widths=unit(rep(1, 2), "null"),
heights=unit(rep(1, 3), "null"))
grid.newpage()
g <- 1
for(i in 1:3) {
for(j in 1:2) {
base <- gtable_add_grob(base,
grobs=list(linesGrob(x=1:4, y=4:1),
rectGrob(gp=gpar(fill="#FF0000")),
textGrob(label=g)), i, j, name=1:3)
g <- g+1
}
}
grid.draw(base)
Run Code Online (Sandbox Code Playgroud) 我有两个ggplot geom_tile图,我使用来自gridExtra库的grid.arrange()放在一起.这是结果
library(ggplot2)
library(plyr)
p3 <- ggplot2(...)
p4 <- ggplot2(...)
grid.arrange(p3, p4, ncol=2)
Run Code Online (Sandbox Code Playgroud)

我想得到它,以便两个地块具有相同的高度并对齐.我用gtable来摆弄一下直接改变尺寸,但这似乎没有成功.
gA <- ggplot_gtable(ggplot_build(p3))
gB <- ggplot_gtable(ggplot_build(p4))
maxHeight <- unit.pmax(gA$heights[2:3], gB$heights[2:3])
gA$heights[2:3] <- maxHeight
gB$heights[2:3] <- maxHeight
grid.arrange(gA, gB, ncol=2)
Run Code Online (Sandbox Code Playgroud)
错误信息:
Error in grid.Call.graphics(L_setviewport, pvp, TRUE) :
non-finite location and/or size for viewport
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
---可重复的例子---
library(ggplot2)
library(plyr)
temp_plot <- structure(list(Sample = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, …Run Code Online (Sandbox Code Playgroud) 我正在努力建立一个基于ggplot物体的双轴绘图.在巴蒂斯特的建议下,我将问题分解为更小的部分.目前的问题是:
grobs在保持轴,轴标签,轴刻度线和网格线的同时从中删除所有数据?"数据"是指与geom_line()和相关的数据geom_points().想要这样做的原因是,在构建双轴绘图的过程中,我遇到了以下问题:来自一个grob的网格线将覆盖来自另一个grob的数据.通过删除数据线和点,将不会覆盖.
让我说清楚:我确实有一些解决方法,包括向aes()添加线型并设置scale_linetype_manual(values = c("solid","blank"),或者,将数据"从网格中发送",但是我想对一个未经过"修饰"的绘图对象进行后期处理,以达到目的.
下面是一些代码和数字.
# Data
df <- structure(list(Year = c(1950, 2013, 1950, 2013), Country = structure(c(1L,
1L, 2L, 2L), .Label = c("France", "United States"), class = "factor"),
Category = c("Hourly minimum wage", "Hourly minimum wage",
"Hourly minimum wage", "Hourly minimum wage"), value = c(2.14,
9.43, 3.84, 7.25), variable = c("France (2013 euros)",
"France (2013 euros)", "United States (2013 dollars)", "United States (2013 dollars)"
), Unit = c("2013 …Run Code Online (Sandbox Code Playgroud) 我一直在尝试在 gtable 单元格中指定 rasterGrob 的绝对位置,但没有成功。我希望能够使图像的范围与 y 轴上的值对齐。该脚本将钻芯图像与 ggplot2 方面绘制的多传感器数据对齐。例如,特定的放射线核心图像需要其顶部为 192 毫米,底部为 1482 毫米,但我希望比例从 0 到 1523 毫米。请参阅包含的链接以获取我正在做的事情的示例,但为了简单起见,我仅在此处发布了 MWE。是否可以在 gtable 单元格内指定 rasterGrob 的绝对位置?
就下面的 MWE 而言,迄今为止我唯一的解决方案是Rlogo.png使用使用时设置的相对位置来移动rasterGrob()。使用"native"坐标似乎也不是我所需要的。同样,我无法理解 中调用的位置参数gtable_add_grob()。
library(png)
library(ggplot2)
library(gtable)
# read Image
img <- readPNG(system.file("img", "Rlogo.png", package = "png"))
# convert to rastergrob
g <- rasterGrob(img, y = unit(0.5, "npc"), x = unit(0.5, "npc"))
# create plot
tp <- qplot(1:5, 1:5, geom="blank") + scale_y_reverse()
# convert plot to gtable
tt <- …Run Code Online (Sandbox Code Playgroud) 这篇文章描述了一种在时间序列图上创建两线 x 轴(年份低于月份)的方法。不幸的是,我在这篇文章中使用的方法(选项 2)与ggsave().
library(tidyverse)
library(lubridate)
df <- tibble(
date = as.Date(41000:42000, origin = "1899-12-30"),
value = c(rnorm(500, 5), rnorm(501, 10))
)
p <- ggplot(df, aes(date, value)) +
geom_line() +
geom_vline(
xintercept = as.numeric(df$date[yday(df$date) == 1]), color = "grey60"
) +
scale_x_date(date_labels = "%b", date_breaks = "month", expand = c(0, 0)) +
theme_bw() +
theme(panel.grid.minor.x = element_blank()) +
labs(x = "")
# Get the grob
g <- ggplotGrob(p)
# Get the y axis
index …Run Code Online (Sandbox Code Playgroud) 我正在使用facet_wrap并且还能够绘制次要y轴.然而,标签没有在轴附近绘制,而是绘制得非常远.我意识到如果我理解如何操纵凹槽的gtable(t,b,l,r)的坐标系,它将全部解决.有人可以解释他们实际描绘的方式和内容 - t:r = c(4,8,4,4)意味着什么.
辅助y轴与ggplot有许多链接,但是当nrow/ncol大于1时,它们会失败.那么请教我网格几何和grobs位置管理的基础知识.
编辑:代码
this is the final code written by me :
library(ggplot2)
library(gtable)
library(grid)
library(data.table)
library(scales)
# Data
diamonds$cut <- sample(letters[1:13], nrow(diamonds), replace = TRUE)
dt.diamonds <- as.data.table(diamonds)
d1 <- dt.diamonds[,list(revenue = sum(price),
stones = length(price)),
by=c("clarity", "cut")]
setkey(d1, clarity, cut)
# The facet_wrap plots
p1 <- ggplot(d1, aes(x = clarity, y = revenue, fill = cut)) +
geom_bar(stat = "identity") +
labs(x = "clarity", y = "revenue") +
facet_wrap( ~ cut) +
scale_y_continuous(labels = …Run Code Online (Sandbox Code Playgroud) gtable ×13
r ×13
ggplot2 ×11
r-grid ×5
plot ×2
axis-labels ×1
cowplot ×1
facet-grid ×1
gridextra ×1
grob ×1