multiple barplots in one fig?

How i can have multiple barplots in one fig (example attached).
Annotation 2020-07-18

Also, i saw code from other forum for multiple barplots, but no idea how to customize it for my data. Code is here:
Annotation 2020-07-17 175856
My data code is here:
library(ggplot2)
library(viridis)
library(hrbrthemes)
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)
names <- c("Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3",
"Valery", "Rio Colorado", "CO99076-6R", "Purple Majesty",
"AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela Russet")
specie <- c(rep("Appearance", 12), rep("Aroma" , 12), rep("Flavor" , 12),
rep("Overall" , 12), rep("Aftertaste", 12), rep("Texture", 12))
condition <- rep(c("Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3",
"Valery", "Rio Colorado", "CO99076-6R", "Purple Majesty",
"AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela Russet") , 6)

Step 1. Put your data in a data.frame.

Hi @sharmachetan ,

l guess that you want one barplot for each pair of HX - name variables, right?

@noeliarico I want one barplot of each HX, so total 6 barplots in one figure and one Y-axis.

Thanks @nirgrahamuk. I tried but it is showing some kind of error-

library(ggplot2)
library(viridis)
#> Warning: package 'viridis' was built under R version 4.0.2
#> Loading required package: viridisLite
library(hrbrthemes)
#> Warning: package 'hrbrthemes' was built under R version 4.0.2
#> NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
#>       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
#>       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library(tidyr)
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),
names <- c("Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3",
           "Valery", "Rio Colorado", "CO99076-6R", "Purple Majesty",
           "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela Russet"),
specie <- c(rep("Appearance", 12), rep("Aroma" , 12), rep("Flavor" , 12),
            rep("Overall" , 12), rep("Aftertaste", 12), rep("Texture", 12)),
condition <- rep(c("Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3",
                   "Valery", "Rio Colorado", "CO99076-6R", "Purple Majesty",
                   "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela Russet") , 6))
df <- df %>% 
  pivot_longer(starts_with("H"))

# plot the bars using facet_
ggplot() +
  geom_col(data = df, mapping = aes(x = value, y = names, fill = names),
           position = "dodge") +
  coord_flip() +
  facet_wrap(vars(name))
#> Error: Aesthetics must be either length 1 or the same as the data (432): y and fill

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

Check the contents of your dataframe. e.g. head (df)

When you built df you used data.frame(H1 <- ... but you have to use data.frame(H1 = ... .

<- does an assignment but does not create a dataframe column. You need to use =

This is just the way R works. Use <- for assignment, = for setting argument values, and == for logical comparison. Other languages have different syntax for these things.

1 Like

Thanks @woodward for the info.
I produced plots close to my wish. Now i have all legend bars in each plot, can i just have one legend based bar in each plot. for example, just appearance based bars in H1 plot, then just aroma based bars in second plot, and flavor, overall, aftertaste and texture. I dont want all bars in all plots.

Here is my code:

library(ggplot2)
library(viridis)
#> Warning: package 'viridis' was built under R version 4.0.2
#> Loading required package: viridisLite
library(hrbrthemes)
#> Warning: package 'hrbrthemes' was built under R version 4.0.2
#> NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
#>       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
#>       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library(tidyr)
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),
names = c("Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3",
           "Valery", "Rio Colorado", "CO99076-6R", "Purple Majesty",
           "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela Russet"),
specie = c(rep("Appearance", 12), rep("Aroma" , 12), rep("Flavor" , 12),
            rep("Overall" , 12), rep("Aftertaste", 12), rep("Texture", 12)),
condition = rep(c("Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3",
                   "Valery", "Rio Colorado", "CO99076-6R", "Purple Majesty",
                   "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela Russet") , 6))
df <- df %>% 
  pivot_longer(starts_with("H"))

# plot the bars using facet_
ggplot() +
  geom_col(data = df, mapping = aes(x = names, y = value, fill = specie), position = "dodge") +
  coord_flip() +
  facet_wrap(vars(name))

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

Yep you just need to swap over specie and name in the code.

I got that, but i am saying how i can get just one type of bars in each plot, for example just appearance bars in H1 plot, then just aroma in second plt?


nameframe <- enframe(unique(df$name))
specieframe <- enframe(unique(df$specie))

(filtframe <- inner_join(nameframe, specieframe, by = "name") %>% mutate(
  filtcont =
    paste0(
      "(name=='", value.x,
      "' & ", "specie=='", value.y, "')"
    )
))

(filtcond <- paste0(filtframe$filtcont, collapse = " | "))

df_filt <- filter(
  df,
  !!rlang::parse_expr(filtcond)
)
ggplot() +
  geom_col(data = df_filt, mapping = aes(x = names, y = value, fill = specie), position = "dodge") +
  coord_flip() +
  facet_wrap(vars(name))
1 Like

Thanks @nirgrahamuk. Another quick question should i install some kind of package for enframe() function?

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

@woodward thanks. Can you please switch names and specie. Like i want names on y axis and specie on x axis. And separate barplot of each specie (appearance, aroma, etc.), with having 12 names (actually these are potato names) on y-axis.Annotation 2020-07-18

Your data is not rectangular, and also I'm not sure exactly what you want to plot, but this is the general procedure.

library(ggplot2)
library(tidyr)

# put your data in a data.frame
# warning - vectors of different lengths get recycled
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),
  names = c(
    "Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3",
    "Valery", "Rio Colorado", "CO99076-6R", "Purple Majesty",
    "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela Russet"
  ),
  specie = c(
    rep("Appearance", 12), rep("Aroma", 12), rep("Flavor", 12),
    rep("Overall", 12), rep("Aftertaste", 12), rep("Texture", 12)
  ),
  condition = rep(c(
    "Russian Banana", "Vermillion", "Atlantic", "POR12PG28-3",
    "Valery", "Rio Colorado", "CO99076-6R", "Purple Majesty",
    "AC99330-1P/Y", "CO05068-1RU", "Masquerade", "Canela Russet"
  ), 6)
)

# combine the H columns into long format
df <- df %>% 
  pivot_longer(starts_with("H"))
  
# plot the bars using facet_
ggplot() +
  geom_col(data = df, mapping = aes(x = specie, y = value, fill = names), position = "dodge") +
  coord_flip() +
  facet_wrap(vars(name))


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

1 Like

I'm confident if you think about the role of specie and names in this short code, you will be able to reverse them yourself

ggplot() +
  geom_col(data = df, 
mapping = aes(x = specie,
              y = value, 
              fill = names), position = "dodge")
1 Like