0

You can copy the following code for the example which creates a barplot using ggplot2:

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)
library(ggthemes)

# add all matrices in a list. I use lst here since the ouptut is 
# a named list 


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

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")+
  geom_text(data = df, aes(label = label))

My question is how can I add different lines with labels? On the picture below you can see an example of what I mean: enter image description here

Does anyone have an idea how I can do something like that?

3
  • 1
    Take a look at geom_hline() Commented Feb 17, 2022 at 14:04
  • can you do it?? Commented Feb 17, 2022 at 14:18
  • here for reference the question to which this one here is a follow up stackoverflow.com/questions/71158103/… Commented Feb 17, 2022 at 14:55

1 Answer 1

1

This is much the same as your last question, and the answer much the same as the last answer. Please take time to read and understand what is happening in the code.

Create this data frame:

df2 <- data.frame(name1 = rep(c("attraction", "compromise", "similarity"), 2),
                  name2 = rep(c("1", "2"), each = 3),
                  yintercept  = runif(6, 5, 10),
                  label = c("Now", "use", "whatever", 
                            "label", "you", "like"))

And add this line:

geomtextpath::geom_texthline(data = df2, aes(yintercept = yintercept, label = label),
    color = "red", size = 6, hjust = 0.8, vjust = -0.2, fontface = 2)

enter image description here

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

5 Comments

I personally feel it is a good follow up question and an even better answer. I was just missing the reference to the first question. (i've linked to this now in the comments)
@tjebo it felt easier coming up with a six-word sentence than an 18-word sentence!
haha! well done in both cases. I really need to have a proper look at your package, all those different geoms are simply amazing!
@AllanCameron II tried to download your package. I get: Error: package or namespace load failed for 'geomtextpath' in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]): Namespace 'systemfonts' 0.3.2 is already loaded, but >= 1.0.0 is requested Additionally: warning message: Package 'geomtextpath' was created under R version 4.0.5
@Rologist sounds like you need to update the systemfonts package?

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.