Home > Enterprise >  How to draw circles inside each other with ggplot2?
How to draw circles inside each other with ggplot2?

Time:10-16

I want to draw two circles inside each other with ggplot2.

So far my effort is: make a fake data and plot it with geom_line(). If I convert this with coord_polar() then I will not be able to see two different circles the one inside each other

x1=seq(0,6000000,1000)
y1=rep(1,length(x))
y2=rep(2,length(x))

data=as.data.frame(cbind(x1,y1,y2))

# plot the data
ggplot(data)  
  geom_line(aes(x1,y1))  
 geom_line(aes(x1,y2)) 
 #coord_polar() 

enter image description here

I would avoid the geom_circle option and use the coord_polar option if possible. The reason is that these two circles have some differences in the x-axis, which I would indicate after drawing the circles.

I would like my plot to look like this enter image description here

CodePudding user response:

Why not use two geom_point() with different sizes and pch = 21?

library(ggplot2)

df <- tibble(x = 0, y = 0)  

ggplot(df, aes(x, y))  
  geom_point(pch = 21, size = 50)  
  geom_point(pch = 21, size = 40)  
  theme_void()

enter image description here

CodePudding user response:

The code you have with coord_polar() is correct, just the plot limits need adjusting to see both the circles, e.g.

ggplot(data)  
  geom_line(aes(x1,y1))  
  geom_line(aes(x1,y2))  
  coord_polar()   ylim(c(0,NA))

Output plot

The reason for using ylim is that this is the direction getting transformed to the radius by the coord_polar()

  • Related