有什么方法可以在使用coord_fixed()?更改形状时删除ggplot2图周围的空白区域?我希望将上方和下方的空白区域裁剪掉,以便仅保留绘图区域和轴标签.我在没有保存的情况下在R markdown文件中渲染绘图输出.
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + coord_fixed(ratio = 1)
Run Code Online (Sandbox Code Playgroud)
下面的代码生成以下图表:
我对 R 和一般编程相当陌生,并且已经在以下几个小时内苦苦挣扎。
我正在尝试创建一个函数,该函数将 df 和列名作为变量,根据提供的列名过滤表并打印输出。
example_function <- function(df=df, col=col){
a <- df[col == 100,]
b <- filter(df, col == 100)
print(a)
print(b)
}
Run Code Online (Sandbox Code Playgroud)
使用example_function(df=example_df, col='percentage')不起作用,两个变量仅返回列名,但不返回数据行(尽管有值== 100)。
使用example_function(df=df, col=percentage),因此百分比在这里没有用引号引起来,我得到:
[.data.frame(df, col == 100,)中的错误:未找到对象“百分比”
然而,当我运行时example_function(df=example_df, col=example_df$percentage),我得到了正确的结果,我的数据帧按预期返回,只有那些等于example_df$percentage100 的行。
我真的希望能够将 df 作为一个变量传递,将列作为另一个变量传递,而不必example_df$percentage每次都键入,因为我希望能够为许多不同的数据帧重复使用该函数,并且键入看起来多余的内容。
基于此,我然后修改了该函数,认为我可以df$col在该函数中使用它,它将评估example_df$percentage并像上面一样工作:
example_function <- function(df=df, col=col){
a <- df[df$col == 100,]
b <- filter(df, df$col == 100)
print(a)
print(b)
}
Run Code Online (Sandbox Code Playgroud)
但现在我在使用example_function(df=example_df, col=percentage)或传递时遇到另一个错误col='percentage':
filter_impl(.data, …