2014-11-25 1620 views
2

我想要使用颠倒的y轴和顶部的x轴绘制ggplot2。我使用scale_y_reverse()来获得颠倒的y轴,但不知道如何将x轴放在顶部而不是底部。任何帮助将不胜感激。由于ggplot2:使用颠倒的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) 

enter image description here

+0

不幸的是,这个功能不太可能实现。见[相关问题](http://stackoverflow.com/questions/21035101/ggplot2-move-x-axis-to-top-intersect-with-reversed-y-axis-at-0)。 – tonytonov 2014-11-25 10:35:33

+1

虽然AFAIK这是在ggvis支持。 – tonytonov 2014-11-25 10:37:37

+0

感谢@tonytonov对我的问题感兴趣。除了使用“Inkscape”之外,是否有任何解决方法?谢谢 – MYaseen208 2014-11-25 10:38:16

回答

1

你需要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') 

enter image description here

2

如果你不想切换到ggvis只是还没有,的的ggdraw(switch_axis_position(p1 , axis = 'x'))功能cowplot包装工作得很好。

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

+0

尽管此链接可能回答此问题,但最好在此处包含答案的基本部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/11167218) – 2016-02-06 06:31:35

+1

该链接仅供参考,并非绝对必要的答案。所提供的代码是答案的重要部分。 – Clay 2016-02-07 11:23:51