最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Plotting points and arc on same ggplot - Stack Overflow

programmeradmin3浏览0评论

I am trying to overlay some random points and an arc using ggplot2, but can't seem to achieve both simultaneously.

Here is my code:

library(ggplot2)
library(ggforce)

# Generate random data
x <- runif(20)
y <- runif(20)
df <- data.frame(x = x, y = y)

# Plot points and quarter circle
ggplot(df, aes(x = x, y = y)) +
geom_point(size = 3) +
geom_arc(
  aes(x0 = 0, y0 = 0,   
      r = 1,            
      start = 0,       
      end = pi / 2),   
  color = "red",       
  size = 1             
) 

I think it's a really simple fix. Any ideas?

I am trying to overlay some random points and an arc using ggplot2, but can't seem to achieve both simultaneously.

Here is my code:

library(ggplot2)
library(ggforce)

# Generate random data
x <- runif(20)
y <- runif(20)
df <- data.frame(x = x, y = y)

# Plot points and quarter circle
ggplot(df, aes(x = x, y = y)) +
geom_point(size = 3) +
geom_arc(
  aes(x0 = 0, y0 = 0,   
      r = 1,            
      start = 0,       
      end = pi / 2),   
  color = "red",       
  size = 1             
) 

I think it's a really simple fix. Any ideas?

Share Improve this question asked 2 days ago compbiostatscompbiostats 9939 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

By default layers inherit the mappings from the base ggplot() call. The problem is your geom_arc is inheriting the x and y mappings. You can turn that off with inherit.aes=. But you are also bound to the same data so you are drawing an arch for every point in df. You can pass a NULL dataset to the layer as well,

ggplot(df, aes(x = x, y = y)) +
  geom_point(size = 3) +
  geom_arc(
    aes(x0 = 0, y0 = 0,   
        r = 1,            
        start = 0,       
        end = pi / 2),   
    color = "red",       
    size = 1, inherit.aes = FALSE
  ) 

That gives

发布评论

评论列表(0)

  1. 暂无评论