[英]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: 绘图条件为:
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. OP尚不完全清楚所需的输出是什么。 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
. ggplot2
首选长格式。 For this, melt()
from the data.table
package is used. 为此,使用了
data.table
包中的melt()
。
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"
)
# 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. 请注意,
melt()
在一个value
列中收集(或收集)了WC
, ASL
和TS
列的所有值,此后为类character
。
When plotting, ggplot2
implicitely turns characters to factors thereby using the alphabetical sort order for the levels. 绘制时,
ggplot2
隐式地将字符转换为因数,从而对级别使用字母排序顺序。 This means that the values of ASL
be plotted in the order 21, 27, 3, 4, 7 instead of 3, 4, 7, 21, 27. 这意味着
ASL
的值以ASL
的顺序绘制,而不是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. 为了使值的顺序保持在数据中,通过使用
forcats
包中方便的fct_inorder()
函数可以明确地强制forcats
。
library(ggplot2)
ggplot(molten, aes(x = value, y = LPD)) +
geom_line() +
geom_point() +
facet_wrap(~ variable, scales = "free_x") +
theme_bw()
The OP has given additional data. OP已提供了其他数据。 These can be plotted using
geom_step()
with 这些可以使用
geom_step()
与
ggplot(DT2, aes(x = grp, y = LPD)) + geom_point() + geom_step() +
theme_bw()
Note that the x-axis shows grp
as continuous scale. 请注意,x轴将
grp
显示为连续刻度。
According to the comments , the OP expects to get lines as "step-lines" . 根据评论 ,OP希望将行作为“阶梯线” 。 Unfortunately, the OP hasn't specfied what exactly to be displayed along the x-axis.
不幸的是,OP尚未指定确切沿x轴显示的内容。 So these are another wild guesses of what the OP might expect to see:
因此,这是OP可能会看到的另一种疯狂猜测:
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.
这些数字显示在x轴上。 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. 如果数据点在每个组中编号,则每个组都以1开头,则结果将显示得更加紧凑。
DT2[, rn := factor(rowid(grp))]
ggplot(DT2, aes(x = rn, y = LPD, group = grp, colour = grp)) +
geom_point() + geom_step() +
theme_bw()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.