2010-05-26 54 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