r - 在ggplot中绘制径向背景

标签 r ggplot2

这是我的数据集:

> 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 代码中:

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 )

最佳答案

这是一种使用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

关于r - 在ggplot中绘制径向背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43573651/

相关文章:

在 data.table 中四舍五入为倍数并过滤

r - 如何保存在 Shiny 的应用程序中制作的绘图

r - 如何叠加多个图

r - 如何在r中的箱线图之间创建单独的线图

r - 如何处理ggplot2中的垂直渐近线

r - 使用用户定义的函数在 R 中聚合数据

r - 将数字连接到字符串输出中,格式化每个数字以采用相同的字符宽度

r - ggplot2 中的绘图显示没有灰色背景

r - 通过 rbind 添加行到 data.frame 会导致日期错误

r - 为什么神经网络在一个简单的分类案例中失败