Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ggplot2中使用facet_grid()时如何定义公共y轴限制_R_Ggplot2 - Fatal编程技术网

在ggplot2中使用facet_grid()时如何定义公共y轴限制

在ggplot2中使用facet_grid()时如何定义公共y轴限制,r,ggplot2,R,Ggplot2,我的第三个帖子在这里。我正在使用ggplot2和facet\u grid()。代码运行良好。以下是我使用的数据: #My data df <- structure(list(Var = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), .Label = c("1", "2&quo

我的第三个帖子在这里。我正在使用
ggplot2
facet\u grid()。代码运行良好。以下是我使用的数据:

#My data
df <- structure(list(Var = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12"), class = c("ordered", "factor")), Val = c(46.0233614780009, 
85.6471698498353, 83.8100037071854, 98.6977939726785, 94.0682111307979, 
92.1834012959152, 79.1579962009564, 62.9422475816682, 2.36891501117498, 
25.3718703053892, 87.2779565863311, 32.0944497128949, 444.363995105959, 
337.84707041923, 93.2718054391444, 171.342949266545, 81.6757546272129, 
135.7353850035, 286.496924120001, 450.293861329556, 339.913251576945, 
80.7274857070297, 122.17661133036, 370.043645612895), Group = c("Up", 
"Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", 
"Down", "Down", "Down", "Down", "Down", "Down", "Down", "Down", 
"Down", "Down", "Down", "Down")), row.names = c(NA, -24L), class = "data.frame")
这个婴儿的出生地:

在这里之前一切都很好。我想知道是否有可能修改两个要素:

  • 如何减少两个面之间的垂直空间,以便视觉上只有一个x轴(在同一轴上对齐两个零并减少该空间)

  • 我想在两个y轴上都有相同的比例,所以代码被设计成具有镜像效果。我的问题是,当我像这样在y轴上设置比例时:

  • 一切都变了:

    如果可能的话,我希望两个y轴都有相同的刻度,从0到800,但要考虑大小。在这种情况下,鞋面将从0变为800,从0变为-800,但根据标签进行遮罩以产生镜像效果


    非常感谢您的帮助。

    对于旧的“看不见的点”技巧来说,这似乎是一个理想的工作,可以在需要的地方设置面限制:

    库(ggplot2)
    图书馆(dplyr)
    df%>%
    变异(Val=ifelse(组=='Down',-Val,Val),
    组=系数(组,级别=c('Up','Down'),顺序=TRUE))%>%
    ggplot(aes(Var、Val、颜色=组、填充=组、组=组))+
    几何尺寸线(尺寸=1)+
    几何面积(α=0.75)+
    几何点(数据=数据帧(组=系数(c(“向下”、“向上”)、c(“向上”、“向下”)),
    Var=c(1,1),
    Val=c(-800800)),alpha=0)+
    刻面网格(组~,刻度=‘自由’)+
    比例y连续(标签=功能(x)abs(x),
    扩展=c(0,0.1))+
    刻度\填充\手动(值=c('青色','番茄'))+
    比例\颜色\手册(值=c(‘青色’、‘西红柿’))+
    主题_bw()+
    主题(面板间距=单位(0,“点”))
    

    不过,公平地说,在这里你并不真正需要切面,你可以通过这样做得到非常相似的效果:

    df%>%
    变异(Val=ifelse(组=='Down',-Val,Val),
    组=系数(组,级别=c('Up','Down'),顺序=TRUE))%>%
    ggplot(aes(Var、Val、颜色=组、填充=组、组=组))+
    几何尺寸线(尺寸=1)+
    几何面积(α=0.75)+
    geom_hline(yintercept=0)+
    比例y连续(标签=功能(x)abs(x),
    扩展=c(0,0.1),
    限值=c(-800800))+
    刻度\填充\手动(值=c('青色','番茄'))+
    比例\颜色\手册(值=c(‘青色’、‘西红柿’))+
    主题_bw()+
    主题(面板间距=单位(0,“点”))
    

    library(ggplot2)
    library(dplyr)
    #Code for the plot
    df %>%
      mutate(Val=ifelse(Group=='Down',-Val,Val),
             Group=factor(Group,levels = c('Up','Down'),ordered = T)) %>%
      ggplot(aes(x=Var,y=Val,color=Group,fill=Group,group=Group))+
      geom_line(size=1)+geom_area(alpha=0.75)+
      facet_grid(Group~.,scales = 'free')+
      scale_y_continuous(labels = function(x) abs(x),
                         expand = c(0,0.1))+
      scale_fill_manual(values=c('cyan','tomato'))+
      scale_color_manual(values=c('cyan','tomato'))+
      theme_bw()
    
    scale_y_continuous(labels = function(x) abs(x),expand = c(0,0.1),limits = c(NA,800))