小编Gus*_*fox的帖子

在函数 -ggplot 中将参数传递给构面网格

我正在尝试编写一个函数来在网格中绘制图形。我正在使用 ggplot 和构面网格。我无法传递有关方面网格的论点。我想知道是否有人能指出我正确的方向。

数据示例:

 Year = as.factor(rep(c("01", "02"), each = 4, times = 1))
 Group = as.factor(rep(c("G1", "G2"), each = 2, times = 2))
 Gender = as.factor(rep(c("Male", "Female"),   times = 4))
 Percentage = as.integer(c("80","20","50","50","45","55","15","85"))
 df1 = data.frame (Year, Group, Gender, Percentage)
Run Code Online (Sandbox Code Playgroud)

不带函数的网格图的代码是:

p = ggplot(data=df1, aes(x=Year, y=Percentage, fill = Gender)) + geom_bar(stat = "identity")
p = p +  facet_grid(~ Group, scales = 'free')  
p
Run Code Online (Sandbox Code Playgroud)

这会产生像我想做的那样的情节。但是,当我将其放入函数中时:

MyGridPlot <- function (df, x_axis, y_axis, bar_fill, fgrid){
p = ggplot(data=df1, aes(x=x_axis, y=y_axis, fill = …
Run Code Online (Sandbox Code Playgroud)

r function facet ggplot2 facet-grid

5
推荐指数
1
解决办法
1488
查看次数

标签 统计

facet ×1

facet-grid ×1

function ×1

ggplot2 ×1

r ×1