R語言可視化學習之【Lattice包】


 本文采用lattice包幫助文檔中的代碼,進行參數說明和結果解釋。

Let's begin

 

Depth <- equal.count(quakes$depth, number=8, overlap=.1)
xyplot(lat ~ long | Depth, data = quakes)

第一行代碼調用函數 equal.count()對連續變量quakes$depth離散化,把該變量轉換為因子類型。參數number設置需要的區間個數,參數 overlap 設置兩個區間之間的靠近邊界的重合(這意味着某些觀測值將被分配到相鄰的區間中)。每個區間的觀測值的個數相等。

第二行代碼繪制在不同depth情況下lat和long的散點圖(lat為Y軸,long為X軸)

update(trellis.last.object(),
       strip = strip.custom(strip.names = TRUE, strip.levels = TRUE),
       par.strip.text = list(cex = 0.75),
       aspect = "iso")

strip.levels控制顯示各個水平Depth:[]

cex控制缺省狀態下符號和文字大小的值;

aspect  控制面板的物理高寬比。可以指定為比率或字符串。字符串參數有:

fill(默認值):試圖使面板盡可能大以填充可用空間

xy:根據45度銀行規則計算寬高比

iso:設備上的物理距離與數據標度中的距離之間的關系被強制為兩個軸相同

如果指定了預處理函數並返回組件dx和dy,則這些用於銀行計算。 否則,使用默認的預備函數的值。 並非所有的默認預備函數都能產生明智的銀行計算。

EE <- equal.count(ethanol$E, number=9, overlap=1/4)

## Constructing panel functions on the fly; prepanel
xyplot(NOx ~ C | EE, data = ethanol,
       prepanel = function(x, y) prepanel.loess(x, y, span = 1),
       xlab = "Compression Ratio", ylab = "NOx (micrograms/J)",
       panel = function(x, y) {
           panel.grid(h = -1, v = 2)
           panel.xyplot(x, y)
           panel.loess(x, y, span=1)
       },
       aspect = "xy")

panel函數,指定實際的繪圖,里面常用一些panel函數。panel.grid添加水平和豎直網格線,中的h、v指定要添加到圖中的水平參考線和垂直參考系的數量。為-1時代表使得網格線與軸標簽對齊

panel.rug()在每個面板的x軸和y軸添加軸須線,panel.lmline()添加回歸線,panel.xyplot()添加散點圖,panel.loess()添加平滑擬合曲線

prepanel預處理函數,使用與panel相同的參數,並返回一個列表,可能包含xlim,ylim,dx,dy等。如果沒有,則使用默認值。

 

  

## Extended formula interface 

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width | Species,
       data = iris, scales = "free", layout = c(2, 2),
       auto.key = list(x = .6, y = .7, corner = c(0, 0)))

  

 

## user defined panel functions

states <- data.frame(state.x77,
                     state.name = dimnames(state.x77)[[1]],
                     state.region = state.region)
xyplot(Murder ~ Population | state.region, data = states,
       groups = state.name,
       panel = function(x, y, subscripts, groups) {
           ltext(x = x, y = y, labels = groups[subscripts], cex=1,
                 fontfamily = "HersheySans")
       })

  

## Stacked bar chart

barchart(yield ~ variety | site, data = barley,
         groups = year, layout = c(1,6), stack = TRUE,
         auto.key = list(space = "right"),
         ylab = "Barley Yield (bushels/acre)",
         scales = list(x = list(rot = 45)))

  

bwplot(voice.part ~ height, data=singer, xlab="Height (inches)")

  

 

 

dotplot(variety ~ yield | year * site, data=barley)

  

## Grouped dot plot showing anomaly at Morris

dotplot(variety ~ yield | site, data = barley, groups = year,
        key = simpleKey(levels(barley$year), space = "right"),
        xlab = "Barley Yield (bushels/acre) ",
        aspect=0.5, layout = c(1,6), ylab=NULL)

  

 

stripplot(voice.part ~ jitter(height), data = singer, aspect = 1,
          jitter.data = TRUE, xlab = "Height (inches)")

  

## Interaction Plot

xyplot(decrease ~ treatment, OrchardSprays, groups = rowpos,
       type = "a",
       auto.key =
       list(space = "right", points = FALSE, lines = TRUE))

  

bwplot(decrease ~ treatment, OrchardSprays, groups = rowpos,
       panel = "panel.superpose",
       panel.groups = "panel.linejoin",
       xlab = "treatment",
       key = list(lines = Rows(trellis.par.get("superpose.line"),
                  c(1:7, 1)),
                  text = list(lab = as.character(unique(OrchardSprays$rowpos))),
                  columns = 4, title = "Row position"))

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM