ggplot2中去除背景网格、背景颜色、边框

发布时间 2023-03-22 21:16:40作者: 小鲨鱼2018

 

001、theme_classic() 主题用来去除背景

a、使用默认的背景

type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G')
nums <- c(10,23,8,33,12,40,60)
df <- data.frame(type = type, nums = nums)            ## 测试数据

ggplot(df, aes(type, weight = nums))  + geom_bar()    ## 使用默认的背景

绘图结果如下:

 

b、theme_classic() 主题去除背景

type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G')
nums <- c(10,23,8,33,12,40,60)
df <- data.frame(type = type, nums = nums)              ## 保持测试数据不变

ggplot(df, aes(type, weight = nums))  + geom_bar() +
  theme_classic()                                       ## 使用 theme_classic()去除背景

如下为绘图结果:

 

002、手动去除背景网格

type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G')
nums <- c(10,23,8,33,12,40,60)
df <- data.frame(type = type, nums = nums)           ## 保持测试数据不变

ggplot(df, aes(type, weight = nums))  + geom_bar() +
  theme(panel.grid=element_blank())                  ## 手动去除背景网格

 

003、手动去除背景颜色

type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G')
nums <- c(10,23,8,33,12,40,60)
df <- data.frame(type = type, nums = nums)             ## 保持测试数据不变

ggplot(df, aes(type, weight = nums))  + geom_bar() +
  theme(panel.background = element_blank())            ## 去除背景颜色

绘图结果如下: