我使用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
等。来自动移动这些部件
**更新:**如问题中所述,hjust
和vjust
可以完成这项工作。使用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
的值?
1条答案
按热度按时间ezykj2lf1#
设置my_scale的值。