R语言中ggplot2绘制柱状图

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

 

001、 基础绘图

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)            ## 生成的测试数据框
df
ggplot(df, aes(type, weight = nums))  + geom_bar()    ## 使用ggplot绘图

绘图结果如下:

 

 

002、给柱状图添加颜色

ggplot(df, aes(type, weight = nums, fill = type))  + geom_bar()   ## 利用fill选项添加颜色

 

003、修改绘图输出的顺序

a、只修改原始数据顺序

type <- c('F', 'B', 'G', 'D', 'E', 'A', 'C')
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、使修改原始数据后,绘图的输出顺序保持不变:

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

df$type <- factor(df$type, level = df$type)    
ggplot(df, aes(type, weight = nums))  + geom_bar()

 

 

004、手动指定输出柱状图的颜色

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)
col <- c("red", "blue", "green", "cyan", "purple", "black",     ## 此处设定颜色
         "magenta")
ggplot(df, aes(type, weight = nums))  + geom_bar(fill = col)    ## 绘制柱状图

绘图结果如下,可以看到颜色输出已经按照指定的颜色输出了:

 

005、根据判断语句设定颜色:

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(fill = ifelse(df$nums > 20,'red','blue'))   ## 利用判断语句输出颜色

绘图结果如下:

 

006、设置柱状图绘图的宽度

a、使用width参数设置为:0.3

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(width = 0.3)                        ## 设置宽度width = 0.3

 

 

b、使用width参数设定为0.8:

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(width = 0.8)                             ## 使用width参数设定宽度为0.8

 

007、手动指定x轴、y轴的名称

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() + labs(x = 'x-name', y = 'y-name')      ## 利用labs选项指定x轴、y轴的名称

 

 

008、手动修改x轴、y轴坐标轴名称的大小

a、设置坐标轴的大小:size = 10

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(axis.title =  element_text(size=10,face = "bold"))  ## 利用该句修改坐标轴名称的大小

绘图结果如下:

 

 

 

b、设置坐标轴的大小:size = 30

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(axis.title =  element_text(size=30,face = "bold"))   ## 设置绘图的大小为30

绘图的结果如下:

 

009、手动调整坐标轴上刻度标签的大小:

a、设施x轴刻度标签的大小,size = 10

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(axis.text.x =   element_text(size=10))       ## 此处调整坐标轴刻度标签名称的大小

 

 

b、设施x轴刻度标签的大小,size = 30

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(axis.text.x =   element_text(size=30))         ## 设置x轴刻度标签 seze = 30

绘图结果如下:

 

010、手动旋转坐标轴文字

a、x轴刻度标签旋转45度。

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(axis.text.x =   element_text(angle=45))   ## x轴旋转45度

 

 

 

b、x轴刻度标签旋转90度。

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(axis.text.x =   element_text(angle=90))  

 

011、手动调整坐标轴刻度标签的位置

a、在x轴上面, 设置hjust为0.5

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(axis.text.x =   element_text( hjust = 0.5))     ## 此处hjust设置为0.5

结果如下图:

 

 

 

b、在x轴上面, 设置hjust为3.5

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(axis.text.x =   element_text( hjust = 3.5))      ## hjust设置为3.5

绘图结果如下:

 

 

c、在x轴上面, 设置hjust为-3.5

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(axis.text.x =   element_text( hjust = -3.5))       ## hjust设置为-3.5

绘图结果如下:

 

012、手动设置页边距

a、页边距设置为1

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(axis.text.x =   element_text( hjust = -3.5),
        plot.margin=unit(rep(1,4),'lines'))               ## 设置页边距为1

 

 

b、页边距设置为4

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(axis.text.x =   element_text( hjust = -3.5),
        plot.margin=unit(rep(4,4),'lines'))                 ## 设置页边距为4

绘图结果如下: