利用ggplot2的绘图

We learn this library "ggplot2" cos the ggplot2 can paint out a beautiful picture. 

1. This means in the way that build a plot layer by layer.

2. Generally ggplot ( 'the main part' ) + geom_XXX(mapping, data, ..., geom, position) such as
#basic form "+" geom_XXX(mapping, data, ..., geom, position) + stat_XXX(mapping, data, ..., stat, position)

p = ggplot(dt, aes(x = A, y = B, color = C, group = factor(1))) +  ## the first layer
    geom_point(size = 3.8) +   ## the second layer
    geom_line(size = 0.8) +    ## the third layer
    geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE) 
    ## the fourth layer
p   ## show this picture

① the first layer : ggplot ('the data you wanna to show' , aes () )
② the second layer : geom_point(size = 0.8 ) 
③ the third layer : geom_line(size = 0.8) Control line length ;
④ the fouth layer: #geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE) 

For example
① Draw a picture for China's Money Supply and analysis it.
② The following xlsx is the data we need.

YearM0M1M2
200627072.62126028.05345577.91
200730334.3152519.17403401.3
200834218.96166217.13475166.6
200938246.97221445.81610224.52
201044628.17266621.54725851.79
201150748.46289847.7851590.9
201254659.77308664.23974148.8
201358574.44337291.051106524.98
201458438.53335114.131208605.95

transition

YearMC
200627072.621
200730334.31
200834218.961
200938246.971
201044628.171
201150748.461
201254659.771
201358574.441
201458438.531
2006126028.052
2007152519.172
2008166217.132
2009221445.812
2010266621.542
2011289847.72
2012308664.232
2013337291.052
2014335114.132
2006345577.913
2007403401.33
2008475166.63
2009610224.523
2010725851.793
2011851590.93
2012974148.83
20131106524.983
20141208605.953
z<-read.table(file=file.choose(),sep=",",header = T)   #Get the file and strictly control the beginning.
library(ggplot2)
x <- z$Year
y <- z$M
C <- z$C
p = ggplot(z, aes(x = x, y = y,colour = factor(C),group = factor(C))) + #ZHUYI
    geom_point(size = 2) +
    geom_line(size = 0.6) +
    geom_text(aes(label = M, vjust = -1, hjust = 0.5, angle = 0 ,fill=I("blue")), show_guide = FALSE) + #Add the value of the point  
    scale_colour_discrete(labels = c('M0','M1','M2'))+  #Set legend labels
    guides(color = guide_legend(title='Money Supply',reverse=TRUE)) +
    xlab(" ") + ylab(" ") + ggtitle("China's Money Supply") #Add axis labels
p

 Note: It is easy to cause a mistake, the seemingly flat M0, in fact, its increase is quite large, but it is very flat under the large data of M2, so the vertical axis should be controlled the next time you read the graph or drawing!

3. Advanced R operation

ggplot(msleep, aes(sleep_rem / sleep_total, awake)) + geom_point() + geom_smooth()
#is equivalent to
plot = ggplot(msleep, aes(sleep_rem / sleep_total, awake)) #FIRST LAYER
plot = plot + geom_point() + geom_smooth() #SECOND LAYER
then we can use this as a mould to save space.

bestfit = geom_smooth(method = "lm", 
                      se = T,
                      colour = "steelblue",
                      alpha=0.5,
                      size = 2)

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值