【发布时间】:2021-07-16 17:11:57
【问题描述】:
我在创建包含 ggplot 的函数时遇到问题。当我在函数外执行代码时,它会生成我想要的情节,但在函数内它会生成不同的情节......我不确定我做错了什么......
我复制下面带有示例数据库的reprex:
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.0.5
#> Warning: package 'ggplot2' was built under R version 4.0.5
#> Warning: package 'tibble' was built under R version 4.0.5
#> Warning: package 'tidyr' was built under R version 4.0.5
#> Warning: package 'readr' was built under R version 4.0.5
#> Warning: package 'purrr' was built under R version 4.0.5
#> Warning: package 'dplyr' was built under R version 4.0.5
#> Warning: package 'stringr' was built under R version 4.0.5
#> Warning: package 'forcats' was built under R version 4.0.5
library(ggplot2)
# Example data
wb <- data.frame(country = c("Antigua and Barbuda", "Belize", "Costa Rica", "Dominica", "Dominican Republic",
"El Salvador", "Guyana", "Guatemala", "Haiti", "Honduras", "Jamaica", "Nicaragua",
"Panama", "Surinam", "Trinidad and Tobago"),
income = factor(c("high", "low", "upper-middle","upper-middle", "upper-middle",
"lower-middle", "upper-middle", "upper-middle","low", NA, "upper-middle",
"lower-middle", "high", "upper-middle", "high")),
group = c("A", "C", "B","C", "B","B", "B", "C","B", "A", "B","C", "C", "B", "A"),
stringsAsFactors = FALSE)
# I execute the folowing code to create a plot (I do not execute it now because I do not have enough reputation in the forum to do it):
df <- wb %>%
drop_na(income) %>%
group_by(group,income) %>%
tally() %>%
complete(income, fill = list(n = 0)) %>%
mutate(percentage = n / sum(n) * 100)
ggplot(df, aes(income, percentage, fill = group)) +
geom_bar(stat = 'identity', position = 'dodge') +
scale_y_continuous("Title Y", expand = c(0,0))+
scale_x_discrete("Title X")+
scale_fill_manual("Title of legend", values = c("#15607a", "#18a1cd", "cyan")) +
theme_classic(base_size=11) +
theme(axis.text.x = element_text(angle = 0,
hjust = 1, vjust = 0),
axis.line = element_blank(),
axis.ticks.x = element_blank(),
legend.title = element_text(colour = "#00344c", size = 8, face = "bold"))
# Now I try to create a function to automatize the previous plot
create_graph <- function(x,y,t){
df <- wb %>%
drop_na({{y}}) %>%
group_by({{x}},{{y}}) %>%
tally() %>%
complete({{y}}, fill = list(n = 0)) %>%
mutate(percentage = n / sum(n) * 100)
ggplot(df, aes(y, percentage, fill = x)) +
geom_bar(stat = 'identity', position = 'dodge') +
scale_y_continuous("Title Y", expand = c(0,0))+
scale_x_discrete(t)+
scale_fill_manual("Title of legend", values = c("#15607a", "#18a1cd", "cyan")) +
theme_classic(base_size=11) +
theme(axis.text.x = element_text(angle = 0,
hjust = 1, vjust = 0),
axis.line = element_blank(),
axis.ticks.x = element_blank(),
legend.title = element_text(colour = "#00344c", size = 8, face = "bold"))
}
# When I execute the function alone, the resulting plot it's not the same as the plot coded above:
create_graph(x = "income",
y = "group",
t = "Funny title")
由reprex package (v2.0.0) 于 2021-04-22 创建
非常感谢!
【问题讨论】:
标签: r function ggplot2 plot bar-chart