2017-08-28 49 views
0

如何使用ifelse和lattice的stripchart()更改某些點的顏色?我可以創建使用stripchart(d, method="stack", offset=1, pch=19, at=1.0)以下點陣圖:着色條形碼在R

enter image description here

我還可以更改使用參數col情節的顏色,但我試圖做更復雜的東西,像這樣:ifelse(d>40, "Red", "Black")。 (在這個例子中,小於或等於40的所有點都是黑色的,而高於40的點都是紅色的。)我如何將ifelse應用於這個圖?

+0

我不知道這是因爲這容易帶狀圖()... –

+0

不是。如果我使用dotplot(),我可以使顏色起作用,但是我無法得到點堆疊起來.... – quil

回答

0

也許最簡單的辦法就是先寫一個簡單的腳本使用標準的劇情做數學題並畫出了帶狀圖()函數

#generate some random numeric data  
set.seed(100) 
    d <- sample(10:80, size = 150, replace = TRUE) 

# count re-occurences of the same value (for plotting on the y-axis) 
td <- do.call(rbind, lapply(1:length(d), (function(i){ 
     c(d[i], 1+ sum(d[1:i] == d[i]))}))) 

# plot 
plot(x= td[,1], y = td[,2], 
     pch=19, ylim = c(0, 20), 
     col = ifelse(td[,1]>49, "red", "blue")) 

enter image description here