ggplot aesthetics in R (Grammer of graphics)
ggplot aesthetics in R, we use graphics grammar to create multivariate data visualization.
The ggplot() command in ggplot2 creates a plot, and any arguments passed to it are used in all future plotting commands.
ggplot aesthetics in R
library(mdsr) head(CIACountries)
country pop area oil_prod gdp educ 1 Afghanistan 32564342 652230 0 1900 NA 2 Albania 3029278 28748 20510 11900 3.3 3 Algeria 39542166 2381741 1420000 14500 4.3 4 American Samoa 54343 199 0 13000 NA 5 Andorra 85580 468 NA 37200 NA 6 Angola 19625353 1246700 1742000 7300 3.5 roadways net_users 1 0.06462444 >5% 2 0.62613051 >35% 3 0.04771929 >15% 4 1.21105528 <NA>
Since we specified the CIACountries data frame in the data parameter, any variables mentioned elsewhere in the plot are believed to be within that data frame in this case.
Elements are used to incrementally build graphics in ggplot2. The geom_point() function is used to plot the sole elements in this instance, which are points.
library(ggplot2) g<-ggplot(data=CIACountries, aes(y=gdp, x=educ)) g+geom_point(size=3)
The geom_point() option specifies the location and method of drawing the points.
Here, the two aesthetics (aes()) transfer the educ variable to the horizontal (x) position and the gdp variable to the vertical (y) coordinate.
The size of each glyph is altered by the size argument to geom_point().
Notice how each dot is the same size in this image. Since it does not link a variable to a visual signal, this size is not aesthetically pleasing.
Each dot stands in for one country as each case is a different one. One glyph can only be distinguished from another by position in the frame.
There is nothing about the symbol itself that identifies the country; the shapes, sizes, etc. of all the glyphs differ.
g+geom_point(aes(color=net_users),size=3)
A glyph can, however, be used with many variables. To develop new visual cues, we can define extra aesthetics.
g+geom_text(aes(label=country, color=net_users), size=3)
The glyph can be changed by just altering the function that creates it; the aesthetic is frequently kept untouched.
GDP is being mapped to a vertical position, Internet connectivity to color, road length to size, and educational achievement to a horizontal position.
g+geom_point(aes(color=net_users,size=roadways))
Thus, we employ the visual cues of position, color, and area, respectively, to represent four variables (gdp, educ, net_users, and highways).
The graphic allows you to select which variables it will show as well as how each variable will be pictorially represented, such as position, size, color, and so on.