2011-10-25 27 views
0

我有以下R代码里面:R:控制规模GGPLOT2的传奇

ggplot(data=curve,aes(x = expected, y=result/games)) + 
geom_point(aes(x=-expected, colour=games)) + 
stat_function(fun=funx, geom="line", col="blue") + 
scale_colour_continuous(name="Number of games") 

然而,在我的传说我得到的值一样1E + 05,+ 2E 05 .. 6E + 05等 我的问题是,有些值非常低(从0-100),有些非常大(高达600000)。我打算使用“break”来指定范围从0-10,11-100,1001-5000)等等。但是,当我把它放到scale_colour_continuous中时,只有图例发生变化,但在我的图表上没有颜色。

UPDATE:

我曾经建议的解决方案,但我得到的错误:

Warning messages: 
1: In Ops.factor(result, games) :/not meaningful for factors 
2: In Ops.factor(result, games) :/not meaningful for factors 
3: In Ops.factor(result, games) :/not meaningful for factors 
+0

未经'curve'示例数据,我不能帮助更多。如果'curve'没有太多的行,'dput(curve)'的输出将会非常有用。如果有很多行,则为'expected','result'和'games'创建一个包含各种值的子集,然后给它的'dput()'。我觉得你似乎用离散版本覆盖了'游戏'变量。由于您使用“游戏”和“游戏”的离散版本,因此您需要将其变成另一个变量。 –

+0

@Brian:dput(曲线)完成后,我更新了问题。谢谢你的努力! – mkk

回答

2

如果要离散的规模,如果你改变变量(或创建一个新的变量),这是最简单的并且用它来绘图。 ggplot不能将连续变量转换为离散变量。

curve$games.d <- cut(curve$games, breaks=c(0,10,100,5000,Inf), 
    labels=c("0-10", "11-100", "101-5000", "5000+"), include.lowest=TRUE) 

然后用colour=games.d绘制,如果你需要调整规模以上(标签等)使用scale_colour_discrete

UPDATE

感谢dput输出。它明确了错误现在的位置。

> str(curve) 
'data.frame': 223 obs. of 4 variables: 
$ expected: int -402 -400 -391 -390 -386 -385 -383 -380 -379 -375 ... 
$ result : Factor w/ 194 levels "0","0,5","1",..: 3 3 3 30 2 3 2 3 3 2 ... 
$ games : int 1 1 1 2 1 1 2 1 1 1 ... 
$ colgame : Factor w/ 4 levels "0","100","5000",..: 1 1 1 1 1 1 1 1 1 1 ... 

请注意result是一个因素。我认为你在使用小数点分隔符是逗号而不是句号的符号。这些必须被转换为数字(见进一步指出后一种方式来避免这种情况摆在首位)

curve$result <- as.numeric(gsub(",",".",as.character(curve$result))) 

现在你的绘图代码将是:(我注释掉stat_function呼叫,因为我没有你功能funx)。

ggplot(data=curve,aes(x = expected, y=result/games)) + 
geom_point(aes(x=-expected, colour=colgame)) + 
#stat_function(fun=funx, geom="line", col="blue") + 
scale_colour_discrete(name="Number of games") 

ggplot plot

至于如何可能避免这种情况摆在首位,假设你从一个CSV文件中读取这,看看read.csv2dec参数传递给read.table家庭的功能,以指定数字的小数点符号。

+0

我很愚蠢。我刚刚在Excel中更改了它,以便能够按数字排序,并且意外地我必须保存它:)感谢您发现错误!现在它就像一个魅力! – mkk

2

如果你把一个可重复的例子,我们会更容易帮助。我不确定明白你想要什么。但有些指针要做你想做的。

如果你想使用休息和改变颜色,那么你需要将颜色映射到审美。

从布赖恩·迪格斯例如贷款额:

curve$games.d <- cut(curve$games, breaks=c(0,10,100,5000,Inf), 
    labels=c("0-10", "11-100", "101-5000", "5000+"), include.lowest=TRUE) 

ggplot(data=curve,aes(x = expected, y=result/games, colour = games.d)) + 
geom_point(aes(x=-expected)) + 
stat_function(fun=funx, geom="line", col="blue") + 
scale_colour_continuous(name="Number of games") 

HTH

+0

在'ggplot'调用中'color = games.d'和'geom_point'中的'color = games'会产生问题,因为它在一种情况下映射到离散变量,在另一种情况下映射到一个连续变量。 –

+0

我得到 警告消息: 1:在Ops.factor(结果,游戏)中:/对因素没有意义 2:在Ops.factor(结果,游戏)中:/对因素没有意义 3:在Ops.factor (结果,游戏):/对因素没有意义 – mkk

+0

@BrianDiggs,Ops,我错过了。我将编辑该问题。 –