我正在使用ggplot2来绘制以下数据geom_area.这些值被设计为ax轴镜像.但是狗的情节结束并没有反映出来.

但正如你从这个数据框架中看到的那样,狗应该在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)
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我只是将它的子集正确地绘制它:
ggplot(subset(dat, animal=="dog")) + theme_bw() +
geom_area(aes(x=x, y=y, group=animal, fill=animal), alpha=.3)
Run Code Online (Sandbox Code Playgroud)

我不知道为什么你会得到这个意想不到的结果。我也有同样奇怪的情节。
一个可能的解决方法:
ggplot() +
geom_area(data=dat[dat$animal=="dog",], aes(x=x, y=y, fill="red"), alpha=.3) +
geom_area(data=dat[dat$animal=="cat",], aes(x=x, y=y, fill="blue"), alpha=.3) +
scale_fill_discrete("Animal",breaks=c("red","blue"),labels=c("dog","cat")) +
theme_bw()
Run Code Online (Sandbox Code Playgroud)
这给出了所需的结果:

| 归档时间: |
|
| 查看次数: |
804 次 |
| 最近记录: |