2017-10-16 1 views
1

ggplot2를 사용하여 세 사이트 (각 사이트마다 각 종마다 발생)에 15 종의 백분율 값을 플로팅합니다. 사이트 'C'와 관련된 데이터 포인트가 참조 포인트입니다.ggplot2에서 데이터 포인트 위와 아래에 컬럼을 플롯하는 방법

이제 사이트 'A'와 'B'를 점으로 그리지 않고 수직선이나 기둥 형 구조로 표시하고 싶습니다. 따라서 이러한 데이터 포인트는 사이트 'C'지점의 상단 또는 하단 측면 (즉, 녹색)으로 수직선으로 확장해야합니다. 즉, 값이 기준값보다 크고 하단이 더 작은 값인 경우 상단으로 확장해야합니다.

특히 빨간색 점에서 녹색 점까지의 빨간색 선과 파란색 점에서 녹색 점까지의 파란색 선이 필요합니다. 빨간색 선은 이상 적으로 빨간색 점과 동일한 너비를 가져야합니다 (파란색의 경우도 마찬가지 임). 선이 빨간색과 파란색 점 (녹색 점에 비례)만큼 오프셋되어 선이 겹치지 않도록해야합니다. 마지막으로 선은 중심으로가 아니라 점의 가장자리로 가야합니다.

이 목적을 위해 나는 'A'와 'B'에 대한 간격 띄우기 점을 가지며 크기를 기준점 크기의 절반으로 줄였습니다.

library(ggplot2) 
MyData$species <- as.character(MyData$species) 
MyData$species <- factor(MyData$species, levels=unique(MyData$species)) 

pos <- position_dodge(width=0.21) 
cols <- c("C" = "darkgreen", "B" = "blue", "A" = "red") 

tiff(file = "MyData.tiff", height=10, width=10, units="in", res=300, compression="lzw") 
ggplot(data = MyData, aes(x=species, y=value, group=site, colour=site)) + 
    geom_point(data=subset(MyData, site=="C"), size = 4, shape=15, alpha=1, position=pos) + 

    geom_line(data=subset(MyData, site=="C"), size = 2, lwd=2, alpha=0.4, show_guide=FALSE) + 

    geom_point(data=subset(MyData, site!="C"), size = 1.8, shape=15, alpha=1, position = pos) + 
    scale_colour_manual(values = cols) + 
    xlab("Species") + 
    ylab("Value (%)") + 
    scale_y_continuous(expand=c(0.01,0.01), 
        limits=c(0.0,100), 
        breaks=c(0,20,40,60,80,100), 
        labels=c("0","20","40","60","80","100")) + 
    theme_bw() + 
    theme(legend.position="none") + 
    theme(axis.title.x = element_text(vjust=0.1,face="bold", size=16), 
     axis.text.x = element_text(vjust=0.4, size=14, angle=90, hjust=1.0)) + 
    theme(axis.title.y = element_text(vjust=0.1,face="bold", size=16), 
     axis.text.y = element_text(face="bold", size=14, angle=0)) + 
    theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) + 
    theme(panel.border = element_rect(size=1, color = "black")) + 
    theme(plot.margin = unit(c(0.3,0.4,0.3,0.3),"lines")) 
dev.off() 

이것은 현재 플롯입니다. 그래서 기본적으로 빨강과 파랑 점을 녹색 점까지 겹치지 않는 선으로 대체하고 싶습니다. 이에 대한 우아한 해결책에 어떤 조언을 사전에

enter image description here

많은 감사합니다.

이것은 내 데이터 집합의 dput()입니다.

structure(list(site = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("A", "B", "C"), class = "factor"), 
    species = structure(c(13L, 11L, 2L, 14L, 1L, 9L, 12L, 10L, 
    6L, 8L, 15L, 7L, 3L, 4L, 5L, 13L, 11L, 2L, 14L, 1L, 9L, 12L, 
    10L, 6L, 8L, 15L, 7L, 3L, 4L, 5L, 13L, 11L, 2L, 14L, 1L, 
    9L, 12L, 10L, 6L, 8L, 15L, 7L, 3L, 4L, 5L), .Label = c("Species 1", 
    "Species 10", "Species 11", "Species 12", "Species 13", "Species 14", 
    "Species 15", "Species 2", "Species 3", "Species 4", "Species 5", 
    "Species 6", "Species 7", "Species 8", "Species 9"), class = "factor"), 
    value = c(2, 3.25, 3.53, 4.31, 4.59, 5.26, 6.02, 6.42, 6.6, 
    7.26, 8.89, 12.45, 35.62, 72.42, 73.55, 1.36, 2.36, 2.17, 
    10.34, 6.84, 1.88, 5.09, 7.35, 3.87, 10.55, 6.6, 14.64, 39.57, 
    88.06, 64.54, 5.03, 12.34, 5.42, 3.63, 5.16, 6.04, 3, 8.94, 
    3.28, 7.64, 6.25, 21.96, 39.35, 78.55, 47.35)), .Names = c("site", 
"species", "value"), class = "data.frame", row.names = c(NA, 
-45L)) 
+0

데이터 프레임? –

+0

표시 방법을 표시하십시오. –

+0

사이트 = c (A, B, C)와 같아야합니다. 종 = 종 1-15 및 값 = 0-100 % 사이의 숫자. 나는 내가 현재 몇 분 안에 얻을 수있는 음모를 게시 할 것이다. 고맙습니다! – tabtimm

답변

2

을 그릴 geom_line을 추가 할 수 있습니다 각 사이트/종, & A/B 라인이 지점 C의 각 측면으로 떨어지는 재주문 사이트 :

라이브러리 (dplyr)

MyData <- MyData %>% 
    group_by(species) %>% 
    mutate(value.C = value[site == "C"]) %>% 
    rowwise() %>% 
    mutate(value.min = min(value, value.C), 
     value.max = max(value, value.C)) %>% 
    ungroup() %>% 
    mutate(site = factor(site, levels = c("A", "C", "B"))) 

줄거리 : 당신은뿐만 아니라 C 값이 포함되어 제공 한

# vary dodge width such that the lines drop to the edge of point C 
# for your chosen dimensions (for mine 0.5 was about right) 
pos <- position_dodge(width = 0.5) 

ggplot(data = MyData, 
     aes(x = species, y = value, 
      ymin = value.min, ymax = value.max, 
      group = site, colour = site, size = site)) + 

    geom_linerange(size = 1.8, alpha = 0.4, position = pos) + 

    geom_line(data = subset(MyData, site == "C"), 
      size = 2, lwd = 2, alpha = 0.4) + 
    geom_point(data = subset(MyData, site == "C"), 
      size = 4, shape = 15, position = pos) + 
    scale_color_manual(values = cols) + 
    theme_classic() + 
    theme(legend.position = "none") 
    # + other theme-related settings... 

plot

+0

그것은 훌륭합니다. geom_linerange()에는 size = 2.25, geom_point에는 size_dodge()에 대해 width = 0.155를 사용했습니다. 이렇게하면 빨간색과 파란색 선이 녹색 점과 정확히 일치합니다. 고맙습니다. – tabtimm

1

당신은 당신은 C.

의 Ymin를/Ymax와 값을 정의를 가리 키도록 지점 A/B에서 라인 geom_linerange()을 시도 할 수 있습니다 수직 라인

library(ggplot2) 
MyData$species <- as.character(MyData$species) 
MyData$species <- factor(MyData$species, levels=unique(MyData$species)) 

pos <- position_dodge(width=0.21) 
cols <- c("C" = "darkgreen", "B" = "blue", "A" = "red") 

windows() 
ggplot(data = MyData, aes(x=species, y=value, group=site, colour=site)) + 
    geom_point(data=subset(MyData, site=="C"), size = 4, shape=15, alpha=1, position=pos) + 
    geom_line(data=subset(MyData, site=="C"), size = 2, lwd=2, alpha=0.4, show_guide=FALSE) + 
    geom_point(data=subset(MyData, site!="C"), size = 1.8, shape=15, alpha=1, position = pos) + 
    geom_line(aes(group = species)) + #New code Added 
    scale_colour_manual(values = cols) + 
    xlab("Species") + 
    ylab("Value (%)") + 
    scale_y_continuous(expand=c(0.01,0.01), 
        limits=c(0.0,100), 
        breaks=c(0,20,40,60,80,100), 
        labels=c("0","20","40","60","80","100")) + 
    theme_bw() + 
    theme(legend.position="none") + 
    theme(axis.title.x = element_text(vjust=0.1,face="bold", size=16), 
     axis.text.x = element_text(vjust=0.4, size=14, angle=90, hjust=1.0)) + 
    theme(axis.title.y = element_text(vjust=0.1,face="bold", size=16), 
     axis.text.y = element_text(face="bold", size=14, angle=0)) + 
    theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) + 
    theme(panel.border = element_rect(size=1, color = "black")) + 
    theme(plot.margin = unit(c(0.3,0.4,0.3,0.3),"lines")) 
관련 문제