R IN ACTION SELF-TUTORIAL-56 用ggplot分组柱状图的绘制 2020-12-29

image.png
  • 数据的准备:

Group   Sample  Value
Automatic   Sample1 12
Automatic   Sample2 13
Automatic   Sample3 14
Automatic   Sample4 15
Automatic   Sample5 16
Manual  Sample1 17
Manual  Sample2 13
Manual  Sample3 9
Manual  Sample4 5
Manual  Sample5 1

存于excel表格里;

  • 读取数据:

library(readxl)
df<-read_xlsx(file.choose())
df
image.png
  • 转为dataframe:

df1<-as.data.frame(df)
df1
image.png
  • 绘制分组柱状图:

q1<-ggplot(data=df1, mapping=aes(x =  Group, y = Value,fill=Sample))+
    geom_bar(stat="identity",position=position_dodge(0.75))+labs(title = "Goruped Bar")
q1
image.png

最终可以用legend函数调整legend的位置等。

  • 去除灰色背景:

q1<-q1+ theme_bw()
q1
image.png
  • 对标题位置的调整:

q1<-ggplot(data=df1, mapping=aes(x =  Group, y = Value,fill=Sample))+
    geom_bar(stat="identity",position=position_dodge(0.75))+theme_bw()+ggtitle(expression(underline(bold('Goruped Bar '))))+theme(plot.title = element_text(hjust = 0.5))
q1
image.png

你可能感兴趣的:(R IN ACTION SELF-TUTORIAL-56 用ggplot分组柱状图的绘制 2020-12-29)