首页 > 解决方案 > ggplot 精确轴范围加反向

问题描述

与ggplot 精确轴范围几乎完全相同的问题,除了我还需要从上到下反转 y 轴。直接从上面表示

myData = data.frame(x = c(0, 1, 2, 3, 4, 5),
                  y = c(0.05,0.06, 0.07, 0.08, 0.09, 0.09))

ggplot() +
  geom_step(data=myData, aes(x=x, y=y), color='blue', size=1) +
  xlab('') +
  ylab('') +
  scale_x_continuous(expand = expand_scale(), limits = c(0,5))+
  scale_y_continuous(labels = scales::percent, expand = expand_scale(), limits = c(0,0.12))

#adding reverse goes wrong
ggplot() +
  geom_step(data=myData, aes(x=x, y=y), color='blue', size=1) +
  xlab('') +
  ylab('') +
  scale_x_continuous(expand = expand_scale(), limits = c(0,5))+
  scale_y_continuous(labels = scales::percent, expand = expand_scale(), limits = c(0,0.12), trans="reverse")

标签: rggplot2axis

解决方案


反转limits值。

library(ggplot2)

ggplot() +
  geom_step(data=myData, aes(x=x, y=y), color='blue', size=1) +
  xlab('') +
  ylab('') +
  scale_x_continuous(expand = expand_scale(), limits = c(0,5))+
  scale_y_continuous(labels = scales::percent, expand = expand_scale(),
                     limits = c(0.12,0),trans = "reverse")

在此处输入图像描述


推荐阅读