Start with ggplot in 2 minutes ! - vaibhavdobriyal/R_Tutorials GitHub Wiki

Install ggplot

install.packages("ggplot2")
library(ggplot2)

We will play with msleep dataset already available with R. you can use head or print the dataset to view contents.

a <- ggplot(data = msleep, aes(x = bodywt, y = sleep_total))
a

Data set is mssleep and on x axis we have bodywt and on y sleep_total

a <- a + geom_point()
a

The above line assumes that you are using defaults to generate aesthetics Let us bring in labels

a <- a + xlab("Body Weight") + ylab("Total Hours Slept") + ggtitle("Sleep Data plot")
a

You can also run the above as a code block

a <- ggplot(data = msleep, aes(x = bodywt, y = sleep_total))
a <- a + geom_point()
a <- a + xlab("Body Weight") + ylab("Total Hours Slept") + ggtitle("Sleep Data plot")
a

This is the bare bone basics Start exploring more

http://blog.echen.me/2012/01/17/quick-introduction-to-ggplot2/ http://www.noamross.net/blog/2012/10/5/ggplot-introduction.html

Requirement: Group the plot by localDate and MeterID

Input Data include data for each meter, for a date and time and the usage value.

plot <- ggplot(aes(y = scaled_usage , x = strTime, group =key), data = subset(p, usage != 0)) + 
  geom_line(aes(y = scaled_usage , x = strTime,colour=meterID),alpha=0.1) +
  #geom_point(alpha=0.01)+
  theme_bw(16) + 
  ggtitle("Usage vs. Time of Reading")+
  scale_size_area() + 
  xlab("Local Time") +
  ylab("Usage") + theme(legend.position = "top") #+ facet_grid(.~meterID)
plot

We tried by specifying the localDate as group but the graph was not clean and there was no way to add two column in grouping. The easy hack was to create one more column.

p$key <- paste0(p$localDate,p$meterID)

Suggested Reads:

http://www.cookbook-r.com/Graphs/