美文网首页
ggplot2绘制条行图

ggplot2绘制条行图

作者: R语言数据分析指南 | 来源:发表于2020-06-18 22:36 被阅读0次

主要内容:
定义轴标签,图例,主标题名称,按组分面
移除所有标签
自定义字体,颜色,主题
自定义顺序、更改起始刻度

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5
library(ggplot2)
p <- ggplot(ToothGrowth,aes(dose,len,fill=dose)) +
  geom_bar(stat="identity",width = 0.5)+
  ggtitle("Plot of length by dose")+
  theme(plot.title = element_text(hjust = 0.5))+
#设置主标题居中
  xlab("supp")+ylab("len")+labs(fill = "lenge")
Rplot.png
#自定义x,y标题字体,刻度字体,图例字体
p1 <- p + theme(plot.title = element_text(color="red", family = "Times",
                              size=14, face="bold.italic"),
    axis.title.x = element_text(color="blue",family = "Times",
                                size=14, face="bold"),
    axis.title.y = element_text(color="#993333", family = "Times",
                                size=14, face="bold"))+
theme(axis.text.x=element_text(family = "Times",
                                 face = "plain",colour = "black",size=10))+
  theme(axis.text.y=element_text(family = "Times",
                                 face = "plain",colour = "black",size=10))+
theme(legend.text=element_text(face="plain",family = "Times",
                                 colour = "black",size = 10))
p1
Rplot01.png
#移除x,y,标题,图例名称
p2 <- p1 + theme(plot.title = element_blank(),
    axis.title.x = element_blank(),axis.title.y = element_blank())+
  theme(legend.position="top")+
  theme(legend.text=element_text(face="plain",family = "Times",
                                 colour = "black",size = 10))+
  theme(legend.title = element_blank())
p2
Rplot02.png
p3 <-  p2 +scale_x_discrete(limits=c("2", "0.5", "1"))+  #更改柱子顺序
   theme(legend.position='none')+   #移除图例
  scale_fill_brewer(palette="Dark2")+  #使用调色板填充
  scale_color_brewer(palette="Set2")
p3
Rplot03.png
#设置主题
 p + theme_test()+ theme(plot.title = element_text(hjust = 0.5)) 
#设置分面
p + theme_test()+ theme(plot.title = element_text(hjust = 0.5))+
  facet_grid(. ~ supp)
Rplot04.png
#定义X轴文本斜体,并且从0刻度开始
p + theme(axis.text.x = element_text(angle = 30,hjust=1,vjust=1))+
expand_limits(x=0,y=0)+scale_y_continuous(expand=c(0,0))
Rplot05.png

相关文章

网友评论

      本文标题:ggplot2绘制条行图

      本文链接:https://www.haomeiwen.com/subject/fhvpxktx.html