R语言 中心轴.标题和图例相对于ggside主图的位置

dkqlctbz  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(74)

我使用ggside在一个图中绘制数据的等高线图和线图。这和预期的一样工作,但对我来说,x轴和y轴标题的位置以及图例都有问题。

rm(list = ls(all = TRUE))

if (!require("ggplot2")) install.packages("ggplot2")
if (!require("ggside")) install.packages("ggside")

# get data

# contour

set.seed(230529)

z <- runif(n = 100, min = -5, max = 20)
x <- y <- seq(1, length(z)/10, 1)
tmp <- expand.grid(x, y)
x <- tmp[,1]
y <- tmp[,2]

df.1 <- data.frame(x, y)
df.1$z <- z
df.1$level <- as.numeric(df.1$z)

# line plot

y <- runif(n = length(z)/10, min = -5, max = 20)
x <- seq(1, length(y), 1)

df.2 <- data.frame(x = x, y = y)

# plot data

ggplot(df.1, aes(x = x, y = y)) +
  geom_contour(aes(z = z, color = after_stat(level))) + 
  geom_hline(yintercept = sum(range(x))/2) +
  geom_vline(xintercept = sum(range(x))/2) +
  theme(aspect.ratio = 1) +
  geom_xsideline(data = df.2, aes(x = x, y = y)) +
  geom_ysideline(data = df.2, aes(x = y, y = x), orientation = "y")

如图中的黑线所示,图的这些部分相对于整个图(主图+侧图)居中。

**问题:**ggside是否有选项将这些部分与主情节对齐?如果没有,是否有方法,例如使用hjust等。来自动移动这些部件
**更新:**如问题中所述,hjustvjust可以完成这项工作。使用ggside.panel.scale设置侧图的大小时。

rm(list = ls(all = TRUE))

if (!require("ggplot2")) install.packages("ggplot2")
if (!require("ggside")) install.packages("ggside")

# get data

# contour

set.seed(230529)

z <- runif(n = 100, min = -5, max = 20)
x <- y <- seq(1, length(z)/10, 1)
tmp <- expand.grid(x, y)
x <- tmp[,1]
y <- tmp[,2]

df.1 <- data.frame(x, y)
df.1$z <- z
df.1$level <- as.numeric(df.1$z)

# line plot

y <- runif(n = length(z)/10, min = -5, max = 20)
x <- seq(1, length(y), 1)

df.2 <- data.frame(x = x, y = y)

# plot data

ggplot(df.1, aes(x = x, y = y)) +
  geom_contour(aes(z = z, color = after_stat(level))) + 
  geom_hline(yintercept = sum(range(x))/2) +
  geom_vline(xintercept = sum(range(x))/2) +
  theme(axis.title.x = element_text(hjust = 0.4375),
        axis.title.y = element_text(vjust = 0.4375),
        legend.position = "right",
        legend.justification = c(1,0.4375),
        aspect.ratio = 1) +
  geom_xsideline(data = df.2, aes(x = x, y = y)) +
  geom_ysideline(data = df.2, aes(x = y, y = x), orientation = "y") +
  theme(ggside.panel.scale = 0.125,
        ggside.axis.text = element_blank(),
        ggside.axis.ticks = element_blank())

**新问题:**是否有办法自动获取ggside.panel.scale的值?

ezykj2lf

ezykj2lf1#

设置my_scale的值。

my_scale <- 0.125
adjust <- ((1 / my_scale) / ((1 / my_scale) +1)) / 2

ggplot(df.1, aes(x = x, y = y)) +
  geom_contour(aes(z = z, color = after_stat(level))) + 
  geom_hline(yintercept = sum(range(x))/2) +
  geom_vline(xintercept = sum(range(x))/2) +
  theme(axis.title.x = element_text(hjust = adjust),
        axis.title.y = element_text(hjust = adjust),
        legend.position = "right",
        legend.justification = c(1,adjust), 
        aspect.ratio = 1) +
  geom_xsideline(data = df.2, aes(x = x, y = y)) +
  geom_ysideline(data = df.2, aes(x = y, y = x), orientation = "y") +
  theme(ggside.panel.scale = my_scale,
        ggside.axis.text = element_blank(),
        ggside.axis.ticks = element_blank())

相关问题