使用 R 中的 ggplot2 创建没有背景面板的图表。
r programmingserver side programmingprogramming
要创建没有背景面板的图表,我们可以使用 ggplot2 包的主题函数,其中我们可以将 panel.background 参数设置为空白。
例如,如果我们有一个名为 df 的数据框,其中包含两列,即 x 和 y,那么我们可以使用以下命令 − 使用 ggplot2 在 x 和 y 之间创建没有背景面板的散点图。
ggplot(df,aes(x,y))+geom_point()+theme(panel.background=element_blank())
示例
考虑下面的数据框 −
x<-rpois(20,5) y<-rpois(20,2) df<-data.frame(x,y) df
创建以下数据框
x y 1 7 0 2 4 1 3 4 3 4 2 4 5 7 0 6 6 1 7 6 1 8 6 4 9 6 0 10 4 3 11 2 2 12 6 2 13 4 1 14 4 3 15 4 2 16 2 3 17 5 0 18 5 3 19 5 5 20 6 3
要加载 ggplot2 包并在上面创建的数据框上创建 x 和 y 之间的散点图,请将以下代码添加到上面的代码片段 −
x<-rpois(20,5) y<-rpois(20,2) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()
输出
如果将上述所有代码片段作为单个程序执行,则会生成以下输出 −
要在上述数据框上创建不带背景面板的 x 和 y 之间的散点图,请将以下代码添加到上述代码片段 −
x<-rpois(20,5) y<-rpois(20,2) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()+theme(panel.background=element_blank())
输出
如果将上述所有代码片段作为单个程序执行,则会生成以下输出 −