04pallav
9/11/2017 - 10:57 PM

ggplot2

ggplot2

#HISTOGRAM OF DATA
qplot(data=myData,x=BM,main=”Histogram of BodyMass”)
#main gives the title of the plot

#scatter plot of data colored by the column Tribe
qplot(data=myData,x=BM,y=var1,log=”xy”,color=Tribe)
qplot(carat, price, data = dsmall, shape = color,alpha = I(1/10)))

#scatter plots with different shapes #uses defauly geom="point"
#alpha is transparency

geom = "smooth" fits a smoother to the data and displays the smooth and
its standard error

geom = "boxplot" produces a box-and-whisker plot to summarise the
distribution of a set of points,


#boxplot
qplot(data=myData,x=Hab,y=var1,geom=”boxplot”)

1D distributions---------------------
qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.1,xlim = c(0,3))

geom = "histogram" draws a histogram, geom = "freqpoly" a frequency polygon, and geom = "density" creates a density plot

discrete variables, geom = "bar"
qplot(color, data = diamonds, geom = "bar")
qplot(color, data = diamonds, geom = "bar", weight = carat) +
scale_y_continuous("carat")
############################################
to compare the distributions of dierent subgroups, just add an aesthetic
mapping, as in the following code.
qplot(carat, data = diamonds, geom = "density", colour = color)
qplot(carat, data = diamonds, geom = "histogram", fill = color)
#########################################################
Combining multiple geoms----------------------
qplot(carat, price, data = dsmall, geom = c("point", "smooth"))

a categorical variable and one or more continuous variables---------
(geom = "jitter"),(geom ="boxplot")
qplot(color, price / carat, data = diamonds, geom = "jitter",alpha = I(1 / 5))
# 1st is categorical variable,2nd is quantitative
#############################################################
time series 
qplot(date, unemploy / pop, data = economics, geom = "line")
##Line plots join the points from left to right, while path plots join them in the order that they appear in the dataset
############################################################
FACETING
qplot(carat, data = diamonds, facets = color ~ .,
geom = "histogram", binwidth = 0.1, xlim = c(0, 3))

qplot(carat, ..density.., data = diamonds, facets = color ~ .,
geom = "histogram", binwidth = 0.1, xlim = c(0, 3))
##############################################
General options
#title
ggtitle("Title1")
qplot(carat, price, data = dsmall,xlab = "Price ($)", ylab = "Weight (carats)",main = "Price-weight relationship")
qplot(carat, price, data = dsmall, log = "xy")
##########
geom_XXX(mapping, data, ..., geom, position)
stat_XXX(mapping, data, ..., stat, position)
p %+% mtcars #exchanging datset within a plot
##################################################################ggplot2
ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex)) + geom_point()+scale_colour_hue(l=45)
############################################################bar plot with count labels
ggplot(data=diamonds,aes(x=clarity)) + geom_bar() + geom_text(stat='bin',aes(label=..count..),vjust=-1)
####################giving manual colors
scale_color_manual(values=c("green", "blue","yellow","black","orange","orange","red"))