ggplot2 plot table as lines

后端 未结 3 1202
孤街浪徒
孤街浪徒 2021-02-04 16:47

I would like to plot the following dataset

structure(list(X = structure(c(3L, 12L, 11L, 7L, 13L, 2L, 1L, 
10L, 5L, 4L, 8L, 14L, 9L, 6L), .Label = c(\"BUM\", \"DD         


        
3条回答
  •  鱼传尺愫
    2021-02-04 17:18

    First, melt the data to put it in a long format.

    melted_data <- melt(the_data, id.vars = "X")
    

    Now draw the plot with a numeric x axis, and fix up the labels.

    p <- ggplot(melted_data, aes(as.numeric(variable), value, colour = X)) + 
      geom_line() + 
      scale_x_continuous(
        breaks = seq_len(nlevels(melted_data$variable)), 
        labels = levels(melted_data$variable)
      ) +
      opts(axis.text.x = theme_text(angle = 90))
    p
    

    Having answered this, I'm not sure what the plot tells you &ndahs; it's just a jumble of lines to me. You might be better greying out most of the lines, and highlighting one or two interesting ones.

    Add a column that picks out, e.g., EXP.

    melted_data$is_EXP <- with(melted_data, X == "EXP")
    

    Ignore my previous anser; Andrie's is better. Use manual colour and size scales to highlight your new column.

    p <- ggplot(melted_data, aes(variable, value, colour = is_EXP, size = is_EXP, group = X)) + 
      geom_line() + 
      scale_colour_manual(values = c("grey80", "black")) + 
      scale_size_manual(values = c(0.5, 1.5)) +
      opts(axis.text.x = theme_text(angle = 90, hjust=1))
    p
    

    enhanced plot

提交回复
热议问题