1

This is quite similar to this question but hopefully different enough to merit a new question.

I have up to 5 observations of my value ('hours') across yearly time series data in R. Each observation thus contains a year group (grp_id) and its own unique ID (short_text).

I would like to display the values (hours) of all observations, per year, within a single bar plot in ggplot2 (set to dodge based on the unique ID, short_text) but would also like the unique ID (short_text) of each observations running along the x axis, in addition to the yearly group name (grp_id).

A little like this

plot

Example data:

structure(list(short_text = 1:20, grp_id = c(3, 3, 4, 4, 4, 4, 
4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7), variable = c("hours", 
"hours", "hours", "hours", "hours", "hours", "hours", "hours", 
"hours", "hours", "hours", "hours", "hours", "hours", "hours", 
"hours", "hours", "hours", "hours", "hours"), value = c("1371.28228277413", 
"4117.61650085692", "4462.05401592845", "4563.63839159463", "6617.47698685436", 
"10498.3641321107", "20735.9579131878", "14838.8668295674", "14884.5002478823", 
"15846.5620639966", "20996.7282642214", "73321.8056031507", "29960.4636692291", 
"32475.8922108366", "35534.418796457", "49040.3036856129", "121255.358807715", 
"15288.7191802188", "69888.6583792545", "127734.59190842")), .Names = c("short_text", 
"grp_id", "variable", "value"), row.names = c(NA, 20L), class = "data.frame")

The nearest I so far have is this, but the unique IDs do not show up along the x axis:

ggplot(allBinsTop5.m, aes(grp_id, value)) +   
  geom_bar(aes(fill = short_text), position = "dodge", stat="identity") + 
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
        legend.position="none")
Beeman
  • 55
  • 8
  • 1
    What is it that makes the question different than the one you linked to? Did you try using the faceting trick shown in [my answer there](https://stackoverflow.com/a/36337286/2461552)? – aosmith Jul 02 '18 at 17:18
  • Where is the variable that corresponds with the lower x-axis labels? You mention years but have no such column. I also agree with @aosmith, I can't see anything significantly different from the other post, except that your second grouping variable isn't here – camille Jul 02 '18 at 17:40
  • The years are in represented by ids in the grp_id column, which is shown in parenthesis in the original question – Beeman Jul 02 '18 at 17:42
  • In the original question, their two sets of labels were ID and year. In yours, your two sets of labels are `grp_id` and what? `variable`? If that's the case, you should expand the sample data to include more than just one value of that column—otherwise, we aren't showing you very much – camille Jul 02 '18 at 18:18
  • my grouping vars are grp_id & short_text – I've reworded the q to make it clearer – Beeman Jul 02 '18 at 18:52
  • @aosmith - I saw your faceting trick and the main difference seems to be that my sub group labels are unique whilst your answer deals with repeating sub group labels. – Beeman Jul 02 '18 at 21:52
  • I get error when running your code on my data: Error in vapply(row_axes, is.zero, logical(length(row_axes))) : values must be length 2, but FUN(X[[1]]) result is length 1 – Beeman Jul 02 '18 at 22:05
  • The use of `scales = "free_x"` in that answer allowed groups to have different ID's (you'll note that not all ID's in that example are shared among groups). I didn't have any problems adapting that answer to your dataset. I'd recommend attempting one of the answers in the link you give and then ask a question or edit this one when you run into any issues. – aosmith Jul 02 '18 at 22:07
  • If you are using `facet_wrap` then you might want to use `nrow = 1` to (possibly) [avoid the error](https://stackoverflow.com/questions/50385053/ggplot2-unexpected-vapply-error). [Looks like that bug](https://github.com/tidyverse/ggplot2/issues/2622) was reported and fixed in the development version of ggplot2. – aosmith Jul 02 '18 at 22:16
  • got it (using facet trick in linked answer)– thanks for the help – happy to have this marked a duplicate – Beeman Jul 03 '18 at 08:50

2 Answers2

1

This worked for me:

# Create a new label which is concatenation of group and id
allBinsTop5.m$newlabel<-paste0("G",allBinsTop5.m$grp_id ,"-ID",allBinsTop5.m$short_text)

# Add a geom_text command that specifies new label along with dodge 
ggplot(allBinsTop5.m, aes(grp_id, value)) +   
    geom_bar(aes(fill = newlabel), position = "dodge", stat="identity") + 
    geom_text(aes(label=newlabel),position=position_dodge(width=.9),
              vjust=allBinsTop5.m$value)+
    theme(axis.title.y=element_blank(),
          axis.text.y=element_blank(),
          axis.ticks.y=element_blank(),
          axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
          legend.position="none")
Ben
  • 1,113
  • 10
  • 26
  • This puts the combination of group and ID above each bar, whereas the OP wanted values above each bar and IDs as an x-axis label with the year labels nested below – camille Jul 02 '18 at 17:33
1

It was possible to use the code supplied in this answer to solve my problem.

ggplot(data = aa, aes(x = short_text, y = value, fill = short_text)) + 
  geom_bar(stat = "identity", width = 1) +
  facet_wrap(~grp_id, strip.position = "bottom", scales = "free_x") +
  theme(panel.spacing = unit(0, "lines"), 
        strip.background = element_blank(),
        strip.placement = "outside") + 
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
        legend.position="none")

plot with multiple groups and unique sub-groups labels

Beeman
  • 55
  • 8