平行坐标用跳过和未排序的坐标绘制

时间:2017-03-02 16:35:55

标签: r ggplot2

人们在不同的赛道上比赛,他们的时间是每门课程的衡量标准。有些人可能无法完成曲目,他们的时间在数据框中标有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)

生成带有以下图像的图像: enter image description here
我想删除跨越Olympic.Stadion的超人和懒人的线路。

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

1 个答案:

答案 0 :(得分: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()))

enter image description here