ggplot Aesthetics length

I tried to plot a barchart with 3 groups (Tentative, Certain, Ratio) of data.
Assumption: 14 colors are too many for the standard ggplot to "come up with itself"

Question1: How do i get the plot working?
(optional) Question2: How do i get the bars for each group to sort ascending?

The Errors are:

  1. Error: Aesthetics must be either length 1 or the same as the data (14): y
  2. Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?

my data.frame (14 obs, 4 Variables, 3 Numeric, 1 Character)
tibble::tribble(
~Nachname, ~Tentative, ~Certain, ~Ratio,
"abc", 195, 516, 321,
"def", 140, 483, 343,
"dgh", 92, 512, 420,
"dfh", 56, 262, 206,
"zit", 93, 368, 275,
"fxg", 67, 464, 397,
"awr", 143, 582, 439,
"nvg", 98, 448, 350,
"uipi", 209, 666, 457,
"lzu", 155, 487, 332,
"awe", 115, 398, 283,
"sdg", 222, 670, 448,
"cvb", 124, 539, 415,
"mjk", 158, 406, 248
my (error) code

nb.cols = 14
mycolors = colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)
> ggplot(liwc_Nachname, aes(x = liwc_Nachname$Nachname, y = c(liwc_Nachname$Certain, liwc_Nachname$Tentative, liwc_Nachname$Ratio))) 
Error: Aesthetics must be either length 1 or the same as the data (14): y
>          geom_col(position = position_dodge()) + 
+          scale_fill_manual(values = mycolors)
Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?

Appreciative of any help. Thanks :slight_smile:

Hi @object941. The y axis value is 3 times the x axis value which is not allowed. From your code, I guess you want to plot column with different color for each column in the data frame. So, gather columns' value and fill according to the column names.

library(tidyverse)
library(RColorBrewer)

liwc_Nachname <- tibble::tribble(
  ~Nachname, ~Tentative, ~Certain, ~Ratio,
  "abc", 195, 516, 321,
  "def", 140, 483, 343,
  "dgh", 92, 512, 420,
  "dfh", 56, 262, 206,
  "zit", 93, 368, 275,
  "fxg", 67, 464, 397,
  "awr", 143, 582, 439,
  "nvg", 98, 448, 350,
  "uipi", 209, 666, 457,
  "lzu", 155, 487, 332,
  "awe", 115, 398, 283,
  "sdg", 222, 670, 448,
  "cvb", 124, 539, 415,
  "mjk", 158, 406, 248)

nb.cols = 14
mycolors = colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)

liwc_Nachname %>%
  gather(group, value, -Nachname) %>%
  ggplot() +
  geom_col(aes(Nachname, value, fill = group), position = "dodge") +
  scale_fill_manual(values = mycolors)

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

1 Like

Hi @object941,

I'll try my hand at Question 2: Notice that in the graph @raytong posted, the 'Tentative' bar for cvb is shorter than the 'Tentative' bar for mjk, but the reverse is true for their 'Certain' bar, so it won't be possible to order the bar in each group at the same.

However, if you wanted to order just by 'Tentative', you could rearrange the Nachnames by inserting a mutate() command just after liwc_Nachname %>% in @raytong's last chunk of code:

liwc_Nachname %>%
  # convert Nachname to factor, with levels according to 'Tentative' values
  mutate(Nachname = reorder(Nachname, Tentative)) %>% 
  gather(group, value, -Nachname) %>%
  ggplot() +
  geom_col(aes(Nachname, value, fill = group), position = "dodge") +
  scale_fill_manual(values = mycolors)

Hello again. Thanks for the quick reply and your help. Both of you were very precise in your replies and even I could follow. Here is the result sorted by "Ratio"
Also: one requires "tidyR" package for the "gather" function in the code

You're welcome, and just to follow up on gather(): The tidyr package is a subpackage of the tidyverse package, which also includes ggplot2 as a subpackage, so if you'd like everything at once, you can use library(tidyverse) to load them all. Also, gather() is older version of the more current pivot_longer() command, so I thought I'd include a version of @raytong's code that uses it, instead, too:

library(tidyverse)
library(RColorBrewer)

liwc_Nachname <- tibble::tribble(
  ~Nachname, ~Tentative, ~Certain, ~Ratio,
  "abc", 195, 516, 321,
  "def", 140, 483, 343,
  "dgh", 92, 512, 420,
  "dfh", 56, 262, 206,
  "zit", 93, 368, 275,
  "fxg", 67, 464, 397,
  "awr", 143, 582, 439,
  "nvg", 98, 448, 350,
  "uipi", 209, 666, 457,
  "lzu", 155, 487, 332,
  "awe", 115, 398, 283,
  "sdg", 222, 670, 448,
  "cvb", 124, 539, 415,
  "mjk", 158, 406, 248)

nb.cols = 14
mycolors = colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)

liwc_Nachname %>%
  # convert Nachname to factor, with levels according to 'Ratio' values
  mutate(Nachname = reorder(Nachname, Ratio)) %>% 
  # reshape table by placing the column names 'Tentative', 'Certain, and 
  # 'Ratio' in their own column, called 'numeric_vars', and place the 
  # corresponding values in a new column, called 'numeric_values'
  pivot_longer(
    cols = -Nachname, # collect column names of all but Nachname
    names_to = 'numeric_columns', # where to place them
    values_to = 'numeric_values', # for corresponding values
  )

# more abbreviated version that uses defaults
liwc_Nachname %>%
  mutate(Nachname = reorder(Nachname, Ratio)) %>% 
  pivot_longer(-Nachname) # place names in 'name', their values in 'value'

# plot
liwc_Nachname %>%
  mutate(Nachname = reorder(Nachname, Ratio)) %>% 
  pivot_longer(-1) %>%  # exclude 'Nachname' by column index
  ggplot() +
  geom_col(aes(Nachname, value, fill = name), position = "dodge") +
  scale_fill_manual(values = mycolors)

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