Reputation: 2812
I'm quite new to R and ggplot and am having a tough time grasping how I am supposed to solve this problem in ggplot.
Essentially I want to draw 2 lines on a plot. One for method "a" and one for method "b". That is usually straightforward, but now I have a situation where I want to use functions in the aesthetic.
I want to do rank
and length
, but for each grouping separately. In this ggplot code, the rank and length are computed over all values. I have tried a lot of different configurations, but can't seem to get this! I include the code here to get the desired plot with regular plots.
d <- rbind(
data.frame(value=1:100, method=c("a")),
data.frame(value=50:60, method=c("b"))
)
ggplot(d, aes(x=value, y=rank(value)/length(value), colour=method)) + geom_point()
a <- d$value[d$method=="a"]
b <- d$value[d$method=="b"]
plot(
rank(a)/length(a),
col="red",
xlab="value",
ylab="F(value)",
pch=19
)
points(
rank(b)/length(b),
col="blue"
)
Is this possible with ggplot or do I need to do my calculations beforehand and then make a special plotting dataframe?
I am finding ggplot powerful, whenever I know how to do something, but frustrating as soon as I don't! Especially when I don't know if it can't do something, or if I just don't know how!
Thanks
Upvotes: 0
Views: 204
Reputation: 2812
Thanks to the commenters. Here is their solution in the context of my test case for reference.
Ranking the grouped values outside of ggplot.
d <- rbind(
data.frame(value=1:100, method=c("a")),
data.frame(value=50:60, method=c("b"))
)
d <- mutate(group_by(d, method), rank=rank(value)/length(value))
ggplot(d, aes(x=value, y=rank, colour=method)) + geom_point()
Upvotes: 0