[英]Error Message "! Aesthetics must be either length 1 or the same as the data (456): label "
I am a newbie to R.我是 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.
我正在尝试使用拼凑而成的 package 和以下代码在一个屏幕上显示两个 ggplots,但出现上述错误。 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问候,克里斯
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") 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.但是,保留label的最好方法是将label置于ggplot的美学之下。
Regards, Chris问候,克里斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.