Error Message "! Aesthetics must be either length 1 or the same as the data (456): label "

Viewed 19

I am a newbie to R. I am trying to show two ggplots on one screen using the patchwork package with the following code and I am getting the above error. I have tried resolving the error unsuccessfully, I can resolve the error for "fill" aesthetic only. Kindly assist.

library(tidyverse)
library(ggridges)
library(patchwork)
library(viridis)
library(hrbrthemes)
library(ggplot2)
library(dplyr)
library(ggpubr)
library(gridExtra)
theme_set(theme_bw())
theme_set(theme_pubr())

file <- ("c:/PMS/DHA&PPTabletsClean.csv")
data <- read.csv(file, header=TRUE, stringsAsFactors=FALSE, fileEncoding="latin1")
data <- filter(data, data$ActiveIngredient == "Dihydroartemisinin")


data <- data %>%
  group_by(Manufacturer) %>%
  mutate(OutOfSpec = ifelse(Assayperc < 0.90 | Assayperc > 1.10, FailureReason, ""))%>%




# is_oos <- function (x) {
#   data$Assayperc < 0.90 | data$Assayperc > 1.10
# }
# 
# data <- data %>%
#   mutate(OutOfSpec = ifelse(is_oos(Assay), BatchNumber, "")) %>%
   # view(data)

# library(data.table)
  # setDT(data)
  # 
  # data[, OutOfSpec := fifelse(Assay < 90 | Assay > 110, BatchNumber, ""),
#      by = .(Manufacturer)]
# view(data)

data.frame(data)

p1 <- data %>%
  group_by(ï..Brand) %>%
  mutate(mean_by_Brand = mean(Assay)) %>%
  ungroup() %>%
  mutate(ï..Brand = fct_reorder(ï..Brand, mean_by_Brand)) %>%
  ggplot(aes(ï..Brand, Assay, colour = ï..Brand,
             show.legend = F)) +
  coord_flip() +
  geom_jitter(show.legend = F,
              size = 4,
              alpha = 0.2,
              width = 0.05) +
  stat_summary(fun = mean, geom = "point", size = 8, show.legend = F) + 
  
  geom_hline(aes(yintercept = mean(Assay)),
             colour = "blue",
             size = 0.9) + geom_hline(aes(yintercept = 110),
                                      colour = "red",
                                      size = 0.9)+
  geom_hline(aes(yintercept = 90),
             colour = "red",
             size = 0.9)+
  geom_hline(aes(yintercept = 100),
             colour = "gray70",
             size = 0.9)+
  geom_segment(aes(x = ï..Brand, xend = ï..Brand,
                   y = mean(Assay), yend = mean_by_Brand),
               size = 2, show.legend = F)  +
  
  geom_text(aes(label = data$OutOfSpec), position = position_dodge(0.75),vjust=-0.8,size =5, color ="navy")+

  labs(title = "Assay by Brand",
       x = "Brand",
       y = "% Assay of Dihydroartemisinin") +
  theme(legend.position = "none") +
  theme_bw()

p1

#### Piperaquine

file <- ("c:/PMS/DHA&PPTabletsClean.csv")
data <- read.csv(file, header=TRUE, stringsAsFactors=FALSE, fileEncoding="latin1")
data <- filter(data, data$ActiveIngredient == "Piperaquine Phosphate")

is_oos <- function (x) {
  data$Assayperc < 0.93 | data$Assayperc > 1.07
}


# library(data.table)
# setDT(data)
# 
# data[, OutOfSpec := fifelse(Assay < 90 | Assay > 110, BatchNumber, ""),
#      by = .(Manufacturer)]
# view(data)

data <- data %>%
  mutate(OutOfSpec = ifelse(is_oos(Assayperc), BatchNumber, "")) %>%
  view(data)

# data <- data %>%
#   group_by(ï..Brand) %>%
#   mutate(OutOfSpec = ifelse(is_oos(Assayperc), BatchNumber, "")) %>%
# view(data)

# data <- data %>%
#   group_by(Manufacturer) %>%
#   mutate(meanAssay = mean(Assay)) %>%

data.frame(data)

# rlang::last_trace()

p2 <- data %>%
  group_by(ï..Brand) %>%
  mutate(mean_by_Brand = mean(Assay)) %>%
  ungroup() %>%
  mutate(ï..Brand = fct_reorder(ï..Brand, mean_by_Brand)) %>%
  ggplot(aes(ï..Brand, Assay, colour = ï..Brand,
                         show.legend = F)) +
  coord_flip() +
  geom_jitter(show.legend = F,
              size = 4,
              alpha = 0.2,
              width = 0.05) +
  stat_summary(fun = mean, geom = "point", size = 8, show.legend = F) + 
  
  geom_hline(aes(yintercept = mean(Assay)),
             colour = "blue",
             size = 0.9) + geom_hline(aes(yintercept = 107),
                                      colour = "red",
                                      size = 0.9)+
  geom_hline(aes(yintercept = 93),
             colour = "red",
             size = 0.9)+
  geom_hline(aes(yintercept = 100),
             colour = "gray70",
             size = 0.9)+
  geom_segment(aes(x = ï..Brand, xend = ï..Brand,
                   y = mean(Assay), yend = mean_by_Brand),
               size = 2, show.legend = F)  +
  
  geom_text(aes(label = data$OutOfSpec), position = position_dodge(0.75),vjust=-0.8,size =5, color ="navy")+
  
  labs(title = "Assay by Brand",
       x = "Brand",
       y = "% Assay of Piperaquine Phosphate") +
  theme(legend.position = "none") +
  theme_bw()

p2

p1 + p2

Kindly assist with resolving error message

Regards, Chris

1 Answers

Removing the following code lines from both codes works:

geom_text(aes(label = data$OutOfSpec), position = position_dodge(0.75),vjust=-0.8,size =5, color ="navy")

However, the best way to retain the label is to put the label under the aesthetics of the ggplot.

Regards, Chris

Related