Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8118029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:21:21+00:00 2026-06-06T04:21:21+00:00

Here is dataset: set.seed(123) myd <- data.frame (class = rep(1:4, each = 100), yvar

  • 0

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))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:21:22+00:00Added an answer on June 6, 2026 at 4:21 am

    Update opts is deprecated; use theme instead.

    Extending bdemarest’s response a little, I think this calculates the bar heights programatically. The last two columns of arrow_pos contain the relevant information: Freq is the height of the bar; xval in the x position of the midpoint of the bar. But still, some of the labels overlap the bars.

    EDIT By default cut bounds its intervals as (b1, b2], whereas it appeas that ggplot2 bounds its intervals in geom_histogram as [b1, b2). I’ve modified the code so that both bound their intervals as [b1, b2), ie the ggplot way.

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my small dataset set.seed(123) X1 <- rep(1:3, each = 4) X2 <-
Here is data: set.seed(123) mat <- matrix(rnorm(5000, 0.5, 0.2), 50) heatmap (mat) mat[mat >
Here is my dataset. set.seed (1234) mydf <- data.frame (Id = c(dis, 1:5), V1.a
Here is my question: #data 1: lab1 <- 1:10 group <- rep(1:3, each =
I've got a data set that contains multiple measurements for each day. I've already
I wan't to create DataSet from code and set it as data source for
Background Here is my issue. Earlier in the course of my program a System.Data.DataSet
Here is my table in the dataset: SELECT tag_work_field.* FROM tag_work_field I created this
I have the following sample dataset (below and/or as CSVs here: http://goo.gl/wK57T ) which
Here is a complete example. I want to forbid using A::set from objects casted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.