在单个ggplot构面中反转y轴 [英] Reversing y-axis in an individual ggplot facet

查看:167
本文介绍了在单个ggplot构面中反转y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制一系列不同的数据,这些数据是在相同的个体之间以小平面相邻的方式测量的。对于某些类型的数据,正值是好,而对于某些负值则是好。后者类型的变量通常用翻转的y轴绘制。是否可以使用ggplot修改单个构面中的轴方向?

  dat < -  data.frame(type = rep(c ('A','B'),每个= 10),x = 1:10,y = rnorm(20))

ggplot(dat,aes(x,y))+ geom_point )+ facet_wrap(〜type,scales ='free_y')



例如,我可以用B轴的y轴做上面的图吗?

解决方案

我不确定这是否可行,所以我会选择使用 <$>中的 grid.arrange 函数的解决方案c $ c> gridExtra 包。

  library(gridExtra)
库(ggplot2)

dat < - data.frame(type = rep(c('A','B'),each = 10),x = 1:10,y = rnorm 20),
stringsAsFactors = FALSE)

p_A < - ggplot(subset(dat,type =='A'),aes(x,y))+ geom_point()+ facet_wrap(〜type,scales ='free_y')+
scale_y_continuous(breaks = c(-1 ,0,1))
p_B < - ggplot(subset(dat,type =='B'),aes(x,y))+ geom_point()+ facet_wrap(〜type,scales ='free_y' )+
scale_y_reverse(breaks = c(-1,0,1))

grid.arrange(p_A,p_B,nrow = 1)


I am plotting a range of different data measured on the same individuals next to each other in facets. For some types of data a positive value is "good" and for some a negative value is "good". The latter types of variables are usually plotted with flipped y-axes. Is it possible to modify axis directions in individual facets with ggplot?

dat <- data.frame(type = rep(c('A', 'B'), each = 10), x = 1:10, y = rnorm(20))

ggplot(dat, aes(x, y)) + geom_point() + facet_wrap( ~ type, scales = 'free_y')

For example, could I do the above plot with the y-axis for B reversed?

解决方案

I'm not sure if that's possible, so I would opt for a solution using the grid.arrange function from the gridExtra package.

library(gridExtra)
library(ggplot2)

dat <- data.frame(type = rep(c('A', 'B'), each = 10), x = 1:10, y = rnorm(20),
                  stringsAsFactors = FALSE)

p_A <- ggplot(subset(dat, type == 'A'), aes(x, y)) + geom_point() + facet_wrap( ~ type, scales = 'free_y')+
    scale_y_continuous(breaks = c(-1,0,1))
p_B <- ggplot(subset(dat, type == 'B'), aes(x, y)) + geom_point() + facet_wrap( ~ type, scales = 'free_y')+
    scale_y_reverse(breaks = c(-1,0,1))

grid.arrange(p_A, p_B, nrow = 1)

这篇关于在单个ggplot构面中反转y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆