R语言ggplot2做柱形图并在指定的位置添加灰色背景

2021-11-16 15:34:41 浏览数 (2)

今天的推文介绍一下柱形图实现的代码

image.png

先介绍一个小知识点

ggplot2作图X轴默认坐标轴的刻度是朝下的,Y轴默认的刻度是朝左的,如果要改为朝上和朝右,该如何设置。之前也有人问过这个问题

代码语言:javascript复制
library(ggplot2)
library(ggstar)

ggplot() 
  geom_star(aes(x=1,y=1),
            size=100,
            starshape=16,
            fill="red") 
  theme_bw() 
  theme(axis.ticks.length.x = unit(-1,'cm'),
        plot.margin = unit(c(1,1,2,1),'cm'),
        axis.text.x = element_text(vjust=-20),
        axis.title.x = element_text(vjust=-20),
        axis.ticks.length.y = unit(-1,'cm'),
        axis.text.y = 
          element_text(
            margin = margin(0,1.2,0,0,'cm')
          ))

image.png

这里我们把axis.ticks.length.x = unit(-1,'cm')刻度线的长度调整为负数就可以了,

但是还遇到一个问题是 横坐标的文本和标题可以通过vjust参数上下调节,纵坐标的参数却不能够用hjust的参数左右调节,不知道是什么原因

下面开始重复开头提到的柱形图

首先是数据,用到的是dslabs这个R包

安装直接使用命令install.packages("dslabs")

加载数据集

代码语言:javascript复制
library(dslabs)
data("nyc_regents_scores")

给数据集增加一列

代码语言:javascript复制
library(dplyr)

nyc_regents_scores %>% head()

nyc_regents_scores$total <- rowSums(nyc_regents_scores[,-1], na.rm=TRUE)

对数据集过滤

如果score这一列是缺失值就把这行数据删除

代码语言:javascript复制
nyc_regents_scores %>% 
  filter(!is.na(score)) -> new_df

最基本的柱形图

代码语言:javascript复制
new_df %>%
  ggplot(aes(score, total))   
  geom_bar(stat = "identity", 
           color = "black", 
           fill = "#C4843C") 

image.png

指定位置添加背景

代码语言:javascript复制
new_df %>%
  ggplot(aes(score, total))   
  annotate("rect", xmin = 65, 
           xmax = 99, 
           ymin = 0, 
           ymax = 35000, 
           alpha = .5)  
  geom_bar(stat = "identity", 
           color = "black", 
           fill = "#C4843C") 

image.png

添加文本注释

代码语言:javascript复制
new_df %>%
  ggplot(aes(score, total))   
  annotate("rect", xmin = 65, 
           xmax = 99, 
           ymin = 0, 
           ymax = 35000, 
           alpha = .5)  
  geom_bar(stat = "identity", 
           color = "black", 
           fill = "#C4843C")  
  annotate("text", 
           x = 66, 
           y = 28000, 
           label = "MINIMUMnREGENTS DIPLOMAnSCORE IS 65", 
           hjust = 0, 
           size = 3)  
  annotate("text",
           x = 0, 
           y = 12000, 
           label = "2010 Regents scores onnthe five most common tests",
           hjust = 0, 
           size = 3)

image.png

修改坐标轴刻度和位置

代码语言:javascript复制
new_df %>%
  ggplot(aes(score, total))   
  annotate("rect", xmin = 65, 
           xmax = 99, 
           ymin = 0, 
           ymax = 35000, 
           alpha = .5)  
  geom_bar(stat = "identity", 
           color = "black", 
           fill = "#C4843C")  
  annotate("text", 
           x = 66, 
           y = 28000, 
           label = "MINIMUMnREGENTS DIPLOMAnSCORE IS 65", 
           hjust = 0, 
           size = 3)  
  annotate("text",
           x = 0, 
           y = 12000, 
           label = "2010 Regents scores onnthe five most common tests",
           hjust = 0, 
           size = 3) 
  scale_x_continuous(breaks = seq(5, 95, 5), 
                     limit = c(0,99))   
  scale_y_continuous(position = "right")  
  ggtitle("Scraping By")   
  xlab("")   ylab("Number of tests")

image.png

最后是对主题进行设置

代码语言:javascript复制
new_df %>%
  ggplot(aes(score, total))   
  annotate("rect", xmin = 65, 
           xmax = 99, 
           ymin = 0, 
           ymax = 35000, 
           alpha = .5)  
  geom_bar(stat = "identity", 
           color = "black", 
           fill = "#C4843C")  
  annotate("text", 
           x = 66, 
           y = 28000, 
           label = "MINIMUMnREGENTS DIPLOMAnSCORE IS 65", 
           hjust = 0, 
           size = 3)  
  annotate("text",
           x = 0, 
           y = 12000, 
           label = "2010 Regents scores onnthe five most common tests",
           hjust = 0, 
           size = 3) 
  scale_x_continuous(breaks = seq(5, 95, 5), 
                     limit = c(0,99))   
  scale_y_continuous(position = "right")  
  ggtitle("Scraping By")   
  xlab("")   
  ylab("Number of tests") 
  theme_minimal()   
  theme(panel.grid.major.x = element_blank(), 
        panel.grid.minor.x = element_blank(),
        axis.ticks.length = unit(-0.2, "cm"),
        plot.title = element_text(face = "bold"))

image.png

0 人点赞