在ggplot2中将点与背景png对齐
Align points with background png in ggplot2
我正在尝试在 384 孔板实验中创建与发光值对应的点网格。我将盘子绘制为 .png 文件并覆盖网格,这样每个点都应该位于盘子的一个孔中。提供示例代码和数据。
是否可以使用 ggplot2 执行此操作?
我正在使用以下代码(提供的示例数据):
library(ggplot2)
library(png)
library(RCurl)
library(grid)
example.gg <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vRcX5aMZGCp9Bs3BRZSg8k4o-kbSjOO5z3LsRxgIv4qJHz1fG-Argruje32OuZ2Tt2qPaNGksGr4Jia/pub?output=csv",
row.names = 1)
example.gg$Row <- factor(example.gg$Row, levels = rev(sort(unique(example.gg$Row))))
png.img <- readPNG(getURLContent("https://i.imgur.com/QeSO7d3.png"))
img.rg <- rasterGrob(png.img, interpolate=TRUE)
gp <- ggplot(example.gg,
aes(x = Col, y = Row, col = Lum)) +
annotation_custom(img.rg, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
geom_point(shape = 15) +
theme_void()
gp
这是制作的图像:
回答
感谢 的原始回答。
gp <- ggplot(example.gg,
aes(x = Col, y = Row, col = Lum)) +
annotation_custom(img.rg,
xmin = -2,
xmax = 27,
ymin = -1,
ymax = 18) +
geom_point(shape = 15, size = 2.5) +
theme_void()
gp + coord_fixed(clip = "off") +
theme(plot.margin = unit(c(3, 6, 5, 2), "lines"),
legend.position = c(1.2, 0.5)) +
scale_colour_gradientn(colours = pals::ocean.haline(100))
通过xmin
/xmax
& ymin
/ymax
手动调整图片位置,固定行列间距coord_fixed(clip = "off)
并在 theme
中扩展 plot.margin
我能够得到一些看起来可行的东西。
library(ggplot2)
library(png)
library(RCurl)
library(grid)
example.gg <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vRcX5aMZGCp9Bs3BRZSg8k4o-kbSjOO5z3LsRxgIv4qJHz1fG-Argruje32OuZ2Tt2qPaNGksGr4Jia/pub?output=csv",
row.names = 1)
example.gg$Row <- factor(example.gg$Row, levels = sort(unique(example.gg$Row)))
png.img <- readPNG(getURLContent("https://i.imgur.com/QeSO7d3.png"))
img.rg <- rasterGrob(png.img, interpolate=TRUE)
gp <- ggplot(example.gg,
aes(x = Col, y = Row, col = Lum)) +
annotation_custom(
img.rg,
xmin = -2,
xmax = 27,
ymin = -1,
ymax = 18
) +
geom_point(shape = 15) +
coord_fixed(clip = "off") +
theme_void() +
theme(plot.margin = unit(c(3, 2, 5, 2), "lines"))
gp
由 reprex package (v2.0.1)
于 2022-02-14 创建
我喜欢挑战,所以这是一个单一的 ggplot 调用,不依赖任何 png 文件
ggplot(example.gg, aes(factor(Col, levels = 1:24), Row, fill = Lum)) +
geom_tile(size = 6, color = "white") +
geom_label(label = " ", fill = NA, label.padding = unit(0.3, "lines")) +
annotation_custom(roundrectGrob(gp = gpar(fill = NA)), xmin = -2,
xmax = 26, ymin = -1, ymax = 19) +
annotation_custom(roundrectGrob(gp = gpar(fill = NA), r = unit(0.01, "npc")),
xmin = 0.5, xmax = 24.5, ymin = 0.5, ymax = 16.5) +
annotation_custom(polygonGrob(
x = c(0.05, 0.99, 1, 1, 0.99, 0.05, 0, 0, 0.05),
y = c(0, 0, 0.01, 0.99, 1, 1, 0.9, 0.1, 0), gp = gpar(fill = NA)),
xmin = -1.5, xmax = 25, ymin = 0, ymax = 18) +
scale_fill_gradientn(colours = pals::ocean.haline(100)) +
theme_void() +
coord_equal(clip = "off") +
scale_x_discrete(expand = c(0.03, 0)) +
guides(x = guide_axis(position = "top")) +
theme(axis.text = element_text(size = 18),
axis.text.x.top = element_text(size = 12),
plot.margin = margin(20, 20, 20, 40),
legend.box.margin = margin(20, 20, 20, 40))
我正在尝试在 384 孔板实验中创建与发光值对应的点网格。我将盘子绘制为 .png 文件并覆盖网格,这样每个点都应该位于盘子的一个孔中。提供示例代码和数据。
是否可以使用 ggplot2 执行此操作?
我正在使用以下代码(提供的示例数据):
library(ggplot2)
library(png)
library(RCurl)
library(grid)
example.gg <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vRcX5aMZGCp9Bs3BRZSg8k4o-kbSjOO5z3LsRxgIv4qJHz1fG-Argruje32OuZ2Tt2qPaNGksGr4Jia/pub?output=csv",
row.names = 1)
example.gg$Row <- factor(example.gg$Row, levels = rev(sort(unique(example.gg$Row))))
png.img <- readPNG(getURLContent("https://i.imgur.com/QeSO7d3.png"))
img.rg <- rasterGrob(png.img, interpolate=TRUE)
gp <- ggplot(example.gg,
aes(x = Col, y = Row, col = Lum)) +
annotation_custom(img.rg, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
geom_point(shape = 15) +
theme_void()
gp
这是制作的图像:
回答
感谢
gp <- ggplot(example.gg,
aes(x = Col, y = Row, col = Lum)) +
annotation_custom(img.rg,
xmin = -2,
xmax = 27,
ymin = -1,
ymax = 18) +
geom_point(shape = 15, size = 2.5) +
theme_void()
gp + coord_fixed(clip = "off") +
theme(plot.margin = unit(c(3, 6, 5, 2), "lines"),
legend.position = c(1.2, 0.5)) +
scale_colour_gradientn(colours = pals::ocean.haline(100))
通过xmin
/xmax
& ymin
/ymax
手动调整图片位置,固定行列间距coord_fixed(clip = "off)
并在 theme
中扩展 plot.margin
我能够得到一些看起来可行的东西。
library(ggplot2)
library(png)
library(RCurl)
library(grid)
example.gg <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vRcX5aMZGCp9Bs3BRZSg8k4o-kbSjOO5z3LsRxgIv4qJHz1fG-Argruje32OuZ2Tt2qPaNGksGr4Jia/pub?output=csv",
row.names = 1)
example.gg$Row <- factor(example.gg$Row, levels = sort(unique(example.gg$Row)))
png.img <- readPNG(getURLContent("https://i.imgur.com/QeSO7d3.png"))
img.rg <- rasterGrob(png.img, interpolate=TRUE)
gp <- ggplot(example.gg,
aes(x = Col, y = Row, col = Lum)) +
annotation_custom(
img.rg,
xmin = -2,
xmax = 27,
ymin = -1,
ymax = 18
) +
geom_point(shape = 15) +
coord_fixed(clip = "off") +
theme_void() +
theme(plot.margin = unit(c(3, 2, 5, 2), "lines"))
gp
由 reprex package (v2.0.1)
于 2022-02-14 创建我喜欢挑战,所以这是一个单一的 ggplot 调用,不依赖任何 png 文件
ggplot(example.gg, aes(factor(Col, levels = 1:24), Row, fill = Lum)) +
geom_tile(size = 6, color = "white") +
geom_label(label = " ", fill = NA, label.padding = unit(0.3, "lines")) +
annotation_custom(roundrectGrob(gp = gpar(fill = NA)), xmin = -2,
xmax = 26, ymin = -1, ymax = 19) +
annotation_custom(roundrectGrob(gp = gpar(fill = NA), r = unit(0.01, "npc")),
xmin = 0.5, xmax = 24.5, ymin = 0.5, ymax = 16.5) +
annotation_custom(polygonGrob(
x = c(0.05, 0.99, 1, 1, 0.99, 0.05, 0, 0, 0.05),
y = c(0, 0, 0.01, 0.99, 1, 1, 0.9, 0.1, 0), gp = gpar(fill = NA)),
xmin = -1.5, xmax = 25, ymin = 0, ymax = 18) +
scale_fill_gradientn(colours = pals::ocean.haline(100)) +
theme_void() +
coord_equal(clip = "off") +
scale_x_discrete(expand = c(0.03, 0)) +
guides(x = guide_axis(position = "top")) +
theme(axis.text = element_text(size = 18),
axis.text.x.top = element_text(size = 12),
plot.margin = margin(20, 20, 20, 40),
legend.box.margin = margin(20, 20, 20, 40))