library(ggplot2)
library(knitr)
library(grid)
kable(mtcars[1:5,])
| mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
theme(plot.title=element_text())p<-ggplot(mtcars,aes(disp,hp,fill=factor(am)))+geom_bar(stat='identity',width=10,alpha=.9)+theme(legend.position='none')
p1<-p+ggtitle('This is a title')+theme(plot.title=element_text(colour = 'red'))
p2<-p+ggtitle('This is a title')
p3<-p+ggtitle('This is a title')+
theme(plot.title=element_text(family = "Courier"))#无衬线'Helvetica',衬线'Times',等宽'Courier'
p4<-p+ggtitle('This is a title')+
theme(plot.title=element_text(face = 'bold.italic'))#普通'plain'粗体'bold'斜体'italic'
p1<-p+ggtitle('This is a title')+theme(plot.title=element_text(size = 30))
p2<-p+ggtitle('This is a title')+
theme(plot.title=element_text(hjust = .8))#介于0,1之间,调节标题的横向位置
p3<-p+ggtitle('This is a title')+
theme(plot.title=element_text(vjust = 0 ))##介于0,1之间,调节标题的纵向位置,但是没有起作用?
p4<-p+ggtitle('This is a title')+
theme(plot.title=element_text(angle = 180))
#p5<-p+ggtitle("This is title\nsecond title\nthired #title")+
# theme(plot.title=element_text(lineheight = 1.5))#调节行间距
theme(plot.background=element_rectp1<-p+theme(plot.background=element_rect(fill='gold'))
p2<-p+theme(plot.background=element_rect(fill='white',colour='red'))
p3<-p+theme(plot.background=element_rect(fill='white',colour='red',size = 3))
p4<-p+theme(plot.background=element_rect(fill='white',colour='red',size = 3,linetype = 3))
修改网格线
p1<-p+theme(plot.background=element_rect(colour='blue'),panel.background=element_rect(colour='red',fil='white',size = 2))
p2<-p+theme(panel.grid.minor=element_line(colour='white'),panel.background=element_rect(fill='white'))
#p3<-p+theme(plot.background=element_rect(colour='blue'),panel.background=element_rect(colour='red',fil='white',size = 2),panel.border=element_rect(colour='lightblue'))
#调整主网格线
p3<-p+theme(panel.grid.major=element_line(colour='black'))
#调整次要网格线
p4<-p+theme(panel.grid.minor=element_line(colour='gray'),panel.background=element_rect(fill='white'))
修改网格线
p1<-p+theme(axis.title=element_text(colour='red'))
p2<-p+theme(axis.title.x=element_text(colour='red'),axis.title.y=element_text(colour='blue'))
p3<-p+theme(axis.text=element_text(colour='purple'))
p4<-p+theme(axis.text.x=element_text(colour='purple'),axis.text.y=element_text(colour='green'))
p1<-p+theme(axis.ticks.length=unit(.5,"lines"))
p2<-p+theme(axis.ticks.length=unit(-.5,"lines"))
p3<-p+theme(axis.line=element_line(colour='red',linetype = 5,size=2))
#调通axis.line.x(y)之前必须定义axis.line
p4<-p+theme(axis.line=element_line(colour='red',linetype = 2,size=2),axis.line.y=element_line(colour='blue',linetype = 2,size=1))