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

时间:2012-01-04 13:23:30

标签: r graphics graph map ggplot2

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

enter image description here

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

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可以灵活(但不怎么样)来做几种类型的图形。

3 个答案:

答案 0 :(得分: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()

enter image description here

更新

这是横条的示例:

# 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))

enter image description here

再次更新

我应该使用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")

答案 1 :(得分:2)

使用非常基本的命令可以让您更好地控制布局,并使图形布局更加整洁。在我的方法中,我只使用fields包来制作水平线,其余的则使用来自graphics的基本命令:

#Create example data with coordinates for plotting height of bars
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)
y.start <- c(group-barheight/2)
y.end <- c(group+barheight/2)
mydf <- data.frame (position, group, barheight, y.start, y.end)
#Remove any crap from the plot
plot(0,type="n",ylim=c(0,3),xlim=c(0,10),axes=F,ylab="",xlab="")
#Create two horizontal lines
require(fields)
yline(1,lwd=4)
yline(2,lwd=4)
#Create text for the lines
text(10,1.1,"Group 1",cex=0.7)
text(10,2.1,"Group 2",cex=0.7)
#Draw vertical bars
segments(mydf$position[1:8],mydf$y.start[1:8],y1=mydf$y.end[1:8])
segments(mydf$position[9:15],mydf$y.start[9:15],y1=mydf$y.end[9:15])
#Add circle in custom position
require(plotrix)
draw.circle(mydf$position[14],2,0.2)
draw.circle(mydf$position[4],1,0.2)

enter image description here

答案 2 :(得分:1)

这会让你接近吗?

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)

library(ggplot2)
ggplot(mydf, aes(position, barheight)) + geom_bar(stat = "identity") + 
  facet_grid(group ~ .)

enter image description here