Edit x-axis scale and add text in facet bar plots?

Hello, if someone can please help me edit x-axis scale for my barplots: I want scale range from 0 to 9 and if possible can i replace 9 with text "liking" and also how i can add text onto each bars such as "a", or "ab" to show statistical significance. Please avoid package errors, i have ggplot2, tibble, tidyr, dplyr, etc. Here is full code:

df <- data.frame(
H1 = c(6.36, 3.03, 6.85, 4.07, 4.69, 6.27, 6.67, 3.11, 5.07, 6.14, 5.93, 6.49),
H2 = c(5.15, 5.00, 5.71, 5.50, 4.99, 5.81, 6.05, 5.76, 5.28, 5.69, 5.69, 5.06),
H3 = c(3.85, 5.13, 4.99, 4.91, 5.01, 5.73, 5.77, 5.94, 5.57, 5.35, 6.00, 4.39),
H4 = c(3.84, 4.80, 5.15, 4.85, 4.99, 5.73, 5.77, 5.45, 5.44, 5.41, 5.81, 4.46),
H5 = c(4.08, 5.17, 4.77, 5.03, 5.00, 5.49, 5.49, 5.80, 5.51, 5.18, 5.76, 4.60),
H6 = c(4.35, 5.59, 5.59, 4.83, 5.52, 5.63, 5.85, 5.74, 5.66, 5.19, 5.79, 4.84), fontface = c("bold"),
names = c("RB", "V", "A", "POR12",
                   "Val", "RC", "CO99", "PM",
                   "AC", "CO05068", "Masq", "Canela"),
specie = c(rep("Appearance", 12), rep("Aroma" , 12), rep("Flavor" , 12),
            rep("Overall" , 12), rep("Aftertaste", 12), rep("Texture", 12)),
condition = rep(c("RB", "V", "A", "POR12",
                   "Val", "RC", "CO99", "PM",
                   "AC", "CO05068", "Masq", "Canela") , 6))
df <- df %>% 
  pivot_longer(starts_with("H"))
#> Error in df %>% pivot_longer(starts_with("H")): could not find function "%>%"

#one condition per plot
nameframe <- enframe(unique(df$name))
#> Error in enframe(unique(df$name)): could not find function "enframe"
specieframe <- enframe(unique(df$specie))
#> Error in enframe(unique(df$specie)): could not find function "enframe"
names.labs <- c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")
names(names.labs) <- c("H1", "H2", "H3", "H4", "H5", "H6")


(filtframe <- inner_join(nameframe, specieframe, by = "name") %>% mutate(
  filtcont =
    paste0(
      "(name=='", value.x,
      "' & ", "specie=='", value.y, "')"
    )
))
#> Error in inner_join(nameframe, specieframe, by = "name") %>% mutate(filtcont = paste0("(name=='", : could not find function "%>%"

(filtcond <- paste0(filtframe$filtcont, collapse = " | "))
#> Error in paste0(filtframe$filtcont, collapse = " | "): object 'filtframe' not found

df_filt <- filter(
  df,
  !!rlang::parse_expr(filtcond)
)
#> Error in parse_exprs(x): object 'filtcond' not found
df$value=as.numeric(levels(df$value))[df$value]
#> Error in `$<-.data.frame`(`*tmp*`, value, value = numeric(0)): replacement has 0 rows, data has 72
ggplot() +
  geom_col(data = df_filt, mapping = aes(x = names, y = value, fill = specie), position = "dodge") +
  coord_flip() +
  labs(y = "", x = "") + theme(legend.title = element_blank()) +
  scale_fill_discrete(breaks=c("Appearance","Aroma","Flavor", "Overall", "Aftertaste", "Texture")) +
  scale_x_continuous(limits = c(0,9)) + 
  facet_wrap(vars(name), labeller = labeller(names = names.labs))
#> Error in ggplot(): could not find function "ggplot"

Created on 2020-10-10 by the reprex package (v0.3.0)

Go back with this reprex and pay attention to the discrete vs continuous issue in ggplot

suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
  library(mlr3misc)
  library(tidyr)
})
# prefer dat over df, which is the name of a built-in function
# some functions treat `df` as a closure
dat <- data.frame(
  H1 = c(6.36, 3.03, 6.85, 4.07, 4.69, 6.27, 6.67, 3.11, 5.07, 6.14, 5.93, 6.49),
  H2 = c(5.15, 5.00, 5.71, 5.50, 4.99, 5.81, 6.05, 5.76, 5.28, 5.69, 5.69, 5.06),
  H3 = c(3.85, 5.13, 4.99, 4.91, 5.01, 5.73, 5.77, 5.94, 5.57, 5.35, 6.00, 4.39),
  H4 = c(3.84, 4.80, 5.15, 4.85, 4.99, 5.73, 5.77, 5.45, 5.44, 5.41, 5.81, 4.46),
  H5 = c(4.08, 5.17, 4.77, 5.03, 5.00, 5.49, 5.49, 5.80, 5.51, 5.18, 5.76, 4.60),
  H6 = c(4.35, 5.59, 5.59, 4.83, 5.52, 5.63, 5.85, 5.74, 5.66, 5.19, 5.79, 4.84), fontface = c("bold"),
  names = c(
    "RB", "V", "A", "POR12",
    "Val", "RC", "CO99", "PM",
    "AC", "CO05068", "Masq", "Canela"
  ),
  specie = c(
    rep("Appearance", 12), rep("Aroma", 12), rep("Flavor", 12),
    rep("Overall", 12), rep("Aftertaste", 12), rep("Texture", 12)
  ),
  condition = rep(c(
    "RB", "V", "A", "POR12",
    "Val", "RC", "CO99", "PM",
    "AC", "CO05068", "Masq", "Canela"
  ), 6)
)
dat <- dat %>%
  pivot_longer(starts_with("H"))

# one condition per plot
nameframe <- enframe(unique(dat$name))

specieframe <- enframe(unique(dat$specie))

names.labs <- c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")
names(names.labs) <- c("H1", "H2", "H3", "H4", "H5", "H6")

(filtframe <- inner_join(nameframe, specieframe, by = "name") %>% mutate(
  filtcont =
    paste0(
      "(name=='", value.x,
      "' & ", "specie=='", value.y, "')"
    )
))
#>    name value.x    value.y                            filtcont
#> 1:    1      H1 Appearance (name=='H1' & specie=='Appearance')
#> 2:    2      H2      Aroma      (name=='H2' & specie=='Aroma')
#> 3:    3      H3     Flavor     (name=='H3' & specie=='Flavor')
#> 4:    4      H4    Overall    (name=='H4' & specie=='Overall')
#> 5:    5      H5 Aftertaste (name=='H5' & specie=='Aftertaste')
#> 6:    6      H6    Texture    (name=='H6' & specie=='Texture')

(filtcond <- paste0(filtframe$filtcont, collapse = " | "))
#> [1] "(name=='H1' & specie=='Appearance') | (name=='H2' & specie=='Aroma') | (name=='H3' & specie=='Flavor') | (name=='H4' & specie=='Overall') | (name=='H5' & specie=='Aftertaste') | (name=='H6' & specie=='Texture')"

df_filt <- filter(
  dat,
  !!rlang::parse_expr(filtcond)
)
dat$value <- as.numeric(levels(dat$value))[dat$value]

ggplot() +
  geom_col(data = df_filt, mapping = aes(x = names, y = value, fill = specie), position = "dodge") +
  coord_flip() +
  labs(y = "", x = "") +
  theme(legend.title = element_blank()) +
  #scale_fill_discrete(breaks = c("Appearance", "Aroma", "Flavor", "Overall", "Aftertaste", "Texture")) +
  # scale_x_continuous(limits = c(0, 9)) +
  facet_wrap(vars(name), labeller = labeller(names = names.labs))

Created on 2020-10-09 by the reprex package (v0.3.0.9001)

1 Like

@technocrat I tried to use scale_x_discrete function but it was not working.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.