GGPlot2 预设 - 创建具有某些 ggplot2 美学选项的函数
GGPlot2 Preset - Creating a function with certain ggplot2 aesthetics options
我现在正在创建很多情节。由于它们都针对相同的演示文稿,因此在我选择的外观选项方面它们有很多共同点。
df1 <- data.frame(name = c("name1","name2","name3","name4"),
variable = c("var1","var1","var2","var2"),
value = c(15,16,17,18))
df1 %>%
ggplot(aes(x = name, y = value, fill = variable)) +
geom_bar(stat = "identity", position = position_stack()) +
labs(title = "Plot Title",
subtitle = "month 1",
x="",
y="Count") +
scale_fill_viridis_d(name = "", option = "inferno", begin = 0.3, end = 0.7, direction = -1) +
scale_shape_tableau() +
theme_economist() +
theme(plot.background = element_rect(fill = "white"),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
plot.margin = unit(c(1,1,1,1), "cm"))
本着减少代码行数的思路,有没有办法创建一个带有其中一些选项的函数,例如“theme”、“theme_economist”、“scale_shape", "scale_fill"?然后我可以为每个情节指定美学和实验室,其余的只是一个“+预设功能”,添加主题、颜色和形状。
您可以将要添加到绘图中的内容存储在列表中。然后,您可以通过将其添加到绘图中来重新使用此列表。
library(ggplot2)
library(ggthemes)
library(magrittr)
common_options <- list(
scale_fill_viridis_d(name = "", option = "inferno", begin = 0.3, end = 0.7, direction = -1),
scale_shape_tableau(),
theme_economist(),
theme(plot.background = element_rect(fill = "white"),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
plot.margin = unit(c(1,1,1,1), "cm"))
)
df1 <- data.frame(name = c("name1","name2","name3","name4"),
variable = c("var1","var1","var2","var2"),
value = c(15,16,17,18))
df1 %>%
ggplot(aes(x = name, y = value, fill = variable)) +
geom_bar(stat = "identity", position = position_stack()) +
labs(title = "Plot Title",
subtitle = "month 1",
x="",
y="Count") +
common_options
由 reprex package (v1.0.0)
于 2021-07-20 创建
我现在正在创建很多情节。由于它们都针对相同的演示文稿,因此在我选择的外观选项方面它们有很多共同点。
df1 <- data.frame(name = c("name1","name2","name3","name4"),
variable = c("var1","var1","var2","var2"),
value = c(15,16,17,18))
df1 %>%
ggplot(aes(x = name, y = value, fill = variable)) +
geom_bar(stat = "identity", position = position_stack()) +
labs(title = "Plot Title",
subtitle = "month 1",
x="",
y="Count") +
scale_fill_viridis_d(name = "", option = "inferno", begin = 0.3, end = 0.7, direction = -1) +
scale_shape_tableau() +
theme_economist() +
theme(plot.background = element_rect(fill = "white"),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
plot.margin = unit(c(1,1,1,1), "cm"))
本着减少代码行数的思路,有没有办法创建一个带有其中一些选项的函数,例如“theme”、“theme_economist”、“scale_shape", "scale_fill"?然后我可以为每个情节指定美学和实验室,其余的只是一个“+预设功能”,添加主题、颜色和形状。
您可以将要添加到绘图中的内容存储在列表中。然后,您可以通过将其添加到绘图中来重新使用此列表。
library(ggplot2)
library(ggthemes)
library(magrittr)
common_options <- list(
scale_fill_viridis_d(name = "", option = "inferno", begin = 0.3, end = 0.7, direction = -1),
scale_shape_tableau(),
theme_economist(),
theme(plot.background = element_rect(fill = "white"),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
plot.margin = unit(c(1,1,1,1), "cm"))
)
df1 <- data.frame(name = c("name1","name2","name3","name4"),
variable = c("var1","var1","var2","var2"),
value = c(15,16,17,18))
df1 %>%
ggplot(aes(x = name, y = value, fill = variable)) +
geom_bar(stat = "identity", position = position_stack()) +
labs(title = "Plot Title",
subtitle = "month 1",
x="",
y="Count") +
common_options
由 reprex package (v1.0.0)
于 2021-07-20 创建