简体   繁体   English

在ggplot2中绘制顶部反转的y轴和x轴

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

I'm trying to get ggplot2 plot with reversed y-axis and x-axis on top. 我试图在顶部反转y轴和x轴获得ggplot2图。 I used scale_y_reverse() to get reversed y-axis but could not figured out how to put x-axis on the top rather than at bottom. 我使用scale_y_reverse()来获得反转的y轴,但无法弄清楚如何将x轴放在顶部而不是底部。

dfn <- read.table(header=T, text='
supp dose length
  OJ  0.5  13.23
  OJ  1.0  22.70
  OJ  2.0  26.06
  VC  0.5   7.98
  VC  1.0  16.77
  VC  2.0  26.14
')

library(ggplot2)
p1 <- ggplot(data=dfn, aes(x=dose, y=length, group=supp, colour=supp)) + geom_line() + geom_point()
p1 <- p1 + scale_y_reverse()
print(p1)

在此输入图像描述

If you don't want to switch to ggvis just yet, the ggdraw(switch_axis_position(p1 , axis = 'x')) function of the cowplot package works very well. 如果你不想切换到ggvis只是还没有,在ggdraw(switch_axis_position(p1 , axis = 'x'))的功能cowplot包工作得很好。

https://cran.r-project.org/web/packages/cowplot/vignettes/axis_position.html https://cran.r-project.org/web/packages/cowplot/vignettes/axis_position.html

Now even easier with ggplot v2.2.0: 现在使用ggplot v2.2.0更容易:

p1 <- ggplot(data=dfn, aes(x=dose, y=length, group=supp, colour=supp)) + geom_line() + geom_point()
p1 <- p1 + scale_y_reverse() + scale_x_continuous(position = 'top')
print(p1)

You need ggvis to do that: 你需要ggvis来做到这一点:

library(ggvis)
dfn %>% ggvis(~dose, ~length, fill= ~supp, stroke=~supp) %>% layer_lines(fillOpacity=0) %>%
  scale_numeric('y', reverse=T) %>% add_axis('x',orient='top')

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM