如何在使用ggplot2绘制的直方图上绘制箭头?

时间:2012-06-20 14:39:57

标签: r ggplot2 histogram

这是数据集:

   set.seed(123)
    myd <- data.frame (class = rep(1:4, each = 100), yvar = rnorm(400, 50,30))
    require(ggplot2)
    m <- ggplot(myd, aes(x = yvar))
    p <- m + geom_histogram(colour = "grey40", fill = "grey40", binwidth = 10)  +
       facet_wrap(~class) + theme_bw( ) 
    p + opts(panel.margin=unit(0 ,"lines"))

我想为每个主题类落入的条形图添加标签,并生成类似后续功能点处理图形的内容。在R中有没有办法做到这一点? ......

编辑:我们可以想到不同的指针,如点或误差条,如果箭头不是不可能的

enter image description here

让我们说以下是要标记的主题:

class   name       yvar
2       subject4    104.0
3       subject3    8.5
3       subject1    80.0
4       subject2    40.0
4       subject1    115.0

classd <- data.frame (class = c(2,3,3,4,4), 
 name = c  ("subject4", "subject3", "subject1", "subject2", "subject1"), 
 yvar = c(104.0, 8.5,80.0,40.0, 115.0))

2 个答案:

答案 0 :(得分:18)

以下部分解决方案使用geom_text()添加标签,geom_segment()添加arrow选项以添加箭头。

缺点是我必须为每个箭头和标签手动选择y位置。也许其他人可以帮助弄清楚如何以编程方式找到直方图栏高度。

set.seed(123)
myd <- data.frame (class = rep(1:4, each = 100), yvar = rnorm(400, 50,30))

library(ggplot2)
library(grid) # unit() is in the grid package.

arrow_pos = read.table(header=TRUE, stringsAsFactors=FALSE,
                       text="class   name       yvar
                             2       subject4    104.0
                             3       subject3    8.5
                             3       subject1    80.0
                             4       subject2    40.0
                             4       subject1    115.0")

arrow_pos$y = c(3, 5, 9, 13, 1) # Manually enter y position.
arrow_pos$class = factor(as.character(arrow_pos$class),
    levels=c("1", "2", "3", "4")) # Gets rid of warnings.

p1 = ggplot(myd, aes(x=yvar)) +
     theme_bw() +
     geom_histogram(colour="grey40", fill="grey40", binwidth=10) +
     facet_wrap(~ class) +
     opts(panel.margin=unit(0 ,"lines")) +
     geom_text(data=arrow_pos, aes(label=name, x=yvar, y=y + 2), size=3) +
     geom_segment(data=arrow_pos, 
                  aes(x=yvar, xend=yvar, y=y + 1.5, yend=y + 0.25),
                  arrow=arrow(length=unit(2, "mm")))

png("p1.png", height=600, width=600)
print(p1)
dev.off()

enter image description here

答案 1 :(得分:16)

更新 opts已弃用;请改用theme

稍微扩展bdemarest的响应,我认为这会以编程方式计算条形高度。 arrow_pos的最后两列包含相关信息:Freq是条形的高度; xval位于条形中点的x位置。但是,有些标签与条形图重叠。

编辑默认情况下,cut将其间隔界定为(b1,b2),而它表示ggplot2将其在geom_histogram中的间隔限制为[b1,b2]。我修改了代码,以便将它们的间隔限制为[b1,b2],即ggplot方式。

library(ggplot2)
library(grid) # unit() is in the grid package.
library(plyr)  # Data restructuring

set.seed(123)
myd <- data.frame (class = rep(1:4, each = 100), yvar = rnorm(400, 50, 30))

arrow_pos = read.table(header=TRUE, stringsAsFactors=FALSE,
                       text="class   name       yvar
                             2       subject4    104.0
                             3       subject3    8.5
                             3       subject1    80.0
                             4       subject2    40.0
                             4       subject1    115.0")

# Calculate the y positions for the labels and arrows
# For the myd data frame, obtain counts within each bin, but separately for each class
bwidth <- 10   # Set binwidth
Min <- floor(min(myd$yvar)/bwidth) * bwidth
Max <- ceiling(max(myd$yvar)/bwidth) * bwidth

# Function to do the counting
func <- function(df) {
   tab = as.data.frame(table(cut(df$yvar, breaks = seq(Min, Max, bwidth), right = FALSE)))
   tab$upper = Min + bwidth * (as.numeric(rownames(tab)))
   return(tab)
   }

# Apply the function to each class in myd data frame
TableOfCounts <- ddply(myd, .(class), function(df) func(df))

# Transfer counts of arrow_pos
arrow_pos$upper <- (floor(arrow_pos$yvar/bwidth) * bwidth) + bwidth
arrow_pos <- merge(arrow_pos, TableOfCounts, by = c("class", "upper"))
arrow_pos$xvar <- (arrow_pos$upper - .5 * bwidth)      # x position of the arrow is at the midpoint of the bin
arrow_pos$class=factor(as.character(arrow_pos$class),
    levels=c("1", "2", "3", "4")) # Gets rid of warnings.

ggplot(myd, aes(x=yvar)) +
     theme_bw() +
     geom_histogram(colour="grey70", fill="grey70", binwidth=bwidth) +
     facet_wrap(~ class) +
     theme(panel.margin=unit(0, "lines")) +
     geom_text(data=arrow_pos, aes(label=name, x=xvar, y=Freq + 2), size=4) +
     geom_segment(data=arrow_pos, 
                  aes(x=xvar, xend=xvar, y=Freq + 1.5, yend=Freq + 0.25),
                  arrow=arrow(length=unit(2, "mm")))

enter image description here