Plotting multiple data series in #R using #Lattice
Hello Readers. Thank you for paying this blog a visit or two. I am very surprised to see the number of visitor. I will continue to post my progress and also my learning process. The following code is a short tutorial to make a simple chart usinglattice package
. I use the electricity consumption data from US Dept.of Energy. To make a simple plot using R is a learning curve. It is straight forward, so you will be able to use it, once you get the basic properties. Loading package and data
- loading library
- loading data (30 rows and 6 columns)
- example on subsetting data, only read row 1 to 29, and excluding line 30.
require("lattice")
## Loading required package: lattice
data <- read.csv("enconsump.csv")
mydata <- data[c(1:29), ]
First plot: Simple xyplot
using the available data
- plotting
- x axis=Country column
- y axis=Year column (2007-2011)
- point style (pch)=16
- legend by auto key
- x and y is the coordinate of the legend
xyplot(mydata$Country ~ mydata$X2007 + mydata$X2008 + mydata$X2009 + mydata$X2010 +
mydata$X2011, main = "Electricity consumption (2007-2011)", type = "p",
pch = 16, auto.key = list(x = 0.7, y = 0.8, text = c("2007", "2008", "2009",
"2010", "2011"), title = "Year"), ylab = "countries", xlab = "billion Kwh")
Second plot: Simple plot
using manually-entered data
- making plot from manually entered data
- making short time series of world's total electricity consumption
main
: setting chart titletype="b"
: type both point and linepch=16
: setting point typexaxt="n"
: no axis labelxlab
: setting x axis labelylab
: setting y axis labelaxis(1...
: setting x tick label
total <- c(17149.41987, 17410.00845, 17316.83824, 18501.4416, 19298.53181)
plot(total, main = "World's total electricity consumption", type = "b", pch = 16,
xaxt = "n", xlab = "Years", ylab = "energy consumption, in billion Kwh")
axis(1, at = 1:5, labels = c("2007", "2008", "2009", "2010", "2011"))
#References: Energy Report-US Dept of Energy
-->
No comments:
Post a Comment