0

suppose I have the following barplot (the barplot can be created with the code)

set.seed(999)

similarity_context_set1 = matrix(rnorm(10*3,10,1),ncol=3)
similarity_context_set2 = matrix(rnorm(10*3,10,1),ncol=3)

attraction_prop_context_set1 = matrix(rnorm(10*3,10,1),ncol=3)
attraction_prop_context_set2 = matrix(rnorm(10*3,10,1),ncol=3)

compromise_context_set1 = matrix(rnorm(10*3,10,1),ncol=3)
compromise_context_set2 = matrix(rnorm(10*3,10,1),ncol=3)

library(tidyverse)

# add all matrices in a list. I use lst here since the ouptut is 
# a named list 
lst(similarity_context_set1,
    similarity_context_set2,
    attraction_prop_context_set2,
    attraction_prop_context_set1,
    compromise_context_set1,
    compromise_context_set2) %>% 
  # transform to tibble and add column names
  map(as_tibble) %>% 
  map(set_names, c("X", "Y","Third")) %>% 
  # bind the list to one dataframe, add list names to column by 
  # setting .id
  bind_rows(.id = "name") %>% 
  # transform data from wide to long as it is recommended ggplot 
  #input format here
pivot_longer(-1,names_to = "x", values_to = "y") %>% 
  # make to columns for facetting
  separate(name, into = c("name1", "name2"), sep = "_", extra = "merge") %>% 
  mutate(name2 = str_extract(name2, "[0-9]")) %>% 
  # finally the plot
  ggplot(aes(x, y, group=x, fill = x)) + theme_hc(base_size = 13)+ 
  geom_bar(stat = "summary", fun = "mean",alpha=0.8 )+
  scale_fill_manual(values = c("Y" = "gray1","X" = "gray1","Third" = "gray1"), guide="none" )+
  facet_grid(name2~name1)+ 
  stat_summary(fun.data = mean_se, geom = "errorbar", width=0.2)+
  ggtitle("Perceptual Domain")+
  theme(plot.title = element_text(hjust = 0.5))+
  labs(x = "Response", y = "Mean Choice Proportion")

How can I add an individual text above each bar? I don't want to write the average values above the bars, but a text like on the picture (Target, Competitor and Third) Benter image description hereild?

Do you know how I can additionally add different lines for each plot with labels? like this: enter image description here

6
  • How do you decide which labels you want above which bars? The labels are not present in your data structure, nor is it obvious how you are arriving at your choice for which label goes where in each panel. Commented Feb 17, 2022 at 12:38
  • Yes, that is the point. I want to insert the text individually without it being present in the data structure. if this is not possible how can i include the labels in the data structure so that i can then display it in the barplot Commented Feb 17, 2022 at 12:43
  • you can add multiple annotations, or simply create a little data frame for your labels, as I show in my answer below. Commented Feb 17, 2022 at 12:56
  • Thanks a lot! Do you know how I can additionally add different lines for each plot with labels? like this in the picture above Commented Feb 17, 2022 at 14:35
  • For a new question, please kindly ask a follow up question - please also don't forget to link to this thread here and to include the code used for this example, in this case, Allans code. Thanks Commented Feb 17, 2022 at 14:44

1 Answer 1

3

If you have this many labels to insert and want complete control over the order, then the easiest way is probably to create a little label data frame:

df <- data.frame(name1 = rep(rep(c("attraction", "compromise", "similarity"), each = 3), 2),
                 name2 = rep(c("1", "2"), each = 9),
                 x     = rep(c("Third", "X", "Y"), 6),
                 y     = rep(12, 18),
                 label = c("Now", "you", "can", "use", "any", "label", "you", "want",
                           "by", "inserting", "it", "as", "a", "string", "into", "this", 
                           "character", "vector"))

Then if you add this line anywhere in your plot code:

geom_text(data = df, aes(label = label))

You will get:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Allan, may I ask if you have used an automated way of creating that character vector for the labels or have you manually added all those quotation marks???
also you get my upvote just for coming up with this meaningful sentence of exactly 18 words.
I'm afraid to say this was manual @tjebo, though I could have used dput and strsplit I suppose!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.