作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找一种使用 ggplot2 绘制轴的方法,如下图所示。 (也许在 theme
或 scale_x_continuous
中有一些提示?)
我的意思是一个对称的标签序列,在这个例子中从 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))
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)
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/
我正在寻找绘制极坐标数据的替代方法。我需要实现像 this 这样的图表具有动态选项,例如 this . 非常感谢您的帮助! 最佳答案 我个人需要这些: Highcharts JS canvasXpre
我是一名优秀的程序员,十分优秀!