r - ggplot2 geom_area 意外绘制(在错误的位置结束)

标签 r ggplot2

我正在使用 ggplot2 使用 geom_area 绘制以下数据。这些值被设计为 x 轴镜像。但《狗》情节的结局并没有反射(reflect)出来。

enter image description here

但是从这个数据帧中可以看出,狗应该以 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)  

enter image description here

最佳答案

我也遇到同样的错误... 另一种可能的解决方法是使用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/

相关文章:

r - 在地理上聚合 worldclim 的单元格,使用 30 秒分辨率来估计 10 分钟分辨率的标准偏差

R 预订 : Tabbed headings

从具有多个方面的 ggplot2 中的聚集条形图中删除空因子

r - 如何在ggplot中反转点大小?

python - 使用每月的第一个交易日将每日 Pandas 股票数据转换为每月数据

performance - R 中的向量化矩阵运算

r - 将单行表转换为 data.frame

r - 使用 sf、ggplot 和 cut_interval() 的 Choropleth 中的组数错误

r - 在 "top"轴上对齐倾斜轴文本

r - ggplot2 的默认图边距是多少?