R和ggplot2:在分面网格中控制刻度

huangapple go评论65阅读模式
英文:

R and ggplot2: controlling scales in facet grid

问题

以下是代码部分的翻译:

library(tidyr)
library(ggplot2)

s <- function(d) ifelse(d < 500, "down", "up")

dist <- seq(1, 1000)
cadence <- floor(25 + 7 * sin(dist/250))
speed <- 4 + sin(dist/250) * 2
stream <- s(dist)

df <- data.frame(dist, cadence, speed, stream)
g <- gather(df, variable, value, speed, cadence)

p <- ggplot(g, aes(dist, value)) +
    geom_line(aes(color=stream, group=1)) + 
    scale_x_continuous(name = "distance [m]") +
    facet_grid(variable ~ ., scales = "free_y")

不包括代码的部分没有需要翻译的内容。

英文:

I am plotting for a rowing boat the relation between cadence, speed, and going up- or down stream using facets in ggplot2:

R和ggplot2:在分面网格中控制刻度

library(tidyr)
library(ggplot2)

s &lt;- function(d) ifelse(d &lt; 500, &quot;down&quot;, &quot;up&quot;)

dist &lt;- seq(1,1000)
cadence &lt;- floor(25 + 7 * sin(dist/250))
speed &lt;- 4+sin(dist/250)*2
stream &lt;- s(dist)

df &lt;- data.frame(dist,cadence,speed,stream)
g &lt;- gather(df, variable, value, speed, cadence)

p &lt;- ggplot(g, aes(dist, value)) +
    geom_line(aes(color=stream, group=1)) + 
    scale_x_continuous(name = &quot;distance [m]&quot;) +
#    scale_y_continuous(sec.axis = sec_axis(~ (500/.),
#    name = &quot;split 
展开收缩
&quot;, # breaks=c(90,95,100,105,110,115,120,125,130,140,150)), # limits=c(3.0,5.5),name=&quot;speed [m/s]&quot;) + facet_grid(variable ~ ., scales = &quot;free_y&quot;)

I rely on scales=&quot;free_y&quot; to get good automatic scaling of the Y axis. However, I would like to have more control and don't know how to achieve this:

  • I would like to limit each y axis individually, if possible
  • I would like to add a second y axis for the speed plot, showing a derived pace of time per 500m.

I know how to do this in individual plots but the facet grid makes sure the plots are properly aligned along the distance.

答案1

得分: 1

以下是代码部分的翻译:

One option would be the ggh4x package which offers some options e.g. facetted_pos_scales to specify the scale individually for each facet and/or to add a secondary scale. Note however that we are still in the world of facets so you won't be able to set the the axis titles individually. To achieve that I would suggest to use patchwork.

以下是代码中的翻译部分:

library(ggh4x)
library(ggplot2)

ggplot(g, aes(dist, value)) +
  geom_line(aes(color = stream, group = 1)) +
  scale_x_continuous(name = "distance [m]") +
  facet_grid(variable ~ ., scales = "free_y") +
  facetted_pos_scales(
    y = list(
      variable == "speed" ~ scale_y_continuous(
        limits = c(3, 5.5), name = "speed [m/s]",
        sec.axis = sec_axis(~ (500 / .), name = "split 
展开收缩
"
, breaks = c(90, 95, 100, 105, 110, 115, 120, 125, 130, 140, 150))
) ) ) + theme(strip.placement = "outside")

请注意,这是代码的翻译部分,不包括问题的回答。

英文:

One option would be the ggh4x package which offers some options e.g. facetted_pos_scales to specify the scale individually for each facet and/or to add a secondary scale. Note however that we are still in the world of facets so you won't be able to set the the axis titles individually. To achieve that I would suggest to use patchwork.

library(ggh4x)
library(ggplot2)

ggplot(g, aes(dist, value)) +
  geom_line(aes(color = stream, group = 1)) +
  scale_x_continuous(name = &quot;distance [m]&quot;) +
  facet_grid(variable ~ ., scales = &quot;free_y&quot;) +
  facetted_pos_scales(
    y = list(
      variable == &quot;speed&quot; ~ scale_y_continuous(
        limits = c(3, 5.5), name = &quot;speed [m/s]&quot;,
        sec.axis = sec_axis(~ (500 / .), name = &quot;split 
展开收缩
&quot;, breaks = c(90, 95, 100, 105, 110, 115, 120, 125, 130, 140, 150)) ) ) ) + theme(strip.placement = &quot;outside&quot;)

R和ggplot2:在分面网格中控制刻度

huangapple
  • 本文由 发表于 2023年2月16日 06:35:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定