【问题标题】:Insert multiple annotated polygons in ggplot background在 ggplot 背景中插入多个带注释的多边形
【发布时间】:2018-08-14 10:22:10
【问题描述】:

我正在尝试使用 ggplot2 制作图表,我将其设想为这两个图的组合:

  1. 显示我的每种作物的生长季节: here

  2. 另一个显示三年研究中每月的图像数量:here

本质上,我想要的是第一张图片在背景中,所有数据都在前面。

到目前为止我尝试了什么:

  • 使用第一张图片作为 ggplot 的背景。

  • 使用+ geom_poly() 或胖+ geom_segment()

但无法像我希望的那样无缝地工作。

点数据如下所示:

input2 <- read.table(
    text = "rowname key variable  value
    1    January     2016   8233
    2   February     2016  11573
    3      March     2016  23016
    4      April     2016  32029
    5        May     2016  51280
    6       June     2016  30652
    7       July     2016  62877
    8     August     2016  80680
    9  September     2016  60807
    10   October     2016  74004
    11  November     2016  60995
    12  December     2016  39608",
    header = T,
    stringsAsFactors = F)

我的情节代码如下所示:

library(ggplot2)

ggplot(input2, aes(x=key, y=value, color=variable))+
  geom_point(aes(size=value)) +
  scale_x_discrete(limits=c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")) +
  #annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  #geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "segment"), data = grass) +
  scale_colour_manual(values=c("#CC6600", "#999999", "#FFCC33")) +
  ggtitle("Number of Images per month\n") +
  labs(x="Month",y="Number of images", fill="Legend")

注释掉的东西是我不成功的尝试。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以像这样使用geom_rectgeom_text 来接近您的理想情节。

    potato_season <- data.frame(
        crop = c("potato", "yam"),
        start = c("January", "March"),
        end = c("August", "October"),
        y_pos = c(20000, 24000)
    )
    library(ggplot2)
    ggplot(input2, aes(x=as.factor(key), y=value, color=as.factor(variable))) +
    
        geom_rect(data = potato_season, inherit.aes = F,
                  aes(xmin = start, xmax = end,
                      ymin = y_pos - 2000, ymax = y_pos + 2000,
                      fill = crop)) +
        geom_text(data = potato_season, inherit.aes = F,
                  aes(label = crop, y = y_pos, x = start),
                  hjust = -1) + # to shift the text right
    
        geom_point(aes(size=value)) +
        scale_x_discrete(limits=c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")) +
        scale_colour_manual(values=c("#CC6600", "#999999", "#FFCC33")) +
        ggtitle("Number of Images per month\n") +
        labs(x="Month",y="Number of images", fill="Legend")
    

    不确定您所指的无缝颠簸是什么,但希望这会有所帮助。如果您在实施时遇到困难,请告诉我。

    【讨论】:

    • 太棒了!几乎一针见血!想知道您是否有一个建议,即如何考虑生长季节不是在新月份的开始或在最后一个月结束时开始的事实,但有时会在月中的某个地方开始和停止。谢谢!
    • @Momchill 真正做到这一点的方法是将您的数据转换为Date 类并在日期轴上绘制图表,这样您就可以指定一个月中的任何一天。执行此操作的 hacky 方法是使用十进制数字(ggplot 必须将基础数值分配给离散轴,所以一月是 1,二月是 2,依此类推。从 1.5 开始你的矩形会将起点放在两者之间)。
    • @JanBoyer,感谢您的建议。我最终使用了日期,这对于分开的生长季节也很有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多