如何使用ggplot2在R中制作具有透明背景的图形?
在本文中,我们将讨论如何使用 R 编程语言创建具有透明背景的图形。
所需的任务将通过使用带有适当参数的 theme()函数来实现。 theme()函数用于修改主题设置。要实际可视化透明背景,图像必须存储为 PNG 图像,这可以通过 ggsave()函数。
Syntax: ggsave(plot, filename, bg)
Parameter:
- plot: plot to be saved
- filename: path of the file
- bg: background
创建一个简单的绘图进行演示:
R
library(ggplot2)
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
plt<-ggplot(df,aes(x,values,col=fun))+geom_line()
ggsave(plt, filename = "output1.png")
R
library(ggplot2)
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
plt<-ggplot(df,aes(x,values,col=fun))+geom_line()+
theme(rect = element_rect(fill = "transparent"))
ggsave(plt, filename = "output.png", bg = "transparent")
R
library(ggplot2)
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
plt<-ggplot(df,aes(x,values,col=fun))+geom_line()+
theme(legend.background = element_rect(fill = "transparent"),
legend.box.background = element_rect(fill = "transparent"),
panel.background = element_rect(fill = "transparent"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "transparent",
color = NA))
ggsave(plt, filename = "output1.png", bg = "transparent")
输出:
方法 1:使用 rect 和 theme()
在这种方法中,在正常创建绘图后,将带有 rect 参数的 theme()函数添加到其中。为了 rect,element_rect()函数通过参数 fill 设置为透明来传递。
Syntax: theme(rect = element_rect(fill=”transparent”))
示例:具有透明背景的绘图
电阻
library(ggplot2)
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
plt<-ggplot(df,aes(x,values,col=fun))+geom_line()+
theme(rect = element_rect(fill = "transparent"))
ggsave(plt, filename = "output.png", bg = "transparent")
输出:
方法二:分别设置每个主题参数
通过设置,可以使绘图背景透明的背景元素变得透明。
示例:具有透明背景的绘图
电阻
library(ggplot2)
function1<- function(x){x**2}
function2<-function(x){x**3}
function3<-function(x){x/2}
function4<-function(x){2*(x**3)+(x**2)-(x/2)}
df=data.frame(x=-2:2,
values=c(function1(-2:2),
function2(-2:2),
function3(-2:2),
function4(-2:2)),
fun=rep(c("function1","function2",
"function3","function4"))
)
plt<-ggplot(df,aes(x,values,col=fun))+geom_line()+
theme(legend.background = element_rect(fill = "transparent"),
legend.box.background = element_rect(fill = "transparent"),
panel.background = element_rect(fill = "transparent"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "transparent",
color = NA))
ggsave(plt, filename = "output1.png", bg = "transparent")
输出: