【问题标题】:line graph with multiple variables on y axis stepwisey轴上具有多个变量的折线图逐步
【发布时间】:2019-11-20 22:35:05
【问题描述】:

我需要一些帮助。这是我要绘制的数据。我想将 $path.ID 保留在 y 轴上,并逐步添加所有其他列的数字。这是非常大的数据集的一个子集,所以我想将 pathID 标签附加到每一行。如果可能的话,还有每个点的其他列的值。

head(table)
  Path.ID     sc    st    rc    rt
  <chr>    <dbl> <dbl> <dbl> <dbl>
1 map00230     1    12     5    52
2 map00940     1    20    10    43
3 map01130    NA    15     8    34
4 map00983    NA    14     5    28
5 map00730    NA     5     3    26
6 map00982    NA    16     2    24

somewhat like this 谢谢

【问题讨论】:

  • 我想知道这是否适合您,每行末尾都有一个标签? imgur.com/xqaz7KC
  • 请把这个代码的链接发给我,我会告诉你

标签: r ggplot2 r-plotly


【解决方案1】:

这是伪代码。

library(tidyr)
library(dplyr)
library(ggplot2)
# convert your table into a long format - sorry I am more used to this type of data
table_long <- table %>% gather(x_axis, value, sc:rt)

# Plot with ggplot2
ggplot() +
  # draw line
  geom_line(data=table_long, aes(x=x_axis, y=value, group=Path.ID, color=Path.ID)) +
  # draw label at the last x_axis in this case is **rt**
  geom_label(data=table_long %>% filter(x_axis=="rt"),
             aes(x=x_axis, y=value, label=Path.ID, fill=Path.ID),
             color="#FFFFFF")

请注意,使用此代码,如果 Path.ID 没有 rt 值,那么它将没有任何标签

【讨论】:

  • > ggplot() + + # 画线 + geom_line(data=table, aes(x=x_axis, y=value, group=Path.ID, color=Path.ID)) + + #在这种情况下,在最后一个 x_axis 绘制标签是 rt + geom_label(data=table %>% filter(x_axis=="rt"), + aes(x=x_axis, y=value, label= Path.ID, fill=Path.ID), + color="#FFFFFF") UseMethod("filter_") 中的错误:没有适用于 'filter_' 的方法应用于“function”类的对象
  • @shahzad:我刚刚意识到我需要将图形table 的代码改为table_long
  • 我没有实际数据来说明为什么它省略了行。我的猜测是很多Path.IDrt 列上有NA。如果是这种情况,您可以查看您的数据并尝试gather() 中的fill 选项,如果可以接受,请填写0(Zero)
  • 我解决了。谢谢你。但是减少 PathID 文本大小的 cex 不起作用还有其他方法吗
  • geom_label() 有一些尺寸选项 - 您可以查看可用选项并尝试最适合您的选项 here
【解决方案2】:
p<-ggplot() +
  # draw line
  geom_line(data=table_long, aes(x=x_axis, y=value, group=Path.ID, color=Path.ID)) +
  geom_text(data=table_long %>% filter(x_axis=="rt"),
             aes(x=x_axis, y=value, label=Path.ID),
             color= "#050505", size = 3, check_overlap = TRUE)
p +labs(title= "title",x = "x-lable", y="y-label")

我必须使用 geom_text,因为我有很大的数据集,它给了我更清晰的图表 谢谢@sinh,它帮助很大。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多