简体   繁体   English

为具有小平面的图设置不同的Y轴范围(比例=“免费”似乎无效)

[英]Setting different Y-axis ranges for plot with facets (scales = “free” seems not working)

I am trying to set different Y-axis ranges for different facets. 我试图为不同的方面设置不同的Y轴范围。 I read many answers on the web and I tried to apply them on my script, but it seems not working. 我在网上阅读了许多答案,并尝试将其应用到脚本中,但似乎无法正常工作。

I am using scales = "free" to make the axis appear on all the facets and I am using geom_blank to set different axis limits depending on the variable Variable of the somePoints dataframe 我正在使用scales = "free"使轴出现在所有构面上,并且我正在使用geom_blank根据somePoints数据帧的变量Variable设置不同的轴限制

Here is a reproducible example of the data: 这是数据的可重现示例:

library(ggplot2)

dummy <- expand.grid(Year = NA, Season = c("Winter","Spring","Summer","Fall"),
                     Variable = c("P","T"), stringsAsFactors=FALSE)

dummy$ymin = NA
dummy$ymax = NA
dummy[dummy$Variable=="P","ymin"] = 0
dummy[dummy$Variable=="P","ymax"] = 400
dummy[dummy$Variable=="T","ymin"] = -10
dummy[dummy$Variable=="T","ymax"] = 30

somePoints = expand.grid(Year = NA, Season = c("Winter","Spring","Summer","Fall"),
                         Variable = c("P","T"), stringsAsFactors=FALSE)

somePoints$Value = NA
somePoints[somePoints$Variable=="P","Year"] = 1940
somePoints[somePoints$Variable=="T","Year"] = 1940
somePoints[somePoints$Variable=="P","Value"] = c(350,365,200,150)
somePoints[somePoints$Variable=="T","Value"] = c(-5,8,25,7)

Basically what I want is having the same Y-axis for P Variable ranging from 0 to 400 and the same Y-axis for T Variable ranging from -10 to 30. 基本上我想要的是具有相同的Y轴(范围从0到400的P变量)和相同的Y轴(范围从-10到30的T变量)。

Here are the ggplot code lines I am using to obtain what I want: 这是我用来获取所需的ggplot代码行:

ggplot()+

  facet_grid(Season ~ Variable, scales = "free_y")+

  geom_blank(data = dummy,aes(y=ymin)) +
  geom_blank(data = dummy,aes(y=ymax)) +

  geom_point(data = somePoints, aes(Year,Value), size = 5) +
  geom_point(data = somePoints, aes(Year,Value), size = 5)

What I notice is that setting scales = "free_y" in the facet_grid function seems not working. 我注意到的是,在facet_grid函数中设置scales = "free_y"似乎无效。

What am I doing wrong? 我究竟做错了什么? I feel I am missing something stupid. 我觉得我缺少一些愚蠢的东西。

Thank you. 谢谢。

If I'm understanding correctly, your issue is more with geom_blank . 如果我的理解正确,那么您的问题更多是与geom_blank The purpose of geom_blank is to ensure common scales between different plots. geom_blank的目的是确保不同地块之间具有相同的比例尺。 You should use the expand argument instead. 您应该改为使用expand参数。 I suggest trying the following: 我建议尝试以下方法:

library(ggplot2)
ggplot()+

  facet_grid(Season ~ Variable, scales = "free_y")+

  geom_point(data = somePoints, aes(Year,Value), size = 5) +
  geom_point(data = somePoints, aes(Year,Value), size = 5) +

  scale_y_continuous(expand = c(.2,0))

expand is just buffering your edges. expand只是缓冲您的边缘。

Alternatively, you can use facet_wrap , rather than facet_grid so that the axes are displayed on each plot. 或者,可以使用facet_wrap而不是facet_grid以便在每个图上显示轴。 We'll just specify number of columns ( ncol = 2 ) and vertical direction ( vir = "v" ): 我们只指定列数( ncol = 2 )和垂直方向( vir = "v" ):

ggplot()+

  facet_wrap(Variable ~ Season, scales = "free_y",
             dir = "v", ncol = 2)+

  geom_blank(data = dummy,aes(y=ymin)) +
  geom_blank(data = dummy,aes(y=ymax)) +

  geom_point(data = somePoints, aes(Year,Value), size = 5) +
  geom_point(data = somePoints, aes(Year,Value), size = 5) +

  scale_y_continuous(expand = c(.2,0))

If you don't like the labels, you can pass labeller = labeller(.multi_line = FALSE) to facet_grid 如果您不喜欢标签,则可以将labeller = labeller(.multi_line = FALSE) facet_grid labeller = labeller(.multi_line = FALSE)传递给facet_grid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM