我一直在寻找一种在 ggplot2 中使用 png 图像作为轴刻度的方法,更准确地说是在 我阅读过的ggridges 中,并试图复制这些帖子的答案,但是从那时起,软件包在语法上发生了很大变化发布日期:
使用额外的刻度和标签注释 ggplot 如何在 ggplot2 中使用通过 grImport 导入的图形作为轴刻度标签(使用网格函数)? 图标作为 R 中的 x 轴标签 - ggplot2
我想将 png(或其他类型的)图像添加到刻度标签,而不是标签 virginica、setosa 和 versicolor;
library(ggridges)
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges()
Run Code Online (Sandbox Code Playgroud) 我试图在 ggplot2 中生成一个轴线中断(轴线上有一个白色段),但遇到了一些问题。
使用内容丰富的帖子annotate-ggplot-with-an-extra-tick-and-label我能够在给定位置生成自定义 grobs,同时还关闭面板以在绘图区域外“绘制”。
我也熟悉其他包,例如plotrix并且能够在 base 中复制断轴,但最重要的是我有兴趣了解为什么我创建的轴不会覆盖线。下面是一些示例代码:
library(ggplot2) # devtools::install_github("hadley/ggplot2")
library(grid)
library(scales)
data("economics_long")
econ <- economics_long
econ$value01 <- (econ$value01/2)
x <- ggplot(econ, aes(date, value01,group=1)) + scale_y_continuous(labels=c(0.0,0.1,0.2,0.3,0.4,0.5,1.0), breaks=c(0.0,0.1,0.2,0.3,0.4,0.5,0.6),limits = c(0,.6),expand = c(0, 0)) +
geom_smooth(colour="deepskyblue", show.legend = TRUE ) + theme_bw()
theme_white <- theme(panel.background=element_blank(),
panel.border=element_rect(color="white"),
plot.margin = unit(c(.2, 0, .2, .2), "cm"),
panel.grid.major.y=element_blank(),
panel.grid.major.x=element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.minor.y=element_blank(),
axis.title.y = element_blank(),
axis.line.x=element_line(color="gray", size=1),
axis.line.y=element_line(color="gray", size=1),
axis.text.x=element_text(size=12),
axis.text.y=element_text(size=12),
axis.ticks=element_line(color="gray", size=1),
legend.position="none"
)
x <- x + theme_white
gline = …Run Code Online (Sandbox Code Playgroud) 我想在每个面板的底部画一个框,每个面板可能有 不同的y 范围。该框应始终位于 x 轴上的 3 到 7 之间,并且从面板底部的边框到 y 轴高度的 5% 左右。所以我想指定 egannotation_custom或geom_rect单位的坐标,例如:
xmin=unit(3, "native"), xmax=unit(7,"native"), ymin=unit(0,"npc"), ymax=unit(0.05,"npc")
Run Code Online (Sandbox Code Playgroud)
我可以做到,但这些单位似乎被忽略了,它总是原生的。
library("grid")
library("ggplot2")
df <- data.frame(x=c(1:10,1:10),y=c(1:10,1001:1010),condition=rep(c("A","B"),each=10))
g <- rectGrob(gp=gpar(fill="black"))
p <- ggplot(df, aes(x=x,y=y)) + geom_line() + facet_wrap(~ condition, scales="free_y")
p + geom_rect(xmin=3,xmax=7,ymin=0.56,ymax=1,colour="black", fill="black")
g <- rectGrob(gp=gpar(fill="black"))
p + annotation_custom(g, xmin=3, xmax=7, ymin=0, ymax=1 )
p + annotation_custom(g, xmin=unit(3, "native"), xmax=unit(7,"native"),ymin=unit(0,"npc"),ymax=unit(1,"npc") )
Run Code Online (Sandbox Code Playgroud)