0

我正在尝试创建 x 轴 ALSO 出现在图上方以及 y 轴出现在图右侧的图。两者都应该包含与正常轴相同的刻度,但不是轴文本。这应该会产生一个“框”,在绘图周围带有有用的刻度线。我还希望在没有标签的主要刻度(已标记)之间有较小的刻度。这是我做的一个图:

像现在一样绘制

我还用粉红色绘制了我想在 R 中实现的效果:

随心所欲地绘制] 3

我的这个情节的代码:

p <- p + xlab("") + ylab("") + theme(legend.position = "none") + theme(axis.ticks.length = unit(-0.25, "cm"), axis.text.x = element_text(size = 30, hjust=1)) + theme(axis.text.y = element_text(size=35, hjust = 1), strip.text = element_text(size=35), axis.title.y = element_text(size = 40), legend.text = element_text(size=30), axis.title.x = element_text(size=40), legend.title = element_text(size=45)) 
p <- p + theme(text = element_text(family = "Helvetica")) + scale_x_continuous(limits=c(-0.5, 25), breaks = c(0, 2, 4, 6, 8, 24))  + theme(legend.background = element_rect(color = "black", linetype = "solid")) + scale_colour_manual(values = cbpallette)
p <- p + theme(legend.key.size = unit(2.5, "cm")) + theme(axis.text.x = element_text(margin = margin(t = .5, unit = "cm")), axis.text.y = element_text(margin = margin(r = .5, unit = "cm")))
p

*** 编辑 *** 这里是更新的代码,使用 Stefans 的建议。该图看起来像这样,刻度线在那里,轴不见了:

更新的数字

p <- p + xlab("") + ylab("") + theme(legend.position = "none") + theme(axis.ticks.length = unit(-0.25, "cm"), axis.text.x = element_text(size = 30, hjust=1)) + theme(axis.text.y = element_text(size=35, hjust = 1, angle=45), strip.text = element_text(size=35), axis.title.y = element_text(size = 40), legend.text = element_text(size=30), axis.title.x = element_text(size=40)) 
p <- p + theme(text = element_text(family = "Helvetica"))  + scale_colour_manual(values = cbpallette)
p <- p + theme(axis.text.x = element_text(margin = margin(t = .5, unit = "cm")), axis.text.y = element_text(margin = margin(r = .5, unit = "cm"))) 
p
4

1 回答 1

0

sec.axis = dup_axis()您可以使用两个比例的参数复制轴,如下所示:

library(ggplot2)

ggplot(mtcars, aes(hp, mpg)) +
  geom_point() +
  scale_x_continuous(sec.axis = dup_axis(name = NULL, labels = NULL)) +
  scale_y_continuous(sec.axis = dup_axis(name = NULL, labels = NULL))

于 2020-12-30T18:47:14.523 回答