2016-06-28 2 views
0

ggplot2에 새로운 geom을 만들어서 많은 수의 선을 그려야합니다. 그러나, 내 문제는 그려진 선이 정확하지 않다는 것입니다. 다음은 내 문제에 대한 간단한 설명입니다. 이 예ggplot2에서 새 기하 구조를 만들 때 데이터를 축척하는 방법은 무엇입니까?

GeomLine1 <- ggproto("GeomLine1", Geom, 
         required_aes = c('x','y'), 
         default_aes = aes(colour = "black"), 
         draw_key = draw_key_abline, 

         draw_panel = function(data, panel_scales, coord) { 

          grid::linesGrob(x=data$x,y=data$y,default.units = 'native')} 
) 

geom_line1 <- function(mapping = NULL, data = NULL, stat = "identity", 
          position = "identity", na.rm = FALSE, show.legend = NA, 
          inherit.aes = TRUE, ...) { 
     layer(
      geom = GeomLine1, mapping = mapping, data = data, stat = stat, 
      position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
      params = list(na.rm = na.rm, ...) 
    ) 
} 

이것은 선을 그어야한다 geom_line1라는 새로운 기하 구조입니다 고려. 이 코드의

dat <- data.frame(x = c(0,10), y = c(0,10)) 

ggplot(dat,aes(x,y))+geom_line1() + geom_point() 

출력

enter image description here

당신은 선이 그 위치에있는 점 이상으로 확대하고 볼 수 있습니다. 기본 기능 geom_line을 사용하면 완벽합니다. 내 코드에서 무엇이 잘못 되었습니까? 또한 누군가가 새로운 기하학을 작성하기위한 좋은 지침서를 제안 할 수 있다면 좋을 것입니다.

ggplot(dat,aes(x,y))+geom_line() + geom_point() 

enter image description here

+0

[튜토리얼 (http://docs.ggplot2.org/dev/vignettes/extending-ggplot2.html). – Axeman

+0

@Axeman 나는 그 행운을 읽었습니다. 감사합니다. – Koundy

+0

'geom_line1'과'geom_line'의 소스를 비교 했습니까? – Axeman

답변

0

난 그냥 문제를 알아낼 수 있었다. 사실 우리는 이와 같은 데이터를 변환하고 플로팅을 위해 변형 된 데이터를 사용해야합니다.

coords <- coord$transform(data, panel_scales) 
grid::linesGrob(x=coords$x,y=coords$y,default.units = 'native') 

대신 data$xdata$y

우리해야 $ 좌표의 X 및 Y 좌표를 $

관련 문제