<title>About Ggplot2 Package (i)</title> About Ggplot2 Package (i) Ggplot2 basic elements
- Data and Mapping (Mapping)
- Geometric objects (geometric)
- Ruler (scale)
- Statistical Transformations (Statistics)
- coordinate system (coordinate)
- Layers (layer)
- Faceted (facet)
- Theme (Theme)
Data and Mapping (Mapping)
The following is an example of diamonds data, because the data is very large, randomly select a subset to draw
> Library(ggplot2)
> Data(diamonds)
> Set. seed($) #设定生成随机数的种子 to make the results repeatable
> small<-diamonds[ Sample(Nrow (diamonds) , +),] #抽样
> head(small)
Carat cutColorClarity DepthTablePrice x y z
49345 0.71Very Good H SI162.5 - 2096 5.68 5.75 3.57
50545 0.79Premium H SI161.8 - 2275 5.97 5.91 3.67
15434 1.03Ideal F SI162.4 $ 6178 6.48 6.44 4.03
44792 0.50Ideal E VS262.2 Wu 1624 5.08 5.11 3.17
34614 0.27Ideal E VS161.6 About 470 4.14 4.17 2.56
27998 0.30Premium E VS261.7 - 658 4.32 4.34 2.67
Summary, please.
>Summary(Small)
Carat Cut Color Clarity Depth
Min.: 0. 2200 Fair: 28D: 121 SI1 : 258 Min.:.
1St Qu.: 0. 4000 Good: 88E: 186 VS2 : 2311St Qu.:. XX
Median : 0. 7100 Very Good: 227 F: 164 SI2 : 175 Median :.
Mean : 0. 8187 Premium : 257 G: 216 VS1 : 141 Mean :.
3Rd Qu.: 1. 0700 Ideal : H: 154 VVS2: 91 3Rd Qu.:. the
Max.: 2. 6600 I: 106 VVS1: 67Max.:.
J: 53 ( Other): 37
Table Price x y
Min.:. Ten Min. : 342. 0 Min.: 3. 850 Min.: 3. 840
1St Qu.:. XX1St Qu.: 989. 51St Qu.: 4. 7401St Qu.: 4. 758
Median :. XX Median: 2595. 0 Median : 5. Median : 5. 775
Mean :. Mean: 4110. 5 Mean : 5. 787 Mean : 5. 791
3Rd Qu.:. XX3Rd Qu.: 5495. 23Rd Qu.: 6. the3Rd Qu.: 6. 610
Max.:. XX Max.: 18795. 0 Max.: 8. 830 Max.: 8. 870
Z
Min.: 2. the
1St Qu.: 2. 920
Median : 3. 550
Mean : 3. 572
3Rd Qu.: 4. 070
Max.: 5. 580
The number of carats (carat) is the x-axis variable, and the price is the y-axis variable
> p<-ggplot(data=small,mapping=aes(x=carat,y=price))#将数据映射到XY坐标轴上
Below, draw a scatter plot.
> p+geom_point()
If you want to map cut (cut) to shape properties:
> p<-ggplot(data=small,mapping=aes(x=carat,y=price,shape=cut))
> p+geom_point()
If you want to map the color attribute again (color):
> p<-ggplot(data=small,mapping=aes(x=carat,y=price,shape=cut,colour=color))
> p+geom_point()
Geometric objects (geometric)
In the above example, the various attribute mappings are performed by the Ggplot function, only one layer is added, and Geom_point () is used to tell Ggplot to draw a scatter plot, so all the attributes are mapped to the scatter point.
As for geom_histogram histogram, used for drawing geom_bar column chart, geom_boxplot for drawing box-type diagram and so on.
, you can also use the following code to draw
> p<-ggplot(small)
> p+geom_point(aes(x=carat,y=price,shape=cut,colour=color))
Histogram
>ggplot(small)+geom_histogram(aes(x=price))
You can also fill a color with another variable.
>ggplot(small)+geom_histogram(aes(x=price,fill=cut))
Again, they can be separated
>ggplot(small)+geom_histogram(aes(x=price, fill=cut)position="dodge")
You can also draw in relative proportions,
>ggplot(small)+geom_histogram(aes(x=price, fill=cut)position="fill")
Bar chart
ggplot(small)+geom_bar(aes(x=clarity))
The stat parameter allows Geom_bar to draw at a specified height,
ggplot()+geom_bar(aes(x=c(LETTERS[1:3]),y=1:3), stat="identity")
Density function diagram
ggplot(small)+geom_density(aes(x=price, colour=cut))
ggplot(small)+geom_density(aes(x=price,fill=clarity))
The colour parameter specifies the color, which fills the color below the curve
Box-type diagram
ggplot(small)+geom_boxplot(aes(x=cut, y=price,fill=color))
Here are a variety of geom_xxx functions
geom_abline geom_area
geom_bar geom_bin2d
geom_blank geom_boxplot
geom_contour geom_crossbar
geom_density geom_density2d
geom_dotplot geom_errorbar
geom_errorbarh
geom_hex geom_histogram
geom_hline geom_jitter
geom_line geom_linerange
geom_map geom_path
geom_point geom_pointrange
geom_polygon
geom_raster geom_rect
geom_ribbon geom_rug
geom_segment geom_smooth
geom_step geom_text
geom_tile geom_violin
geom_vline
R Package--ggplot2 (i)