Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw an arrow on a histogram drawn using ggplot2?

Here is dataset:

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

I want to add labels to bars which each subject class fall into and produce something like the post-powerpoint processed graph. Is there way to do this within R ? ......

Edit: we can think of different pointer such as dot or error bar, if arrow is not impossible

enter image description here

Let's say the following is subjects to be labelled:

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)) 
like image 756
jon Avatar asked Jun 20 '12 14:06

jon


People also ask

How do you make an arrow in R?

To create an arrow R, we can use plot function and arrows function. We just need to understand all the coordinate values that should be passed inside the arrows function. For example, if we have two vectors that contains values from 1 to 10 then the arrow can be created by using arrows function as arrows(1,1,10,10).

Can you build a histogram using ggplot2?

You can also make histograms by using ggplot2 , “a plotting system for R, based on the grammar of graphics” that was created by Hadley Wickham. This post will focus on making a Histogram With ggplot2.

Which method is used to create a histogram using ggplot2?

Basic histogram with geom_histogram It is relatively straightforward to build a histogram with ggplot2 thanks to the geom_histogram() function. Only one numeric variable is needed in the input.

What does bins do in ggplot2?

To construct a histogram, the data is split into intervals called bins. The intervals may or may not be equal sized. For each bin, the number of data points that fall into it are counted (frequency). The Y axis of the histogram represents the frequency and the X axis represents the variable.


1 Answers

Here is partial solution using geom_text() to add labels and geom_segment() with the arrow option to add arrows.

The drawback is that I had to manually choose y positions for each arrow and label. Maybe someone else can help figure out how programmatically find the histogram bar heights.

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

like image 159
bdemarest Avatar answered Oct 07 '22 18:10

bdemarest