Introduction
There is no doubt about the quality of the Ggplot2 package, but its drawing grammar is still a little difficult for beginners, Ggplot2: Data analysis and Graphic Art This book also introduces the basic philosophy and operation of Ggplot2 package, and the personal feeling examples are not rich enough, So the use of the package is still stuck at the dabbler level. One day, suddenly found a Ggplot2 package example book, exultation, English name is R Graphics Cookbook, Chinese version is called R Data Visualization Handbook, in view of the high price, I decided to bite the bullet to the English version, by the way to do some notes, for their own or friends for reference, the English level is not high, If there is a mistake, Mo strange. Basic operations
First of all, I would like to introduce the use of Ggplot2 to draw histograms, the basic usage is as follows:
Install.packages (' Gcookbook ') #R数据可视化手册书中的数据集
Library (ggplot2)
Library (Gcookbook)
Ggplot (Pg_mean, AES (X=group, Y=weight)) + Geom_bar (stat= "Identity")
The Ggplot () function generates layers, AES () specifies x and Y axis variables, x is typically nominal, and geom_bar generates a histogram layer.
If x is a continuous variable or a numeric variable, the histogram is somewhat different. If it is a continuous variable, the horizontal axis of the histogram takes each numerical value between the maximum and the minimum value of the continuous variable, and if it is a numeric variable, you have to convert the numeric variable into a factor, as shown in the code.
BOD#BBD DataSet time variable does not contain 6 This value time
demand
1 8.3
2 10.3
3 19.0
4 16.0
5 15.6
7 19.8
Ggplot (BOD, AES (X=time, Y=demand)) + Geom_bar (stat= "Identity") #直接赋值, do not make factor conversion
Ggplot (BOD, AES (X=factor (time), y= Demand) + Geom_bar (stat= "Identity") #变量进行因子转化
The difference in x after assigning a continuous variable to x and a number to a factor can be clearly seen from the diagram. Adjust color
The histogram defaults to a black-gray fill, and we can modify the fill color and histogram border color.
Ggplot (Pg_mean, AES (X=group, Y=weight)) +
Geom_bar (stat= "Identity", fill= "LightBlue", colour= "Black")
Grouping histograms
Sometimes we want to compare two different kinds of objects in the same data, we can use the grouping histogram. We use the Cabbage_exp dataset, which contains two different types of data
cabbage_exp# DataSet
cultivar Date Weight c39 d16 3.18 c39 d20 2.80 c39
d21 2.74 c52
D16 2.26
c52 d20 3
c52 D21 1.47 ggplot (Cabbage_exp,aes (x=date, Y=weight
, Fill=cultivar)) +geom_bar (stat= ' identity ', Position= "Dodge") #identity意味着把y当做值去输入, if changed to Bin, you will calculate the frequency of the y appears. Dodge means that each group is distributed around rather than up and down
ggplot (Cabbage_exp,aes (X=date, Y=weight, Fill=cultivar)) +geom_bar (stat= ' identity ')
Make frequency histogram
The frequency histogram does not need to specify the variable y, but is automatically generated based on the frequency of the variable x.
Ggplot (Diamonds, AES (X=cut)) + Geom_bar (stat= "bin") #stat = "Bin" is optional, this parameter is the default
Give a positive and negative histogram a different color
Csub <-subset (climate, source== "Berkeley" & Year >= 1900) Csub$pos <-csub$anomaly10y >=
0 #pos变量是个布尔 Variable with a value of T or F
Ggplot (Csub, AES (X=year, y=anomaly10y, Fill=pos)) +geom_bar (stat= "Identity", position= "Identity") # Position= "Identity" is to turn off warnings that are not defined by negative histograms
Adjust histogram column size and width
The histogram width is adjusted with the width function and the default value is 0.9. We can change the value to make the column wider or narrower.
Ggplot (Pg_mean, AES (X=group, Y=weight)) + Geom_bar (stat= "Identity", width=0.5) #窄柱子
ggplot (Pg_mean, AES (X=group, Y =weight) + Geom_bar (stat= "Identity", width=0.5) #宽柱子
There is no gap between the columns in the group histogram, if we want to change the distance between the columns, we need to set Position=po
Sition_dodge ().
Ggplot (Cabbage_exp, AES (X=date, Y=weight, Fill=cultivar)) +geom_bar (stat= "Identity", width=0.5, Position=position_ Dodge (0.6)) #值越大越离得远
Add data to the diagram
You can add a drawing or data to the histogram using Geom_text (), and you can set the Vjust to display the data on the pillar or façade.
Ggplot (Cabbage_exp,aes (X=interaction (Date, cultivar), y=weight)) +geom_bar (stat= "Identity") +geom_text (Aes (label= Weight), vjust=-0.2) #显示在上面
ggplot (Cabbage_exp,aes (X=interaction (Date,cultivar), y=weight)) +geom_bar (stat=) Identity ") +geom_text (Aes (label=weight), vjust=1.5,colour=" white ") #显示在里面
For the time being, we'll start with some basics, more advanced topics or examples, reference to the R data visualization manual, or multiple Help ().