在ggplot2中,根据给定条件对绘制的线进行对齐。

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

In ggplot2, align plotted lines according to the given condition

问题

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

让我们有以下的虚拟数据:

library(tidyverse)
library(ggplot2)

df <- tibble(
  id = c(rep("abcdef-123", 3), rep("defghi-678", 2), rep("mnopqr-345", 1)),
  length = c(rep(137, 3), rep(293, 2), rep(91, 1)),
  position = c(10, 77, 103, 82, 222, 45)
)

这个数据框包含3列。 "id" 对应于对象(物品)的名称, "length" 对应于物品的总长度,而 "position" 指示在给定的 "length" 中发生了一个有趣特征的位置。因此,每个唯一的 "id" 都有唯一的 "length",但可能会观察到多个 "position" 与一个 "id" 相关。

我将数据按 "id" 分组,因为这是每个物品的唯一标签:

df_grouped <- df %>% group_by(id)

然后,我想以以下方式绘制数据:

- 每个 "id" 应该表示为一个单独的水平线
- 位置应该标记为点
- 线应根据每个 "id" 中的第一个(或理想情况下:选择的)位置对齐

到目前为止,我能够获得以下结果:

ggplot2::ggplot(df_grouped, aes(x=length, y=id, xend=0, yend=id)) + 
  ggplot2::geom_segment()+ 
  ggplot2::geom_point(aes(x=position, y=id), size=2) + 
  ggplot2::theme_void() +
  ggplot2::theme(axis.ticks.x = element_blank(), axis.text.x = element_blank())#+ggplot2::scale_y_discrete()

希望这对你有帮助。如果有其他问题,请随时提出。

英文:

Let's have a following dummy data:

library(tidyverse)
library(ggplot2)

df &lt;- tibble(
  id = c(rep(&quot;abcdef-123&quot;, 3), rep(&quot;defghi-678&quot;, 2), rep(&quot;mnopqr-345&quot;, 1)),
  length = c(rep(137, 3), rep(293, 2), rep(91, 1)),
  position = c(10, 77, 103, 82, 222, 45)
)

This dataframe contains 3 columns. "id" corresponds to the object (item) name, "length" corresponds to the total length of the item, while the "position" indicates where in the given "length" an interesting feature occurred. So each unique "id" has its unique "length", while there might be more than one "position" observed per "id".

I group the data by the "id", as this is the unique label of each item:

df_grouped &lt;- df %&gt;% group_by(id)

Then I want to plot the data in the following manner:

  • each "id" should be depicted as a separate horizontal line
  • positions should be marked as points
  • the lines should be aligned according to the first (or ideally:chosen) position in each "id"

This is what I am able to obtain so far:

ggplot2::ggplot(df_grouped, aes(x=length, y=id, xend=0, yend=id)) + 
  ggplot2::geom_segment()+ 
  ggplot2::geom_point(aes(x=position, y=id), size=2) + 
  ggplot2::theme_void() +
  ggplot2::theme(axis.ticks.x = element_blank(), axis.text.x = element_blank())#+ggplot2::scale_y_discrete()

在ggplot2中,根据给定条件对绘制的线进行对齐。

And this is what I want to achieve (I made this in gimp):
在ggplot2中,根据给定条件对绘制的线进行对齐。

I do not know how to align lines conditionally (according to the selected first or n-th position). I tried multiple solutions, including indexing positions in brackets while passing the arguments to the aesthetics. This did not work, so I am asking for help.

Currently trying to figure this out with Bioconductor, but would appreciate base R or ggplot2 solution(s).

答案1

得分: 2

First step is to shift the position by the maximum value of the first position per id. Second is to shift the starting positions for your segments which is the minimum of the shifted or nudged positions minus the position of the first point per id. Note that I use a separate and summarized dataset for the segments.

library(tidyverse)

df &lt;- df |&gt;
  mutate(start = min(position), .by = id) |&gt;
  mutate(
    start_max = max(start),
    nudge = start_max - start,
    position_nudge = position + nudge
  )

df_segment &lt;- df |&gt;
  mutate() |&gt;
  summarise(
    x = min(position_nudge - start),
    xend = min(x + length),
    .by = id
  )

ggplot(df) +
  geom_segment(data = df_segment, aes(x = x, xend = xend, y = id, yend = id)) +
  geom_point(aes(x = position_nudge, y = id), size = 2) +
  theme_void() +
  theme(
    axis.ticks.x = element_blank(),
    axis.text.x = element_blank()
  )

在ggplot2中,根据给定条件对绘制的线进行对齐。

英文:

First step is to shift the position by the maximum value of the first position per id. Second is to shift the starting positions for your segments which is the minimum of the shifted or nudged positions minus the position of the first point per id. Note that I use a separate and summarized dataset for the segments.

library(tidyverse)

df &lt;- df |&gt;
  mutate(start = min(position), .by = id) |&gt;
  mutate(
    start_max = max(start),
    nudge = start_max - start,
    position_nudge = position + nudge
  )

df_segment &lt;- df |&gt;
  mutate() |&gt;
  summarise(
    x = min(position_nudge - start),
    xend = min(x + length),
    .by = id
  )

ggplot(df) +
  geom_segment(data = df_segment, aes(x = x, xend = xend, y = id, yend = id)) +
  geom_point(aes(x = position_nudge, y = id), size = 2) +
  theme_void() +
  theme(
    axis.ticks.x = element_blank(),
    axis.text.x = element_blank()
  )

在ggplot2中,根据给定条件对绘制的线进行对齐。<!-- -->

huangapple
  • 本文由 发表于 2023年4月6日 19:45:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949138.html
匿名

发表评论

匿名网友

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

确定