gpt4 book ai didi

r - ggplot2 将 x 轴移动到顶部(在 0 处与反向 y 轴相交)

转载 作者:行者123 更新时间:2023-12-04 01:12:49 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Plot with reversed y-axis and x-axis on top in ggplot2

(3 个回答)


1年前关闭。




我想制作一个在 y=0 处反转 y 轴和 x 轴的图形。
y 轴与 scale_y_reverse 反转,但 x 轴保持在底部。

p <- ggplot(df, aes(x= conc, y=depth, group=factor(stn), color=factor(stn)))+
geom_point(shape=1)+
geom_path(alpha=0.5)+
scale_y_reverse(limits=(c(20,0)), expand=c(0,0))+
scale_x_continuous(expand=c(0,0))

我尝试了来自 this post 的代码像下面一样,但没有用。
p + 
scale_x_continuous(guide = guide_axis(position = "top")) +
scale_y_continuous(guide = guide_axis(position = "right"))

我不需要有两个 x 轴,只需从底部移动到顶部即可。

最佳答案

这在 ggplot2 中仍然是不可能的,但在 ggvis 中是可能的,它结合了 ggplot2语法与 dplyr管道。只需使用 add_axis函数将轴放在顶部。

# sample data
N <- 20
df <- data.frame(conc = seq(0, N),
depth = runif(N+1, 0, 20),
stn = rep(1:4, length=N+1))
# ggplot version
require(ggplot2)
p <- ggplot(df, aes(x= conc, y=depth, group=factor(stn), color=factor(stn)))+
geom_point(shape=1)+
geom_path(alpha=0.5)+
scale_y_reverse(limits=(c(20,0)), expand=c(0,0))+
scale_x_continuous(expand=c(0,0))
p
# ggvis version
require(ggvis)
df %>% transform(stn = factor(stn)) %>%
ggvis(x = ~conc, y = ~depth, stroke = ~stn) %>%
layer_points(shape := "circle", fill := "white") %>%
layer_lines(opacity := 0.5) %>%
scale_numeric("y", reverse=TRUE, domain=c(0,20), expand=c(0,0)) %>%
scale_numeric("x", expand=c(0,0)) %>%
add_axis("x", orient = "top")

关于r - ggplot2 将 x 轴移动到顶部(在 0 处与反向 y 轴相交),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21035101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com