如何在同一图上绘制两条Lorenz曲线?

时间:2019-01-11 20:47:25

标签: r ggplot2 plot

我想在一个图形上绘制两个洛伦兹曲线(data1和data2),所以请提供代码帮助我?

我有一个代码可以完美地与一条曲线(data1)配合使用,但是我想与同一条曲线上的tho曲线(data1和data2)进行比较。

struct a { int x; }; // Regular complete struct type
typedef struct a aa; // Alias 'aa' for the struct 'a'

struct { int x; } b; // Tag 'b'
typedef struct b bb; // Compile, but unusable.

struct c { int x; } C; // identifier or struct name 'c' and tag 'C'
typedef struct { int x; } d; // Alias 'd'
typedef struct e { int x; } ee; // struct 'e' and alias 'ee'

Plot

1 个答案:

答案 0 :(得分:1)

这是一个最小的,可复制的示例,该示例说明如何使用ggplot2在同一图上显示多条曲线。这里的关键点是将“长格式”的数据传递给ggplot()函数。

library(ggplot2)

# Construct example data.frame with separate columns for each curve.
x = seq(from=0, to=3, by=1)
dat = data.frame(x=x,
                 y1=9 * x,
                 y2=3 * x^2,
                 y3=x^3)

dat
#   x y1 y2 y3
# 1 0  0  0  0
# 2 1  9  3  1
# 3 2 18 12  8
# 4 3 27 27 27

# Convert data to long form, putting all y-values in a single column.
mdat = reshape2::melt(dat, id.vars="x", measure.vars=c("y1", "y2", "y3"))

mdat
#    x variable value
# 1  0       y1     0
# 2  1       y1     9
# 3  2       y1    18
# 4  3       y1    27
# 5  0       y2     0
# 6  1       y2     3
# 7  2       y2    12
# 8  3       y2    27
# 9  0       y3     0
# 10 1       y3     1
# 11 2       y3     8
# 12 3       y3    27

p = ggplot(data=mdat, aes(x=x, y=value, colour=variable, group=variable)) +
    geom_point() + 
    geom_line()

enter image description here