用facet换行设置个别的y轴限制,不用缩放free_y [英] Setting individual y axis limits with facet wrap NOT with scales free_y

查看:251
本文介绍了用facet换行设置个别的y轴限制,不用缩放free_y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据

$ $ $ $ $ $ $ $ $ $ $ $ dat $ data $ frame $ = c(1,2,1,2),
group = c(a,a,b,b),
y = c(10,20,1000, 2000))

ggplot(dat,aes(x = x,y = y))+
geom_point()+
geom_line()+
facet_wrap(〜 group,ncol = 1)+
coord_cartesian(ylim = c(0,30))

您可以看到B组没有出现,因为我将y限制设置为 0,30 。我想手动设置每个图表的个人y限制。我不想使用 scales =free_y,因为我需要控制每张图表的限制。



有没有办法做到这一点?你可以以某种方式为每个图表提供y方面的限制吗?

解决方案

除非你想减少你的绘图区域绘制点数),您仍然可以在使用 scales =free_y时对您的y限制进行全面控制。

您可以使用我提供的相同技巧来回答您的其他问题:


I have this data

library(ggplot2)

dat = data.frame(x = c(1,2,1,2), 
                 group = c("a","a","b","b"), 
                 y = c(10,20,1000,2000))

ggplot(dat, aes(x = x, y = y)) + 
    geom_point() + 
    geom_line() + 
    facet_wrap(~group, ncol = 1) +
    coord_cartesian(ylim = c(0, 30))

You can see the B group does not show up because I set the y limit to 0,30. I want to manually set the individual y limits for each chart. I do NOT want to use scales = "free_y" because I need control over the limits in each chart.

Is there a way this can be done? Can you somehow supply y limits for each chart in a facet wrap?

解决方案

Unless you want to decrease your plotting area (i.e. not plot some points), you can still have "full" control over your y limits while using scales = "free_y".

You can use the same trick I have given to answer your other question: how to set limits on rounded facet wrap y axis?

dat <- data.table(dat)

dat[,y_min := y*0.5, by = group]
dat[,y_max:= y*1.5, by = group]

ggplot(dat, aes(x = x, y = y)) + 
  geom_point() + 
  geom_line() + 
  facet_wrap(~group, ncol = 1, scales = "free_y") +
  geom_blank(aes(y = y_min)) +
  geom_blank(aes(y = y_max))

For others reading this question, trick is to explicitly create y_min and y_max variables for each group. And "plot" them via geom_blank(). (Nothing is actually plotted, but each facet's plotting area is adjusted based on y_min and y_max values for that group).

If for some reasons, you want to manually give min and max (instead of a rule), none is stopping you. But it is tedious:

dat[group == "a",y_min := 0]
dat[group == "a",y_max := 30]
dat[group == "b",y_min := 0]
dat[group == "b",y_max := 3000]

ggplot(dat, aes(x = x, y = y)) + 
  geom_point() + 
  geom_line() + 
  facet_wrap(~group, ncol = 1, scales = "free_y") +
  geom_blank(aes(y = y_min)) +
  geom_blank(aes(y = y_max))

But, as I have mentioned this works if you want to extend your limits, not decrease them.

这篇关于用facet换行设置个别的y轴限制,不用缩放free_y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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