在多个绘图的ggplot中更改y轴限制 [英] Change the y-axis limits in a ggplot of multiple plots

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

问题描述

我有以下ggplot:

I have the following ggplot:

:

df_long <- melt(df)

ggplot(df_long, aes(x = variable, y = value)) + 
  geom_boxplot() +
  facet_wrap(~variable, scales = 'free', ncol = 6)

我想更改y轴的限制,以每个变量的最大值从0开始,以最大值结束,并且还希望所有变量具有相同的间隔.目前, AC ND 很好,因为它们具有相同的间断(它们的标签甚至对齐),但其他变量不同.对于 NV IC ,它们没有以最大值结尾,并且它们与 AC ND .对于 PIC DBI ,它们的中断与 AC ND 的中断不同.在其他问题上,我希望所有y轴对齐并看起来优雅.

I would like to change the y-axis limits to start with 0 and end with the maximum value for each variable, and also to have the same breaks for all the variables. Currently, the AC and ND are fine since they have same breaks (their labels are even aligned) but the other variables are different. For the NV and IC, they don't end with the maximum value and they don't have the same breaks as with AC and ND. For the PIC and DBI, they don't have the same breaks as with AC and ND. In other woeds, I want all y axes to be aligned and look elegant.

您知道如何解决该问题吗?

Do you have any idea how to fix that?

推荐答案

您需要指定y轴的要求,并使用 scale_y_continuous 语句进行设置.

You need to specify your requirements for the y axis and set it up with the scale_y_continuous statement.

breaks 参数可以是从给定数据返回中断的函数,因此您可以设置一个函数,以在其最小值和最大值之间给出一个设置长度的序列.请注意,这些轴值可能没有多大意义(例如2.09、2.83,...),但它们在图形上的y位置相同.

The breaks argument can be a function returning breaks from the given data, so you can set up a function to give a sequence of set length between its min and max values. Note that these axis values may not make much sense (eg. 2.09, 2.83, ...) but they will be at the same y positions on the graph.

所选中断点的值非常不可读,因此我们还可以为 labels 参数指定一个函数,该函数将中断作为输入并返回四舍五入的标签.

The selected break values are pretty unreadable so we can also specify a function for the labels argument that takes the breaks as input and returns the rounded labels.

library(ggplot2)
# generate dummy data
set.seed(2)
df <- data.frame(var = sample(LETTERS[1:4], 1000, replace = T),
                 val = abs(rnorm(1000)))
df$val[df$var%in%c("B", "D")] <- df$val[df$var%in%c("B", "D")] / 2 
head(df)


# actual plotting
my_breaks <- function(x){seq(min(x), max(x), length.out = 5)}
my_labels <- function(x){round(x, digits = 2)}

ggplot(df, aes(x=var,y=val)) + geom_boxplot() + facet_wrap(~var, scales = 'free', ncol = 4) +
  scale_y_continuous(breaks = my_breaks, labels = my_labels)

这将输出以下图形

向轴添加一些约束

如果要将轴限制在特定范围内,则必须使用 my_breaks()函数定义.根据您的评论,我将在下面给您提供一些示例.

If you want to restrain your axes to specific ranges, you have to play around with the my_breaks() function definition. I'll give you a few examples below as per your comments.

  • 在0上启动轴: my_breaks<-函数(x){seq(0,max(x),length.out = 5)}}
  • 如果小于1,则结束on 1或最大值: my_breaks<-函数(x){seq(min(x),min(1,max(x)),length.out = 5)}}

我确定您可以找出满足您需求的具体要求;)

I'm sure you can figure out the specific requirements to your needs ;)

这篇关于在多个绘图的ggplot中更改y轴限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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