【问题标题】:Parallel coordinates plot with skipped and unsorted coordinates带有跳过和未排序坐标的平行坐标图
【发布时间】:2017-03-02 16:35:54
【问题描述】:

人们在不同的赛道上比赛,每个赛道的时间都会被测量。有些人可能没有完成一首曲目,他们的时间在数据框中用 Inf 标记。我想在带有 Inf 值的特殊注释的平行坐标图中显示此信息。

previous post 中的答案与此类似:

require(ggplot2)

df <- data.frame(ID = factor(c(rep(1, 4), rep(2, 4), rep(3, 4)), labels = c('Realman', 'Lazyman', 'Superman')),
                 race = factor(rep(seq(1,4,1), 3), labels = c('Obstacle.Course', 'Olympic.Stadion', 'Jungle','Beach')),
                 runTime = c(8.9, 20.5, 150.9, 900, 100.1, +Inf, 300.3, 900, 1.2, +Inf, 5, 900))

str(df)

g <- ggplot(filter(df, runTime != +Inf), aes(x = race, y = runTime, group = ID, color = ID)) + 
  geom_line(size = 2) +
  geom_point(size = 4) +
  geom_line(data = df, linetype = 'dashed', size = 1) +        
  geom_point(data = df, shape = 21, size = 1) +
  geom_text(aes(label = runTime), position = position_nudge(y = -.1)) +
  scale_y_continuous(trans = 'log10', breaks = c(1, 10, 100, 1000)) +
  scale_x_discrete('Track') +
  scale_color_manual('Racer', values = brewer.pal(length(levels(df$ID)), 'Set1')) +
  theme(panel.background = element_blank(),
        panel.grid.major.x = element_line(colour = 'lightgrey', size = 25),
        legend.position = 'top',
        axis.line.y = element_line('black', .5, arrow = arrow()))


ggsave('image.png', g)

这会生成具有以下图像的图像:
我想删除穿过 Olympic.Stadion 的超人和懒人的线条。

仅供参考:这是previous question 的延续,可能会有更多信息。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以在实线上绘制的迹线中使用 NA 值,以在缺少(无限)值的地方打破它们:

    df$runTimeNA = df$runTime
    df$runTimeNA[df$runTime==+Inf] = NA
    
    ggplot(df, aes(x = race, y = runTime, group = ID, color = ID)) + 
      geom_line(aes(y = runTimeNA), size = 2) +
      geom_point(size = 4) +
      geom_line(data = df, linetype = 'dashed', size = 1) +        
      geom_point(data = df, shape = 21, size = 1) +
      geom_text(aes(label = runTime), position = position_nudge(y = -.1)) +
      scale_y_continuous(trans = 'log10', breaks = c(1, 10, 100, 1000)) +
      scale_x_discrete('Track') +
      scale_color_manual('Racer', values = brewer.pal(length(levels(df$ID)), 'Set1')) +
      theme(panel.background = element_blank(),
        panel.grid.major.x = element_line(colour = 'lightgrey', size = 25),
        legend.position = 'top',
        axis.line.y = element_line('black', .5, arrow = arrow()))
    

    【讨论】:

      猜你喜欢
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 2014-09-02
      相关资源
      最近更新 更多