负值的填充区域图

时间:2018-03-02 18:06:46

标签: r ggplot2

我正在尝试绘制ggplot中的填充区域图,以创建湖泊的测深。表面为零(y轴顶部),每个深度记录为负。绘制时,填充区域与我想要的相反。我的问题是,如何翻转填充区域? 在图片中,黑色是水,我想要未填充,透明的是湖底。 提前谢谢!

    df <-read.table("x.txt",header=T)
    library(ggplot2)
    p2 <- ggplot() + geom_area(aes(y = depth, x = distance), data = df, 
       stat="identity")
    p2

depth   distance
-7  100
-7  200
-7  300
-8  400
-8  500
-9  600
-9  700
-8  800
-8  900
-7  1000
-6  1100
-5  1200

添加代码和数据:

     library(akima)                                                    
     attach(df)
     names(df)
     fld <- with(df, interp(x = distance, y = depth, z = temperature))
     filled.contour.ungeoreferenced <-   (filled.contour(x = fld$x,
     y = fld$y,z = fld$z,  color.palette =
     colorRampPalette(c("blue", "green", "yellow",                                      
     "orange", "red")),   xlab = "Distance",
     ylab = "Depth",key.title = title(main = "Temp", cex.main = 1)))  



depth   distance    temperature
0   100 13
0   1200    13
-1  100 11
-1  1200    11
-2  500 11
-2  600 11
-2  700 11
-2  800 11
-3  1000    10
-3  1100    10
-3  1200    10
-4  100 10
-4  200 10
-4  300 10
-5  100 8
-5  1200    7
-6  100 7
-6  1200    0.1
-7  100 7
-7  1200    0.1
-8  500 6
-8  600 5
-9  100 0.1
-9  600 5
-9  1200    0.1

1 个答案:

答案 0 :(得分:1)

您可以在背景中绘制黑色geom_rect(从-InfInf),以表示底部和顶部的蓝色geom_area

library(ggplot2)
ggplot(df, aes(distance, depth)) + 
    geom_rect(aes(xmin = -Inf, xmax = Inf, 
                  ymin = -Inf, ymax = 0), fill = "black") + 
    geom_area(fill = "blue") +
    theme_classic()

enter image description here

数据:

# df
structure(list(depth = c(-7L, -7L, -7L, -8L, -8L, -9L, -9L, -8L, 
-8L, -7L, -6L, -5L), distance = c(100L, 200L, 300L, 400L, 500L, 
600L, 700L, 800L, 900L, 1000L, 1100L, 1200L)), .Names = c("depth", 
"distance"), row.names = c(NA, -12L), class = "data.frame")