我在R中的想象力图 - 一个条与条高度之间的间距

jon*_*jon 4 graphics r graph map ggplot2

我想开发以下类型的图:

在此输入图像描述

位置决定了杆的位置(不是单个方向而是两个方向,虽然方向没有特殊意义但美观看起来像地图)并且高度决定了每个位置的杆的高度.以下是相应的数据集.

position <- c(0, 1, 3, 4, 5, 7, 8, 9,   0, 1, 2, 4.5, 7, 8, 9)
group <- c(1, 1, 1,  1, 1, 1, 1, 1,   2, 2, 2, 2, 2, 2, 2)
barheight <- c(0.5, 0.4, 0.4, 0.4, 0.6,  0.3, 0.4, 1, 0.75, 0.75,
           0.75, 1, 0.8, 0.2, 0.6)
mydf <- data.frame (position, group, barheight)
mydf
   position group barheight
1       0.0     1      0.50
2       1.0     1      0.40
3       3.0     1      0.40
4       4.0     1      0.40
5       5.0     1      0.60
6       7.0     1      0.30
7       8.0     1      0.40
8       9.0     1      1.00
9       0.0     2      0.75
10      1.0     2      0.75
11      2.0     2      0.75
12      4.5     2      1.00
13      7.0     2      0.80
14      8.0     2      0.20
15      9.0     2      0.60
Run Code Online (Sandbox Code Playgroud)

他们的任何图表包都可以做到这一点.我想欢迎您的创新理念,将受到高度赞赏.我相信基础R图形或ggplot2是灵活的(但不怎么样)做几种类型的图形.

koh*_*ske 9

这是一个使用ggplot2的例子:

# top panel
ggplot(mydf, aes(position, factor(group), size = barheight)) + 
  geom_point() + opts(legend.position = "none")

# bottom panel
ggplot(mydf, aes(y = factor(group), 
                 xmin = position - 0.1, 
                 xmax = position + 0.1, 
                 ymin = group - barheight/2,
                 ymax = group + barheight/2)) + 
  geom_rect()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

UPDATE

这是横条的示例:

# arbitral bar length
bar <- data.frame(y = c(1, 1, 2, 2), x = c(0, 10, 1, 9))

ggplot() +
  geom_line(aes(x, factor(y), group = factor(y)), 
            bar, size = 2, colour = "skyblue") +
  geom_rect(aes(y = factor(group),
                xmin = position - 0.1, 
                xmax = position + 0.1, 
                ymin = group - barheight/2,
                ymax = group + barheight/2),
            mydf)

# bar length is from data range
ggplot(mydf) +
  geom_line(aes(position, factor(group), group = factor(group)),
            size = 2, colour = "skyblue") +
  geom_rect(aes(y = factor(group),
                xmin = position - 0.1, 
                xmax = position + 0.1, 
                ymin = group - barheight/2,
                ymax = group + barheight/2))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

再次更新

我应该用过geom_tile:

 ggplot(mydf, aes(position, factor(group), group = factor(group))) +
   geom_line(size = 2, colour = "skyblue") +
   geom_tile(aes(height = barheight))
Run Code Online (Sandbox Code Playgroud)

再次更新

ggplot(mydf, aes(position, factor(group), group = factor(group))) +
   geom_line(size = 2, colour = "skyblue") +
   geom_tile(aes(height = barheight)) +
   geom_point(aes(x, y, group = NULL), data.frame(x = c(5, 5), y = c(1, 2)),
     size = 5, colour = "cyan")
Run Code Online (Sandbox Code Playgroud)