ggplot2和facet_grid:为每个图添加最大值

皮埃尔·杜德克

我正在使用facet_grid()绘制每组数据划分的多个图。对于每个图,我想在角落添加Y轴的最大值。我已经尝试了几次破解,但从未给我预期的结果。这个答案部分地帮助了我,但是我想增加的价值将不断变化,因此我看不到如何应用它。

这是一个最小的示例,我想在下图中添加红色数字:

library(ggplot2)

data <- data.frame('group'=rep(c('A','B'),each=4),'hour'=rep(c(1,2,3,4),2),'value'=c(5,4,2,3,6,7,4,5))

ggplot(data,aes(x = hour, y = value)) +
  geom_line() +
  geom_point() +
  theme(aspect.ratio=1) +
  scale_x_continuous(name ="hours", limits=c(1,4)) +
  scale_y_continuous(limits=c(1,10),breaks = seq(1, 10, by = 2))+
  facet_grid( ~ group)

在此处输入图片说明

谢谢你的帮助!

休戈德堡

这可以解决问题。如果始终具有固定范围,则可以手动放置文本。

library(ggplot2)

data <- data.frame('group'=rep(c('A','B'),each=4),'hour'=rep(c(1,2,3,4),2),'value'=c(5,4,2,3,6,7,4,5))

ggplot(data,aes(x = hour, y = value)) +
    geom_line() +
    geom_point() +
    geom_text(
        aes(x, y, label=lab),
        data = data.frame(
            x=Inf,
            y=Inf,
            lab=tapply(data$value, data$group, max),
            group=unique(data$group)
        ),
        vjust="inward",
        hjust = "inward"
    ) +
    theme(aspect.ratio=1) +
    scale_x_continuous(name ="hours", limits=c(1,4)) +
    scale_y_continuous(limits=c(1,10),breaks = seq(1, 10, by = 2))+
    facet_grid( ~ group)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ggplot2:添加用于facet_grid的变量的名称

结合 facet_grid (ggplot2) 和 denscomp (fitdistrplus)

使用每个变量的最大值创建ggplot2图的R函数是什么?

在 facet_grid 中将 label_both 设置为 ggplot2 中主题的默认值

ggplot2中的facet_wrap()和facet_grid()有什么区别?

如何在ggplot2中使用facet_grid制作甜甜圈图?

如何使用facet_grid(ggplot2)在每个构面上绘制多条线

使用ggplot facet_grid R图添加图例

在ggplot2中的特定构面之间添加空间(facet_grid)

在某些图中添加y = 0线facet_grid ggplot2

ggplot2:在 facet_grid 中按组添加水平线

如何在ggplot2中按组为facet_grid着色?

如何在ggplot2 :: facet_grid中格式化网格标题和条目

ggplot2中的facet_grid错误

ggplot2:如何获取facet_grid()的labeller = label_both和facet_wrap()的ncol选项的合并功能?

重新排列facet_grid并将其添加到ggplot2的geom_bar中

如何在gpplot面板(facet_grid)中提取stat_smooth曲线最大值?

ggplot2 - facet_grid:如何沿 Y 轴设置不同范围的值但零对齐?

ggplot2的facet_grid内部元素具有多个重复值的不同重排序

如何將 ggplot2 geom_col 與 facet_grid 和 coord_flip 一起使用

ggplot2:具有coord_flip()和自由刻度的geom_pointrange()facet_grid()

R Shiny ggplot2 折线图在使用“key”属性和 facet_grid 时不会显示线条

让facet_grid中的每个图都有自己的Y轴值

如何在ggplot2中自定义中间值和最大值的颜色?

每条刻面线具有不同 y 轴的 ggplot2 刻面:如何从 facet_grid 和 facet_wrap 中获得“最佳”?

ggplot2:geom_violin(),geom_text()与facet_grid()一起打印每个因素中不包括NA的总行数

使用facet_grid从ggplot中提取单个图

如何使用ggplot2 facet_grid注释没有数据的构面?

ggplot2 geom_rect在facet_grid下无法适当缩放