美文网首页
ggplot2学习(4)

ggplot2学习(4)

作者: BioLearner | 来源:发表于2019-11-20 13:18 被阅读0次
    > library(gcookbook)   #for the data set
    > pg_mean
      group weight
    1  ctrl  5.032
    2  trt1  4.661
    3  trt2  5.526
    > ggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat = "identity")
    # geom_bar() 默认的stat为“bin”, stat=bin则前面aes()中不需要y,会统计x中每一种元素的数目
    #stat = "identity"则统计x中每一种元素对应y的数据
    
    ggplot(pg_mean, aes(x=group)) + geom_bar()  
    # geom_bar() 等同于 geom_bar(stat=“bin”)
    
    >BOD   #一个生物需氧量的数据集
      Time demand
    1    1    8.3
    2    2   10.3
    3    3   19.0
    4    4   16.0
    5    5   15.6
    6    7   19.8      #注意Time中没有6
    >ggplot(BOD, aes(x=Time, y=demand)) + geom_bar(stat = "identity")
    
    ggplot(BOD, aes(x=factor(Time), y=demand)) + geom_bar(stat = "identity")
    
    ggplot(pg_mean, aes(x=group,y=weight)) + 
      geom_bar(stat = "identity", fill = "lightblue", colour = "black")
    
    > #Grouping bars together
    > cabbage_exp
      Cultivar Date Weight        sd  n         se
    1      c39  d16   3.18 0.9566144 10 0.30250803
    2      c39  d20   2.80 0.2788867 10 0.08819171
    3      c39  d21   2.74 0.9834181 10 0.31098410
    4      c52  d16   2.26 0.4452215 10 0.14079141
    5      c52  d20   3.11 0.7908505 10 0.25008887
    6      c52  d21   1.47 0.2110819 10 0.06674995
    > ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
       geom_bar(position = "dodge")
    Error: stat_count() must not be used with a y aesthetic.
    # 上述报错是因为,在geom_bar()中没有修改stat,默认为"bin"
    # stat为"bin"时,”用于统计x中个元素的数目,不应该指定y
    > ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
       geom_bar(position = "dodge", stat = "identity")
    
    ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
      geom_bar(position = "stack", stat = "identity")
    
    ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
      geom_bar(position = "fill", stat = "identity")
    
    #Making a Bar Graph of Counts
    ggplot(diamonds, aes(x=cut)) + geom_bar()  # geom_bar() 等同于 geom_bar(stat=“bin”)
    
    > #Using colors in a Bar Graph
    > str(uspopchange)
    'data.frame':  50 obs. of  4 variables:
     $ State : chr  "Alabama" "Alaska" "Arizona" "Arkansas" ...
     $ Abb   : chr  "AL" "AK" "AZ" "AR" ...
     $ Region: Factor w/ 4 levels "Northeast","South",..: 2 4 4 2 4 4 1 2 2 2 ...
     $ Change: num  7.5 13.3 24.6 9.1 10 16.9 4.9 14.6 17.6 18.3 ...
    > upc <- subset(uspopchange, rank(Change)>40)   #表示对change排序后,取第40之后的数据
    > upc   #第一列为该行在原本数据集uspopchange中的行数​
                State Abb Region Change
    3         Arizona  AZ   West   24.6
    6        Colorado  CO   West   16.9
    10        Florida  FL  South   17.6
    11        Georgia  GA  South   18.3
    13          Idaho  ID   West   21.1
    29         Nevada  NV   West   35.1
    34 North Carolina  NC  South   18.5
    41 South Carolina  SC  South   15.3
    44          Texas  TX  South   20.6
    45           Utah  UT   West   23.8​      
    > ggplot(upc, aes(x=Abb, y=Change, fill=Region)) + geom_bar(stat = "identity") 
    
    ggplot(upc, aes(x=reorder(Abb, Change), y=Change, fill=Region)) + 
      geom_bar(stat = "identity", color = "black") 
    #reorder(Abb, Change)排序,前一个填factor,后面填数据,要求factor和数据的长度一致
    
    > #Coloring Negative and Positive Bars Differently
    > str(climate)
    'data.frame':   499 obs. of  6 variables:
     $ Source    : chr  "Berkeley" "Berkeley" "Berkeley" "Berkeley" ...
     $ Year      : num  1800 1801 1802 1803 1804 ...
     $ Anomaly1y : num  NA NA NA NA NA NA NA NA NA NA ...
     $ Anomaly5y : num  NA NA NA NA NA NA NA NA NA NA ...
     $ Anomaly10y: num  -0.435 -0.453 -0.46 -0.493 -0.536 -0.541 -0.59 -0.695 -0.763 -0.818 ...
     $ Unc10y    : num  0.505 0.493 0.486 0.489 0.483 0.475 0.468 0.461 0.453 0.451 ...
    > csub <- subset(climate, Source=="Berkeley" & Year>=1900)
    > csub$pos <- csub$Anomaly10y >= 0   #新增一列“pos”储存Anomaly10y是否大于等于0的返回值(True或False)
    > str(csub)
    'data.frame':   105 obs. of  7 variables:
     $ Source    : chr  "Berkeley" "Berkeley" "Berkeley" "Berkeley" ...
     $ Year      : num  1900 1901 1902 1903 1904 ...
     $ Anomaly1y : num  NA NA NA NA NA NA NA NA NA NA ...
     $ Anomaly5y : num  NA NA NA NA NA NA NA NA NA NA ...
     $ Anomaly10y: num  -0.171 -0.162 -0.177 -0.199 -0.223 -0.241 -0.294 -0.312 -0.328 -0.281 ...
     $ Unc10y    : num  0.108 0.109 0.108 0.104 0.105 0.107 0.106 0.105 0.103 0.101 ...
     $ pos       : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
    > ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) + 
         geom_bar(stat = "identity", position = "identity") +
         scale_fill_manual(values = c("#669933", "#FFCC66"), guide=FALSE)
    #values = c("#669933", "#FFCC66")表示替换fill=pos的两种颜色,guide=FALSE为去掉图例​  
    
    #Adjusting Bar Width and spacing  调整柱的宽度和间距
    ggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat = "identity")
    
    ggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat = "identity", width = 0.5)
    # 默认的 width为 0.9​
    
    ggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat = "identity", width = 1)
    # 默认的 width为 0.9​
    
    # Grouped data
    ggplot(cabbage_exp, aes(x=Date, y=Weight, fill =Cultivar)) +
      geom_bar(stat = "identity", width = 0.5, position = "dodge")
    
    ggplot(cabbage_exp, aes(x=Date, y=Weight, fill =Cultivar)) +
      geom_bar(stat = "identity", width = 0.5, position = position_dodge(0.7))
    
    ggplot(cabbage_exp, aes(x=Date, y=Weight, fill =Cultivar)) +
      geom_bar(stat = "identity", width = 0.5, position = position_dodge(0.5))
    
    ggplot(cabbage_exp, aes(x=Date, y=Weight, fill =Cultivar)) +
      geom_bar(stat = "identity", width = 0.5, position = position_dodge(0.2))
    
    > # Adding labels to a Bar Graph
    > # below the top
    > cabbage_exp
      Cultivar Date Weight        sd  n         se
    1      c39  d16   3.18 0.9566144 10 0.30250803
    2      c39  d20   2.80 0.2788867 10 0.08819171
    3      c39  d21   2.74 0.9834181 10 0.31098410
    4      c52  d16   2.26 0.4452215 10 0.14079141
    5      c52  d20   3.11 0.7908505 10 0.25008887
    6      c52  d21   1.47 0.2110819 10 0.06674995
    > ggplot(cabbage_exp, aes(x=interaction(Date,Cultivar), y=Weight)) + 
     ​   geom_bar(stat = "identity") + 
     ​   geom_text(aes(label = Weight), vjust = 1.5, colour = "white") 
    > #x=interaction(Date,Cultivar)表示对每一行的Date和Cultivar进行组合
    > #aes(label = Weight)表示标注的内容为对应的Weight值
    > #vjust>0则label位于顶端下部,colour = "white"表示字体颜色为白色
    
    #Above the top
    ggplot(cabbage_exp, aes(x=interaction(Date,Cultivar), y=Weight)) + 
      geom_bar(stat = "identity") + 
      geom_text(aes(label = Weight), vjust = -0.2) 
    
    #Adjust y limits to be a little higher
    ggplot(cabbage_exp, aes(x=interaction(Date,Cultivar), y=Weight)) + 
      geom_bar(stat = "identity") + 
      geom_text(aes(label = Weight), vjust = -0.2) + 
      ylim(0, max(cabbage_exp$Weight)*1.05)
    
    #Adjust y limits to be higher
    ggplot(cabbage_exp, aes(x=interaction(Date,Cultivar), y=Weight)) + 
      geom_bar(stat = "identity") + 
      geom_text(aes(label = Weight), vjust = -0.2) + 
      ylim(0, max(cabbage_exp$Weight)*1.5)
    
    # Map y position slightly above bar top
    ggplot(cabbage_exp, aes(x=interaction(Date,Cultivar), y=Weight)) +
      geom_bar(stat = "identity") +
      geom_text(aes(y=Weight+0.1, label = Weight))
    
    ggplot(cabbage_exp, aes(x=interaction(Date,Cultivar), y=Weight)) +
      geom_bar(stat = "identity") +
      geom_text(aes(y=Weight-0.1, label = Weight))
    
    ggplot(cabbage_exp, aes(x=Date, y=Weight, fill = Cultivar)) +
      geom_bar(stat = "identity", position = "dodge") +
      geom_text(aes(y=Weight+0.15, label = Weight), position = position_dodge(0.9), size=4)
    

    欢迎关注微信公众号:BioLearner

    相关文章

      网友评论

          本文标题:ggplot2学习(4)

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