在ggplot中绘制径向背景

时间:2017-04-23 16:26:31

标签: r ggplot2

这是我的数据集:

#include<unistd.h>
#include <stdio.h>
#include <time.h>
clock_t begin;
clock_t end;
int a[1000],i=0;
void B(int a[],int n)
{
    printf("%d",a[0]); //do your process here.
}
void A()
{
    scanf("%d",&a[i]);
    int p;
    i++;
    end=clock();
    double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    if(time_spent>=15)
    {
        p=fork();
        if(p==0) // child process will execute
        {
            B(a,i);
        }
        else //parent resets the clock
        { 
            begin=clock();
        }
    }
    A(); // This reads infinitely better write base condition to stop.
}
int main()
{

    A();
}

我想绘制与附图中相同的背景,添加到我当前的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"))

enter image description here

我想我应该使用geom_polygon函数,但我真的不知道如何创建数据集来绘制所有必需的段,包括从深灰色到浅灰色和白色的颜色渐变。

也许这可能是一个开始?

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")

1 个答案:

答案 0 :(得分: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