简体   繁体   中英

multiple lines plot using multiple groups in data

Based on the certain conditions, I want to plot the data as given below:

WC     ASL  TS  LPD
7101    3   L   573.57
7101    3   L   323.33
7102    4   R   113.36
7102    4   R   3.05
7103    7   L   204.72
7103    7   L   123.65
7104    21  R   252.36
7104    21  R   225.74
7104    21  R   147.79
7104    21  R   151.47
7105    27  L   350.51
7105    27  L   41.61
7105    27  L   22.29
7105    27  L   2.00

Conditions for plot are:

  1. All the lines should be on one plot
  2. Plot (for line) is grouped by TS, ASL and WC

and I am trying something like this:

  ggplot(df2, aes(LPD, ASL, TS, colour=WC)) + 
  geom_line() + 
  geom_point()

Its all the mess... may be you could help me with better view in plot.

Because the above might be a lot confusing for many, I will make it a little simpler:

grp LPD
1   0.56744
1   0.4200938
2   0.070033616
2   0.065484951
3   0.327293
3   0.4718639
3   0.539516903
3   0.672646939
3   0.769603286
3   0.560730825
3   0.592725708
4   0.27415791
4   0.17706706
4   0.084262982
4   0.220420565
5   1.066580889
5   0.663611983
5   0.005299676
5   0.180121466
5   0.055205502
6   0.435355273
6   0.544843791
7   2.437439375
7   0.631948813
7   0.165961834
7   0.092725372
7   0.262442283
9   0.268541902
9   0.041798885
11  0.348282689
11  0.033044253
11  0.046070074
11  0.127173837
11  0.030580852
12  7.213290722
12  2.738036844
12  1.581291713
12  1.234204974
12  0.043930992
12  0.089781343
12  0.077482862
12  0.056506104
12  0.007676594

The OP isn't fully clear what the desired output is. However, concluding from the comments it might be something like this:

在此处输入图片说明

To create this plot, the data need to be reshaped from wide to long format. The long format is preferred by ggplot2 . For this, melt() from the data.table package is used.

Read data

library(data.table)
df2 <- fread(
  "WC     ASL  TS  LPD
  7101    3   L   573.57
  7101    3   L   323.33
  7102    4   R   113.36
  7102    4   R   3.05
  7103    7   L   204.72
  7103    7   L   123.65
  7104    21  R   252.36
  7104    21  R   225.74
  7104    21  R   147.79
  7104    21  R   151.47
  7105    27  L   350.51
  7105    27  L   41.61
  7105    27  L   22.29
  7105    27  L   2.00"
)

Prepare data

# reshape from wide to long format 
molten <- melt(setDT(df2), id = "LPD")
# ensure that factor levels are in order of appearance
molten[, value := forcats::fct_inorder(value)]

str(molten)
#Classes ‘data.table’ and 'data.frame': 42 obs. of  3 variables:
# $ LPD     : num  573.57 323.33 113.36 3.05 204.72 ...
# $ variable: Factor w/ 3 levels "WC","ASL","TS": 1 1 1 1 1 1 1 1 1 1 ...
# $ value   : Factor w/ 12 levels "7101","7102",..: 1 1 2 2 3 3 4 4 4 4 ...
# - attr(*, ".internal.selfref")=<externalptr> 

Note that melt() has collected (or gathered) all values of the WC , ASL , and TS columns in one value column which is of class character afterwards.

When plotting, ggplot2 implicitely turns characters to factors thereby using the alphabetical sort order for the levels. This means that the values of ASL be plotted in the order 21, 27, 3, 4, 7 instead of 3, 4, 7, 21, 27.

To keep the order of values as the appear in the data, the coercion to factor is done explicitely by using the handy fct_inorder() function from the forcats package.

Plot data

library(ggplot2)
ggplot(molten, aes(x = value, y = LPD)) + 
  geom_line() + 
  geom_point() + 
  facet_wrap(~ variable, scales = "free_x") +
  theme_bw()

Edit 1

The OP has given additional data. These can be plotted using geom_step() with

ggplot(DT2, aes(x = grp, y = LPD)) + geom_point() + geom_step() + 
  theme_bw()

在此处输入图片说明

Note that the x-axis shows grp as continuous scale.

Edit 2

According to the comments , the OP expects to get lines as "step-lines" . Unfortunately, the OP hasn't specfied what exactly to be displayed along the x-axis. So these are another wild guesses of what the OP might expect to see:

DT2[, grp := factor(grp)]
ggplot(DT2, aes(x = seq_len(nrow(DT2)), y = LPD, group = grp, colour = grp)) + 
  geom_point() + geom_step() + 
  theme_bw()

在此处输入图片说明

Note that here the data points are numbered consecutively. These numbers are shown on the x-axis. Data points are connected by a step function group-wise. In addition, the groups are colour-coded.

A more compact display will result if the data points are numbered within each group so that each group starts with 1.

DT2[, rn := factor(rowid(grp))]
ggplot(DT2, aes(x = rn, y = LPD, group = grp, colour = grp)) + 
  geom_point() + geom_step() + 
  theme_bw()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM