gpt4 book ai didi

r - 极坐标图的对称轴标签

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

我正在寻找一种使用 ggplot2 绘制轴的方法,如下图所示。 (也许在 themescale_x_continuous 中有一些提示?)
enter image description here
我的意思是一个对称的标签序列,在这个例子中从 100 到 100,中心在 20(不是轴的水平方向)。
可重现的例子:

library(ggplot2)
required(grid)
ggplot(data = i, aes(x = wd, y = co2)) +
ggtitle("CO2")+
geom_point(size=4, colour = "red")+
geom_linerange(aes(ymax =ci2, ymin=ci1), colour = "red", size = 2)+
coord_polar()+

geom_hline(yintercept = seq(365, 405, by = 5), colour = "grey", size = 0.2) +
geom_vline(xintercept = seq(0, 360, by = 22.5), colour = "grey", size = 0.2) +
scale_x_continuous(limits = c(0, 360), expand = c(0, 0),
breaks = seq(0, 359.99, by = 22.5),
labels=c("N","NNE","NE","ENE","E","ESE","SE","SSE",
"S","SSW","SW","WSW","W","WNW","NW","NNW")) +
scale_y_continuous(limits = c(365, 405), breaks = seq(365, 405, by = 10)) +

theme_bw() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
legend.key = element_blank(),
axis.ticks = element_line(colour = "grey"),
axis.ticks.length = unit(-1, "lines"),
axis.ticks.margin = unit(1.3,"lines"),
axis.text = element_text(size=24),
axis.text.y = element_text(size=24),
axis.title = element_blank(),
axis.line=element_line(),
axis.line.x=element_blank(),
axis.line.y = element_line(colour = "grey"),
plot.title = element_text(hjust = 0, size = 20))
enter image description here
数据:
i <- structure(list(wd = c(0, 112.5, 135, 180, 202.5, 22.5, 225, 247.5, 
270, 292.5, 337.5, 45, 67.5, 90), co2 = c(389.82, 376.82, 386.06,
392.04, 392.55, 387.97, 391.45, 389.87, 390.12, 389.68, 391.39,
390.1, 386.89, 383.05), ci1 = c(388.37, 367.67, 378.98, 381.76,
388.63, 386.65, 388.32, 388.5, 389.03, 387.25, 389.05, 388.65,
385.64, 381.1), ci2 = c(391.26, 385.98, 393.15, 402.31, 396.46,
389.28, 394.58, 391.23, 391.21, 392.12, 393.73, 391.55, 388.15,
385.01)), row.names = c(NA, -14L), class = "data.frame")

最佳答案

假设原始情节被命名为 p ,并且在对称中心已经有一个轴标签/刻度(否则这个用例将没有意义),下面的 grob hack 应该可以工作:

gp <- ggplotGrob(p)

# flip labels
new.labels <- gp$grobs[[which(gp$layout$name == "axis-l")]]$children[[2]]$grobs[[1]]$children[[1]]
new.labels$label <- c(rev(new.labels$label[-1]), new.labels$label)
new.labels$x <- unit.c(rep(new.labels$x[1], length(new.labels$x) - 1),
new.labels$x)
new.labels$y <- unit.c(rev(2*new.labels$y[1] - new.labels$y[-1]),
new.labels$y)

# flip ticks
new.ticks <- gp$grobs[[which(gp$layout$name == "axis-l")]]$children[[2]]$grobs[[2]]
new.ticks$x <- unit.c(rep(new.ticks$x[1:2], length(new.ticks$x)/2 - 1),
new.ticks$x)
new.ticks$y <- new.labels$y[rep(seq_along(new.labels$y), each = 2)]
new.ticks$id.lengths <- rep(new.ticks$id.lengths[1], length(new.ticks$y))

# assign flipped labels / ticks back to plot
new.labels -> gp$grobs[[which(gp$layout$name == "axis-l")]]$children[[2]]$grobs[[1]]$children[[1]]
new.ticks -> gp$grobs[[which(gp$layout$name == "axis-l")]]$children[[2]]$grobs[[2]]

# show plot
grid::grid.draw(gp)
plot
(注:相较于问题,这里定义了 p,主题规范较少,为简单起见)
p <- ggplot(data = i, aes(x = wd, y = co2)) +
ggtitle("CO2")+
geom_point(size=4, colour = "red")+
geom_linerange(aes(ymax =ci2, ymin=ci1), colour = "red", size = 2)+
coord_polar()+

geom_hline(yintercept = seq(365, 405, by = 5),
colour = "grey", size = 0.2) +
geom_vline(xintercept = seq(0, 360, by = 22.5),
colour = "grey", size = 0.2) +
scale_x_continuous(limits = c(0, 360), expand = c(0, 0),
breaks = seq(0, 359.99, by = 22.5),
labels=c("N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW")) +
scale_y_continuous(limits = c(365, 405),
breaks = seq(365, 405, by = 10)) +

theme_void() +
theme(axis.ticks = element_line(colour = "grey"),
axis.ticks.length = unit(-0.5, "lines"),
axis.text = element_text(size=12,
margin = unit(rep(1, 4), "lines")),
axis.line.y = element_line(colour = "grey"))

关于r - 极坐标图的对称轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807846/

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