R 如何使用数据表的两列在ggplot2中定义x轴范围

R 如何使用数据表的两列在ggplot2中定义x轴范围,r,ggplot2,R,Ggplot2,我有下面的数据表 name rating min_date max_date weight 1 abc123 39 2018-04-29 2018-04-29 2000 2 abc123 80 2018-04-16 2018-07-31 3131 使用此功能,我想使用ggplot库为每行绘制一条线,在x轴上保持最小日期到最大日期,在y轴上保持额定值,线的颜色将根据重量而变化。我更改了数据,因为第一行中有

我有下面的数据表

    name        rating  min_date    max_date    weight
1   abc123      39     2018-04-29   2018-04-29  2000
2   abc123      80     2018-04-16   2018-07-31  3131

使用此功能,我想使用ggplot库为每行绘制一条线,在x轴上保持最小日期到最大日期,在y轴上保持额定值,线的颜色将根据重量而变化。

我更改了数据,因为第一行中有相同的
min\u日期
max\u日期
,并且只有两个观察值

data <- read.table(text = "name        rating  min_date    max_date    weight
1   abc123      39     2018-04-29   2018-06-29  2000
2   abc123      80     2018-04-16   2018-07-31  3131
3   abc123      56     2018-04-15   2018-05-30  1831", header = T)

第二种选择是将is作为一个因素:

ggplot(data, aes(x = min_date, xend = max_date, 
                 y = rating, yend = rating, 
                 col = as.factor(weight))) + 
  geom_segment()

我正在试图弄清楚这张图是什么样子的……似乎只有一个y值,所以我不确定如何用它来划线。你能澄清或展示一个手绘的例子吗?从那里应该很容易得到答案。
ggplot(data, aes(x = min_date, xend = max_date, 
                 y = rating, yend = rating, 
                 col = as.factor(weight))) + 
  geom_segment()