eval_tidy(pair$lhs, env = default_env) 中的错误:找不到对象 'Var1'
Error in eval_tidy(pair$lhs, env = default_env) : object 'Var1' not found
我在使用 case_when 在管链内的 ggplot 中自定义标签时遇到问题。
我正在处理带标签的数据,但我制作了这个可重现的数据来显示我的错误。这是我的代码:
#data
padmin1<- data.frame(q0005_0001 = rep(c("Insuficiente1", "Poco Suficiente2","Regular3","Suficiente4","Muy Suficiente5")),5)
#Graphic
padmin1 %>%
rename(Var1=q0005_0001) %>%
ggplot(aes(x = "", y = X5, fill = fct_rev(ordered(Var1)))) +
geom_bar(stat = "identity", width = 0.2) +
geom_text(aes(label = X5), position = position_stack(vjust=0.5), colour= case_when(
Var1 == "Insuficiente1" ~ "white",
Var1 == "Poco Suficiente2" ~ "black",
Var1 == "Regular3" ~ "black",
Var1 == "Suficiente4" ~ "white",
Var1 == "Muy Suficiente5" ~ "white",
TRUE ~ "white"
) , fontface = "bold") +
coord_flip() +
labs(title= "La información brindada por la facultad le resultó...", caption = "Elaborado por SS, 2021") +
#Temas de colores
theme(axis.title = element_blank(),
line = element_blank(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill = "transparent", color = NA),
legend.position = "bottom",
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill = "transparent", linetype = "solid", colour = "transparent"),
legend.box.background = element_rect(fill = "transparent", colour = "transparent"),
axis.text = element_blank()) +
scale_fill_manual("Leyenda", values = c("Insuficiente1"="#8A0000", "Poco Suficiente2"="#FFCD2F", "Regular3"="#DAA600", "Suficiente4"="#144D6C", "Muy Suficiente5"="#071C27"))
在运行此代码后出现以下错误:
#eval_tidy(pair$lhs, env = default_env) 中的错误:找不到对象 'Var1'
让我知道哪里出了问题,我已经尝试将 .$
放在 geom_text()
中的 Var1
之前,但没有用。
要达到您想要的结果,请将 colour=case_when(...
移到 aes()
内并添加 scale_color_identity
。但是,我建议使用 ifelse
+ grepl
而不是使用 case_when,因为只有两个选项,black
和 white
.
此外,要获得标签的正确顺序,您必须添加 group=fct_rev(ordered(Var1))
。否则标签的“堆叠”顺序与条形不同:
#data
padmin1<- data.frame(q0005_0001 = rep(c("Insuficiente1", "Poco Suficiente2","Regular3","Suficiente4","Muy Suficiente5")),5)
library(ggplot2)
library(dplyr)
library(forcats)
#Graphic
padmin1 %>%
rename(Var1=q0005_0001) %>%
ggplot(aes(x = "", y = X5, fill = fct_rev(ordered(Var1)))) +
geom_bar(stat = "identity", width = 0.2) +
geom_text(aes(label = X5,
color = ifelse(grepl("^(P|R)", Var1), "black", "white"),
group = fct_rev(ordered(Var1)) ), position = position_stack(vjust=0.5) , fontface = "bold") +
scale_color_identity() +
coord_flip() +
labs(title= "La información brindada por la facultad le resultó...", caption = "Elaborado por SS, 2021") +
#Temas de colores
theme(axis.title = element_blank(),
line = element_blank(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill = "transparent", color = NA),
legend.position = "bottom",
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill = "transparent", linetype = "solid", colour = "transparent"),
legend.box.background = element_rect(fill = "transparent", colour = "transparent"),
axis.text = element_blank()) +
scale_fill_manual("Leyenda", values = c("Insuficiente1"="#8A0000", "Poco Suficiente2"="#FFCD2F", "Regular3"="#DAA600", "Suficiente4"="#144D6C", "Muy Suficiente5"="#071C27"))
我在使用 case_when 在管链内的 ggplot 中自定义标签时遇到问题。
我正在处理带标签的数据,但我制作了这个可重现的数据来显示我的错误。这是我的代码:
#data
padmin1<- data.frame(q0005_0001 = rep(c("Insuficiente1", "Poco Suficiente2","Regular3","Suficiente4","Muy Suficiente5")),5)
#Graphic
padmin1 %>%
rename(Var1=q0005_0001) %>%
ggplot(aes(x = "", y = X5, fill = fct_rev(ordered(Var1)))) +
geom_bar(stat = "identity", width = 0.2) +
geom_text(aes(label = X5), position = position_stack(vjust=0.5), colour= case_when(
Var1 == "Insuficiente1" ~ "white",
Var1 == "Poco Suficiente2" ~ "black",
Var1 == "Regular3" ~ "black",
Var1 == "Suficiente4" ~ "white",
Var1 == "Muy Suficiente5" ~ "white",
TRUE ~ "white"
) , fontface = "bold") +
coord_flip() +
labs(title= "La información brindada por la facultad le resultó...", caption = "Elaborado por SS, 2021") +
#Temas de colores
theme(axis.title = element_blank(),
line = element_blank(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill = "transparent", color = NA),
legend.position = "bottom",
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill = "transparent", linetype = "solid", colour = "transparent"),
legend.box.background = element_rect(fill = "transparent", colour = "transparent"),
axis.text = element_blank()) +
scale_fill_manual("Leyenda", values = c("Insuficiente1"="#8A0000", "Poco Suficiente2"="#FFCD2F", "Regular3"="#DAA600", "Suficiente4"="#144D6C", "Muy Suficiente5"="#071C27"))
在运行此代码后出现以下错误:
#eval_tidy(pair$lhs, env = default_env) 中的错误:找不到对象 'Var1'
让我知道哪里出了问题,我已经尝试将 .$
放在 geom_text()
中的 Var1
之前,但没有用。
要达到您想要的结果,请将 colour=case_when(...
移到 aes()
内并添加 scale_color_identity
。但是,我建议使用 ifelse
+ grepl
而不是使用 case_when,因为只有两个选项,black
和 white
.
此外,要获得标签的正确顺序,您必须添加 group=fct_rev(ordered(Var1))
。否则标签的“堆叠”顺序与条形不同:
#data
padmin1<- data.frame(q0005_0001 = rep(c("Insuficiente1", "Poco Suficiente2","Regular3","Suficiente4","Muy Suficiente5")),5)
library(ggplot2)
library(dplyr)
library(forcats)
#Graphic
padmin1 %>%
rename(Var1=q0005_0001) %>%
ggplot(aes(x = "", y = X5, fill = fct_rev(ordered(Var1)))) +
geom_bar(stat = "identity", width = 0.2) +
geom_text(aes(label = X5,
color = ifelse(grepl("^(P|R)", Var1), "black", "white"),
group = fct_rev(ordered(Var1)) ), position = position_stack(vjust=0.5) , fontface = "bold") +
scale_color_identity() +
coord_flip() +
labs(title= "La información brindada por la facultad le resultó...", caption = "Elaborado por SS, 2021") +
#Temas de colores
theme(axis.title = element_blank(),
line = element_blank(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill = "transparent", color = NA),
legend.position = "bottom",
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(fill = "transparent", linetype = "solid", colour = "transparent"),
legend.box.background = element_rect(fill = "transparent", colour = "transparent"),
axis.text = element_blank()) +
scale_fill_manual("Leyenda", values = c("Insuficiente1"="#8A0000", "Poco Suficiente2"="#FFCD2F", "Regular3"="#DAA600", "Suficiente4"="#144D6C", "Muy Suficiente5"="#071C27"))