与多个顶部相等的间距 [英] Equal spacing with multiple atop
问题描述
我试图在一个 ggplot2
图表中创建一个图例,每行有多行和一个参数和值。由于我有符号作为变量,所以需要使用表达式
来完成。为了创建新的线条,我使用了多个 atop
命令,但这会导致最后一行中的间距不均匀。请看下面的例子:
library(ggplot2)
N = 25
a = -5
b = 2
sigma = 1
x = runif(N,0,10)
y = a + x * b + rnorm(N ,sd = sigma)
df = data.frame(x,y)
ggplot(df,aes(x,y))+
geom_point() +
geom_label(aes(x = 1,y = max(y) - 2),
label = paste0(atop(atop(,
textstyle(a ==, a,,
textstyle(b ==,b,)),,
textstyle(sigma ==,sigma,))
) ,parse = TRUE
)
ggsave(plotmath_atop.png,width = 6,height = 4,scale = 1)
这给出以下图表:
正如您所看到的,行之间的间距 b = 2
和 \sigma = 1
显着大于 a = -5
和 b = 2
。
有没有一种方法可以在使用多行换行的情况下使用表达式
,而每行之间仍然有间距?
您可以使用gridExtra :: tableGrob,
library(gridExtra)
库(网格)
table_label< - function(label,params = list()){
params< - modifyList(list(hjust = 0,x = 0),params)
mytheme -ttheme_minimal(padding = unit(c(1,1),mm),
core = list(fg_params = params),parse = TRUE)
disect < - strsplit(label,\\)[[1]]
m< - as.matrix(disect)
tg< - tableGrob(m,theme = mytheme)
BG < - roundrectGrob(width = sum(tg $ widths)+ unit(3,mm),height = sum(tg $ heights)+ unit(3,mm))
grobTree(bg,tg )
}
txt < - 'a == -5\
b == 2\ $
sigma == 1'
library(ggplot2)
qplot(1:10,1:10)+
annotation_custom(table_label(txt),xmin = 0, xmax = 5,ymin = 7.5)
I'm trying to create a legend in a ggplot2
graph with multiple lines and a parameter and value on each line. Since I have symbols as variables, this needs to be done with expression
. To create new lines, I have used multiple atop
commands, but this leads to uneven spacing in the final line. Please see my following example:
library(ggplot2)
N = 25
a = -5
b = 2
sigma = 1
x = runif(N, 0, 10)
y = a + x * b + rnorm(N, sd = sigma)
df = data.frame(x, y)
ggplot(df, aes(x, y)) +
geom_point() +
geom_label(aes(x = 1, y = max(y) - 2),
label = paste0("atop(atop(",
"textstyle(a == ", a, "),",
"textstyle(b == ", b, ")),",
"textstyle(sigma == ", sigma, "))"
), parse = TRUE
)
ggsave("plotmath_atop.png", width = 6, height = 4, scale = 1)
This gives the following plot:
As you can see, the spacing between the lines b=2
and \sigma=1
is noticeably larger than the spacing between the lines a=-5
and b=2
.
Is there a way of using expression
with multiple line breaks while still having even spacing between each line?
you could use gridExtra::tableGrob,
library(gridExtra)
library(grid)
table_label <- function(label, params=list()) {
params <- modifyList(list(hjust=0, x=0), params)
mytheme <- ttheme_minimal(padding=unit(c(1, 1), "mm"),
core = list(fg_params = params), parse=TRUE)
disect <- strsplit(label, "\\n")[[1]]
m <- as.matrix(disect)
tg <- tableGrob(m, theme=mytheme)
bg <- roundrectGrob(width = sum(tg$widths) + unit(3, "mm"), height = sum(tg$heights) + unit(3, "mm"))
grobTree(bg, tg)
}
txt <- 'a == -5\n
b == 2\n
sigma == 1'
library(ggplot2)
qplot(1:10,1:10) +
annotation_custom(table_label(txt), xmin=0, xmax=5, ymin=7.5)
这篇关于与多个顶部相等的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!