简体   繁体   English

错误消息“:美学必须是长度1或与数据相同(456):label”

[英]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.

相关问题 错误:美学长度必须为 1 或与数据 (2190) 相同:x - Error: Aesthetics must be either length 1 or the same as the data (2190): x ggplot2:Aesthetics中的几个图表必须是长度为1或相同的长度 - Several graphs in ggplot2: Aesthetics must either be length one, or the same length 绘图误差向量的长度必须相同 - Plottng Error Vector Must Be Same Length 错误:Alpha 必须为 1 或长度为 x 之一。(在我的代码中使用 renderPlotly) - Error: Alpha must be 1 or either of length x.(Using renderPlotly in my code) MatPlotLib RTE xdata和ydata的长度必须相同 - MatPlotLib RTE xdata and ydata must be same length Neo4j Dgs.fastrp.write 返回的UnionProperties 必须都具有相同的类型,但是发现[LONG, DOUBLE, DOUBLE, DOUBLE]”错误 - Neo4j Dgs.fastrp.write returnsUnionProperties must all have the same type, but found [LONG, DOUBLE, DOUBLE, DOUBLE]" error 在Excel中将折线图拉伸到相同长度 - stretch line chart to same length in excel 为什么我的模型预测相同的标签? - Why does my model predict the same label? 如果 = #N/A,Excel 图表隐藏数据标签 - Excel graph hide data label if = #N/A 将图划分为具有必须在同一子图中的一组顶点的连通子图 - Partitioning a graph into connected subgraphs with sets of vertices that must be in the same subgraph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM