2017-04-23 6 views
2

で放射状の背景を描く:私の現在のggplotコードに追加、私は、添付の図のように同じ背景を描きたいが、これは私のデータセットですggplot

> dput(dfw) 
structure(list(SITE = c("ASPEN", "ASPEN", "BioCON", "DUKE", "Lancaster", 
"Merrit Island", "Nevada FACE", "NZ", "ORNL", "PHACE", "BioCON" 
), SPECIES = c("A", "AB", "Legume", "PITA", "mixed", "Oak", "desert", 
"grassland", "SG", "grassland", "C3forb"), FRr = c(0.197028535345918, 
0.296799297050907, 0.195436310641759, 0.152972526753089, 0.0313948973476966, 
0.139533057346518, 0.188221278921143, NA, 0.70542764380006, 0.119320766735777, 
0.135665667633474), Nupr = c(0.122177669046786, 0.305573297532757, 
0.131181914007488, 0.217519050530067, -0.0436788294371676, 0.153632658941404, 
-0.00803217169726427, 0.168440046857285, 0.145172439177718, -0.108563178158001, 
0.00546006390438276), myc = c("ECM", "ECM", "N-fixing", "ECM", 
"ECM", "ECM", "AM", "AM", "AM", "AM", "AM"), SITE_Sps = structure(c(1L, 
2L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 3L), .Label = c("Aspen FACE-A", 
"Aspen FACE-AB", "BioCON", "BioCON-legumes", "Duke FACE", "Lascaster", 
"Florida OTC", "Nevada FACE", "NZ FACE", "ORNL FACE", "PHACE" 
), class = "factor")), row.names = c(NA, -11L), vars = list(SITE, 
    SPECIES, myc), indices = list(0L, 1L, 10L, 2L, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
    SITE = c("ASPEN", "ASPEN", "BioCON", "BioCON", "DUKE", "Lancaster", 
    "Merrit Island", "Nevada FACE", "NZ", "ORNL", "PHACE"), SPECIES = c("A", 
    "AB", "C3forb", "Legume", "PITA", "mixed", "Oak", "desert", 
    "grassland", "SG", "grassland"), myc = structure(c(2L, 2L, 
    1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("am", "ecm", 
    "ecm+am"), class = "factor")), row.names = c(NA, -11L), class = "data.frame", vars = list(
    SITE, SPECIES, myc), .Names = c("SITE", "SPECIES", "myc")), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), .Names = c("SITE", "SPECIES", 
"FRr", "Nupr", "myc", "SITE_Sps")) 

ggplot(dfw, aes(FRr, Nupr, group=myc, label = SITE_Sps)) + 
    geom_point(aes(fill=myc),size=4,shape = 21) + 
    geom_text() + 
    geom_hline(yintercept=0) + geom_vline(xintercept = 0) + 
    geom_abline(intercept = 0, slope = 1, linetype = "longdash") 

enter image description here

私はgeom_polygon関数を使うべきだと思いますが、暗い灰色から明るい色のグラデーションを含む必要なすべてのセグメントを描画するためのデータセットを作成する方法は本当に分かりませんレイとホワイト。

おそらくこれはスタートになる可能性がありますか?ここで

nlines <- 

    phis <- seq(0, 2*pi, by=2*pi/nlines) 
    rad <- 999 

    xs <- rad * cos(phis) 
    ys <- rad * sin(phis) 

答えて

3

geom_polygonを使用しての方法である:ここでは

nlines <- 25 
inc <- pi/(nlines) 
phis <- seq(-pi/2, by=inc, length.out = nlines) 
rad <- 1 

#Create the triangles 
points <- lapply(phis, function(a) { 
    x <-c(0, rad*cos(a), rad*cos(a+inc),0, -rad*cos(a), -rad*cos(a+inc)) 
    y <-c(0, rad*sin(a), rad*sin(a+inc),0, rad*sin(a), rad*sin(a+inc)) 
    g <-c(a,a,a,a,a,a) # used for grouping 
    data.frame(x,y,g) 
}) 

#Create a data.frame to be used on ggplot 
bckg <- do.call(rbind,points) 

#You need to set the data for each geometry as we have more than one dataset 
ggplot(mapping=aes(FRr, Nupr, group=myc)) + 
    #Draw the background 
    geom_polygon(data=bckg,aes(x=x,y=y,group=g,alpha=g), fill = "gray50")+ 

    geom_point(data=dfw, aes(FRr, Nupr, group=myc, fill=myc),size=4,shape = 21) + 
    geom_text(data=dfw, aes(FRr, Nupr, group=myc, label = SITE_Sps), nudge_y = -0.02) + 
    geom_hline(data=dfw,yintercept=0) + geom_vline(data=dfw,xintercept = 0) + 
    geom_abline(data=dfw,intercept = 0, slope = 1, linetype = "longdash")+ 

    #We need to define a scale in ourder to deal with out of boundary points on the background 
    scale_x_continuous(limits = c(-0.2,0.4), oob=function(x, rg) x)+ 
    scale_y_continuous(limits = c(-0.2,0.4), oob=function(x, rg) x)+ 
    scale_alpha_continuous(guide="none", range=c(1.0,0))+ 
    theme(panel.background = element_blank()) 

はプロットである: enter image description here

+0

これは素晴らしいです!どのようにして放射状の線分を現在のように180度でなく360度に描くことができますか?完全なデータセットには、x <0 –

+0

@ fede_luppiのポイントがあります。ポスト上の画像には、対称的な背景があります。ポリゴンをx軸に反映させるだけです。ソリューションを更新しました。 – Marcelo

関連する問題