首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误信息"!美学必须是长度1或与数据(456):标签相同。“

错误信息"!美学必须是长度1或与数据(456):标签相同。“
EN

Stack Overflow用户
提问于 2022-09-19 22:47:39
回答 1查看 27关注 0票数 0

我是R.的新手,我试图用下面的代码在一个屏幕上显示两个ggplots,并得到上面的错误。我试图解决错误失败,我可以解决错误的“填充”美学。请帮忙。

代码语言:javascript
复制
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

请协助解决错误信息。

你好,克里斯

EN

回答 1

Stack Overflow用户

发布于 2022-09-20 09:13:53

从这两个代码中删除以下代码行是有效的:

Geom_text(标签=数据$OutOfSpec),位置= position_dodge(0.75),size =-0.8,大小=5,颜色=“海军”)

然而,保留标签的最好方法是将标签置于ggplot的美学之下。

你好,克里斯

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73779976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档