简体   繁体   English

R-ggplot2-将区域绘制为线

[英]R - ggplot2 - plot regions as lines

I have a dataframe with regions and values. 我有一个包含区域和值的数据框。 Here's a toy example. 这是一个玩具示例。

Start    End    Value
1        100     2
100      200     3
300      400     2
400      500     1

What I'd like to do is create a plot where each region (in line 1, that is Start=1 , End=100 ) is plotted on the x, and the Value is on the y. 我想做的是创建一个绘图,其中每个区域(第1行,即Start=1End=100 )都绘制在x上,而Value则在y上。 And preferably, I'd like to use ggplot2. 而且最好是,我想使用ggplot2。 I have many different applications for this, but they all boil down to this one question. 我为此有许多不同的应用程序,但它们都归结为这个问题。

What I'd end up with is a plot with flat (slope=0) lines for each region. 我最终得到的是每个区域的平坦(slope = 0)线图。 Here's an example of the type of plot if you ignore the points, and just focus on the lines . 如果忽略点,而只关注线 ,这是情节类型的示例。

Copy number plot 复制号码图

In words, you would have one plot, with (for the toy data) a line from x=1-100 with ay value of 2, then lines (x,y): (100-200, 3)(300-400, 2)(400-500, 1) 换句话说,您将有一个图,(对于玩具数据)从x = 1-100开始,y值为2的直线,然后是(x,y):(100-200,3)(300-400, 2)(400-500,1)

You can use geom_segment to draw line segments. 您可以使用geom_segment绘制线段。 See ?geom_segment for details. 有关详细信息,请参见?geom_segment

ggplot(df) + 
    geom_segment(aes(x = Start, xend = End, y = Value, yend = Value))

在此处输入图片说明

Using this data: 使用此数据:

df = structure(list(Start = c(1L, 100L, 300L, 400L), End = c(100L, 
200L, 400L, 500L), Value = c(2L, 3L, 2L, 1L)), .Names = c("Start", 
"End", "Value"), class = "data.frame", row.names = c(NA, -4L))

You should probably check out some introductory ggplot2 resources. 您可能应该检查一下ggplot2资源。 There are lots of suggestions in the ggplot2 tag wiki . ggplot2标签Wiki中有很多建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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