r - 我在 R 中的想象图 - 条形间距和条形高度之间的 map

标签 r graphics graph map ggplot2

我想开发以下类型的图表:

其中位置决定了bar的位置(不是单向而是双向,虽然方向没有特殊的意义但看起来像 map 一样美观),height决定了每个位置的bar的高度。以下是对应的数据集。

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

他们的任何图形包都可以做到这一点。我想欢迎您的创新想法将受到高度赞赏。我相信基础 R 图形或 ggplot2 是灵活的(但不知道如何)来制作多种类型的图形。

最佳答案

这是一个使用 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()

更新

这是一个带有单杠的示例:
# 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))

再次更新

我应该使用 geom_tile :
 ggplot(mydf, aes(position, factor(group), group = factor(group))) +
   geom_line(size = 2, colour = "skyblue") +
   geom_tile(aes(height = barheight))

再次更新
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")

关于r - 我在 R 中的想象图 - 条形间距和条形高度之间的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8727625/

相关文章:

r - 如何向 quantmod 添加多条垂直线?

java - 如何在 Java3D 中旋转一组对象

api - 时间线图-如何处理“时间间隔”?

graph - Neo4j/Gremlin/密码 : how to get all nodes until i reach a certain distance (depth) in a map-like setup?

java - 持久化 Guava Graph 的方法

r - 是否有 R 命令使 Keras Tensorflow-GPU 在 CPU 上运行?

html - 将颜色主题更改为 HTML markdown

r - 扩展轴的比例

c++ - 没有获得正确的阴影位置,基于 CPU 的光线追踪

java - 重绘时不调用paintComponent()