0

I try to use ggplot to plot quarted circles to visualize contour plots but I get misconfigured plot using geom_area (following this tutorial on stacked area with ggplot2)

The code I tried reads

library(ggplot2)
library(dplyr)

N <- 1E2
r <- rev(c(1,2,4,7))
maxXY = max(r)+.25*max(r)
grupp <- c("0","0.25","0.5","0.75")
datalist = list()
plot(0,0,xlim=c(0,maxXY),ylim=c(0,maxXY))
for (i in 1:length(r)) {
  quadX <- seq(from = 0,to = r[i],length.out = N) # calculate x coords
  quadY <- sqrt(r[i]^2 - quadX^2)                 # calculate y coords
  lines(quadX,quadY)

  # data for ggplot
  dat <- data.frame(X = quadX, Y = quadY)
  dat$group <- grupp[i] 
  datalist[[i]] <- dat # add it to your list
}
DF = do.call(rbind, datalist)

# stacked area chart
p1 <- ggplot(DF, aes(x=X, y=Y, fill=group)) +
  geom_area(alpha=0.6 , size=1, colour="black")
plot(p1)

and I get quarter circles plotted correctly with basic plot enter image description here

but a weird one with geom_area enter image description here

Any help would be very appreciated. MJS

EDIT: using Z.Lin's suggestions I get the correct plot, thanks! enter image description here

mjs
  • 150
  • 1
  • 20
  • 1
    Given your data, I don't think you actually want to stack the different areas on top of each other. Specify `position = "identity"` (instead of the default "stack") inside `geom_area()` and your plot should look fine. – Z.Lin Dec 31 '19 at 08:32
  • @Z.Lin do you have by any chance idea how to plot 2D CDF contour plots (middle two cases) as asked in https://stackoverflow.com/questions/59329270/how-to-plot-non-standard-contour-plots-in-r-or-matlab – mjs Dec 31 '19 at 11:46

0 Answers0