2010-05-26 25 views
0

如何在使用ggplot時輕鬆創建小數y值?ggplot中的小數y-var

t <- as.factor(test=sample(seq(0,100,10),1000,rep=T)) 
d <- as.factor(sample(c(0,1),1000,rep=T) 
p <- data.frame(t,d) 

我最好的拍攝是:

ggplot(p,aes(x=t,y=prop.table(table(t,d),1)[,1])) + geom_point() 

但是這並不工作,我想有解決這個更簡單的方法...

+0

不是2011年1月?爲什麼這個彈出到列表的頂部? – Chase 2011-01-22 20:09:41

回答

0

如果我理解正確你的問題,這應該得到你想要的東西,從使用的@jthetzel功能formatFraction上述

plotData <- data.frame(prop.table(table(t,d),1)) 

#Plot only non-zero values 
plotData <- plotData[plotData$d == 1 , ] 

ggplot(data = plotData, aes(x = t, y = Freq)) + geom_point() + 
scale_y_continuous(formatter = "formatFraction") 
1

scale_y_continuous()接受「格式化」的說法,其可以採取功能。我在和你的示例代碼一些錯誤,但考慮到以下幾點:

require(ggplot2) 

t <- as.factor(sample(seq(0,100,10),1000,rep=T)) 
d <- as.factor(sample(c(0,1),1000,rep=T)) 
p <- data.frame(t,d) 

formatFraction <- function(x) 
{ 
    fraction <- paste(x*100, '/100', sep='') 
    fraction 
} 


plot <- ggplot(p, aes(x=prop.table(table(t, d), 1)[,2], 
    y=prop.table(table(t, d), 1)[,1])) + geom_point() 
plot + scale_y_continuous(formatter=formatFraction) 
+0

我會避免命名一個對象'plot',因爲這可能會掩蓋base R中的'plot()'函數。另外,我不認爲OP想要轉換x軸?如果我閱讀正確的話,這是一個有10個關卡的因素。 – Chase 2011-01-22 20:05:54