2014-02-20 3 views
9

내 데이터로 이와 같은 작업을했는데 투명성에도 불구하고 세그먼트의 시작과 끝을보기 위해 세그먼트를 시각화하기가 어렵습니다 (내 데이터의 세그먼트 수가 아래 예보다 많습니다).geom_segments를 지터/닷지 (zitter/dodge)하여 병렬로 유지하는 방법은 무엇입니까?

require(ggplot2) 
ggplot(iris, aes(x = Petal.Length, xend = Petal.Width, 
       y = factor(Species), yend = factor(Species), 
       size = Sepal.Length)) + 
    geom_segment(alpha = 0.05) + 
    geom_point(aes(shape = Species)) 

해결책은 있지만, 선은 십자형이다. 팁에 포인트가있는 평행선을 생성하는 방법이 있습니까? position_jitter 대신 position_dodge을 시도했지만 ymax이 필요합니다. ymaxgeom_segment과 함께 사용할 수 있습니까?

ggplot(iris, aes(x = Petal.Length, xend = Petal.Width, 
       y = factor(Species), yend = factor(Species))) + 
    geom_segment(position = position_jitter(height = 0.25))+ 
    geom_point(aes(size = Sepal.Length, shape = Species)) 

답변

9

내가 아는 한 geom_segment은 지터 링이나 피할 수 없습니다. 데이터 프레임의 관련 변수에 jittering을 추가 한 다음 jittered 변수를 플롯 할 수 있습니다. 귀하의 예에서는 요소가 숫자로 변환 된 후 계수 레벨의 레이블이 scale_y_continuous을 사용하여 축에 추가됩니다.

enter image description here

library(ggplot2) 
iris$JitterSpecies <- ave(as.numeric(iris$Species), iris$Species, 
    FUN = function(x) x + rnorm(length(x), sd = .1)) 

ggplot(iris, aes(x = Petal.Length, xend = Petal.Width, 
       y = JitterSpecies, yend = JitterSpecies)) + 
    geom_segment()+ 
    geom_point(aes(size=Sepal.Length, shape=Species)) + 
    scale_y_continuous("Species", breaks = c(1,2,3), labels = levels(iris$Species)) 
하지만 geom_linerange을 보인다는 닷징 수 있습니다.

ggplot(iris, aes(y = Petal.Length, ymin = Petal.Width, 
       x = Species, ymax = Petal.Length, group = row.names(iris))) + 
     geom_point(position = position_dodge(.5)) + 
    geom_linerange(position = position_dodge(.5)) + 
    coord_flip() 

enter image description here

+0

많은 들으! 두 이미지는 내가 원했던 것과 같습니다. 그것을 시도 할 것이다. – Anto

+0

첫 번째 해결책은 나를 위해 일했습니다. 내 데이터를 사용하면 세그먼트의 길이는 시간 지속 시간을 나타내며 geom_linerange를 파셋으로 사용하면 (비늘 = '자유') 축은 y 축의 전체 범위와 각 패널의 범위 만 표시합니다. ymin 및 ymax 설정 그래서 geom_segment에 들어갔습니다. :) – Anto

+1

@Sandy Muspratt'geom_segment'의 피하는 동작에 대한 문서를 알고 계십니까? ("알고있는 한, geom_segment는 지터거나 피하는 것을 허용하지 않습니다")? _vertical_ dodging에서 [** this post **] (https://groups.google.com/forum/#!searchin/ggplot2/geom_segment$20position_dodge/ggplot2/iWojkpMicRY/ZMR75OOpHBQJ)를 보았지만 비슷한 문제가 있습니다. 수평 도요 퇴치의 경우 :'y'는 회피되지만'yend'는 회피되지 않습니다. 어쨌든'geom_segment'가'position' 인수를 가지고 있다는 것은 다소 수수께끼입니다 ... – Henrik

관련 문제