我正在使用 ggplot2 使用 geom_area
绘制以下数据。这些值被设计为 x 轴镜像。但《狗》情节的结局并没有反射(reflect)出来。
但是从这个数据帧中可以看出,狗应该以 x=100 和 y=0 结束,但它似乎以 x=100 和 y=100 结束(参见最后一行)
怎样才能准确地制作狗镜像猫?
x y animal
1 100.0 100 cat
2 90.0 89 cat
3 84.0 85 cat
4 55.5 60 cat
5 28.3 37 cat
6 27.0 32 cat
7 18.0 25 cat
8 0.0 0 cat
9 0.0 100 dog
10 10.0 89 dog
11 16.0 85 dog
12 44.5 60 dog
13 71.7 37 dog
14 73.0 32 dog
15 82.0 25 dog
16 100.0 0 dog
dat <- structure(list(x = c(100, 90, 84, 55.5, 28.3, 27, 18, 0, 0, 10,
16, 44.5, 71.7, 73, 82, 100), y = c(100, 89, 85, 60, 37, 32,
25, 0, 100, 89, 85, 60, 37, 32, 25, 0), animal = c("cat", "cat",
"cat", "cat", "cat", "cat", "cat", "cat", "dog", "dog", "dog",
"dog", "dog", "dog", "dog", "dog")), class = "data.frame", row.names = c(NA,
-16L), .Names = c("x", "y", "animal"))
library(ggplot2)
ggplot(dat) + theme_bw() +
geom_area(aes(x=x, y=y, group=animal, fill=animal), alpha=.3)
奇怪的是,如果我只对狗进行子集化,它会正确绘制:
ggplot(subset(dat, animal=="dog")) + theme_bw() +
geom_area(aes(x=x, y=y, group=animal, fill=animal), alpha=.3)
最佳答案
我也遇到同样的错误...
另一种可能的解决方法是使用geom_ribbon
。
ggplot(dat) + theme_bw() +
geom_ribbon(aes(x = x, ymin = 0, ymax = y, group = animal, fill = animal, position = "stack"), alpha=.3)
这也给出了正确的情节
关于r - ggplot2 geom_area 意外绘制(在错误的位置结束),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27136177/