Set tick mark distance within for loop for ggplot2.

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

Set tick mark distance within for loop for ggplot2

问题

问题

我有一个for循环,生成一组图表,它们的所有内容都相同,只是位置不同。

我想要能够控制刻度标记,使得每个图表上每个刻度标记之间的距离都相同。

我不想要硬性的限制,因为每个图表都太不同了。我希望每个刻度之间的间隔距离是相同的,这样这些图表就可以更容易比较了。也就是说,我希望所有的y轴是相对于彼此的。

注意: 我不希望它们都有相同的坐标轴,我只是希望它们是相对的。每个图表上两个刻度之间的距离是相同的。

目前的图表外观:

Set tick mark distance within for loop for ggplot2.

英文:

Problem

I have a for loop that produces a set of graphs that are all the same but for different locations.

I want to be able to control the tick marks so that the the distance between each tick mark is the same for each plot. I don't want hard set limits as each graph is too different. I'd like the interval distance between each tick to be the same, so the graphs are more comparable. i.e. I'd like all the y axes to be relative to one another.

NOTE: I don't want them all the have the same axes, I just want them to be relative. The distance between two ticks is the same on each plot.

Plots currently look like this:

Set tick mark distance within for loop for ggplot2.

Current Code

j = 1   # counter for plot title and index
for (datTime in chapel){
  plotName <- names(chapel)[j]
  j = j+1
  
  datTime <- datTime %>% 
    filter((DateTime > as.POSIXct("2020-03-05 00:00:00")) & (DateTime < as.POSIXct("2023-03-04 00:00:00")))
  
  plot <- 
    datTime %>%
    ggplot(aes(x=DateTime)) +
    geom_point(aes(y=mAOD), col='grey') +
    geom_smooth(aes(y=mAOD), col='black', se=FALSE, span=0.1, method="loess") +
    geom_hline(yintercept=datTime$groundLevel, col='dark green') +
    theme_classic() +
    labs(y='Water Depth (mAOD)', x=NULL) +
    ggtitle(plotTitles[[plotName]][1]) + 
    scale_x_datetime(
      breaks=seq(update(min(datTime$DateTime), month = 1, day = 1), max(datTime$DateTime), 
                 by= "1 year"), date_labels="%Y") +
    scale_y_continuous(labels=scaleFUN,  limits = c(81.5, 84.8)) +
    theme(text=element_text(size=20, family='Calibri Light')) +
    theme(plot.margin = unit(c(1, 1, 1, 1), 'cm')) +
    theme(axis.title.y=element_text(margin=margin(t=0, r=20, b=0, l=0))) +
    theme(axis.title.x=element_text(margin=margin(t=20, r=0, b=0, l=0))) +
    theme(axis.title.y.right=element_text(margin=margin(t=0, r=0, b=0, l=20)))
  
  plotlist_controlSites[[plotName]] <- plot
}

Dataframe

The data frames each have a datetime column and a mAOD column (all values vary between 81 and 84). There are 10 plots in total.

This is the top 10 lines of the first dataframe in the list "chapel":

structure(list(DateTime = structure(c(1576022400, 1576108800, 
1576195200, 1576281600, 1576368000, 1576454400), class = c("POSIXct", 
"POSIXt"), tzone = "GMT"), mAOD = c(NA, 82.4402870833333, 82.5335010416667, 
82.5776004166667, 82.5414891666667, 82.505000625)), row.names = c(447L, 
488L, 529L, 570L, 611L, 652L), class = "data.frame")

答案1

得分: 1

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

以您的数据框 `df` 为例,创建一个类似 `chapel` 的列表:

chapel <- list(
  basic = df,
  plus_ten = mutate(df, mAOD = mAOD + 10),
  plus_40 = mutate(df, mAOD = mAOD + 40)
)

chapel %>%
  map2(names(chapel), ~ mutate(.x, title = .y)) %>%
  bind_rows() %>%
  ggplot(aes(DateTime, mAOD)) +
  geom_smooth() +
  facet_wrap(~title)

Set tick mark distance within for loop for ggplot2.


<details>
<summary>英文:</summary>

Taking your example data.frame as `df` and creating a list that looks like `chapel`

chapel <- list(
basic = df,
plus_ten = mutate(df, mAOD = mAOD + 10),
plus_40 = mutate(df, mAOD = mAOD + 40)
)

chapel %>%
map2(names(chapel), ~ mutate(.x, title = .y)) %>%
bind_rows() %>%
ggplot(aes(DateTime, mAOD)) +
geom_smooth() +
facet_wrap(~title)


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/UVYPH.png

</details>



# 答案2
**得分**: 0

我成功地解决了这个问题,方法是找到了y轴数据的最大范围。然后将ylim设置为最小的y值和最小值加上数据中最大的范围。现在所有的y轴都是相对于彼此的,可以通过视觉比较图表。

我将这部分包括在了上面的for循环中:

```R
min <- min(datTime$mAOD, na.rm=T) 
max <- (min + 1.8)
top <- max(datTime$mAOD, na.rm=T)
range <- (top - min)
print(range)

并在ggplot中添加了这一部分:

scale_y_continuous(labels=scaleFUN, breaks=seq(70, 90, by=0.5), limits = c(min, max))

可能有点小技巧,但它完成了工作。

英文:

I managed to solve this by finding the max range of the data on the y axis. Then Setting the ylims to the minimum y value and the min + the largest range in the data. Now all the y axis are relative to one another and plots can be compared visually.

I included this in the for loop above the plot

min &lt;- min(datTime$mAOD, na.rm=T) 
  max &lt;- (min + 1.8)
  top &lt;- max(datTime$mAOD, na.rm=T)
  range &lt;- (top - min)
  print(range)

And added in this to the ggplot.

scale_y_continuous(labels=scaleFUN, breaks=seq(70, 90, by=0.5), limits = c(min, max))

Maybe a slight hack, but it did the job.

Set tick mark distance within for loop for ggplot2.

huangapple
  • 本文由 发表于 2023年5月22日 22:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307413.html
匿名

发表评论

匿名网友

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

确定