r - 图表中固定位置的ggplot注释

标签 r ggplot2

我正在将以下数据绘制到 ggplot 条形图中。

structure(list(MEDIATYPE = c("BACKLIT TOWER", "BILLBOARDS", "BRIDGE PANEL", 
"BUILDING FACADES", "BUS SHELTER", "CANTILIVERS", "CYCLE SHELTER", 
"FOB", "FREE STANDING PANEL", "GANTRIES"), RENTAL = c(197, 278363, 
1423, 26, 35960, 6194, 70, 4845, 27, 9420)), .Names = c("MEDIATYPE", 
"RENTAL"), row.names = c(NA, 10L), class = "data.frame")

我正在使用以下代码来呈现图表。它工作正常。然而问题是 yaxis 值不断变化,图表顶部的注释有时会消失或在其他情况下出现在图表中间。
library(ggplot2)
library(stringr) # str_wrap


ggplot(b, aes(x=reorder(MEDIATYPE,-RENTAL), y=RENTAL, fill=MEDIATYPE)) + geom_bar(stat = "identity", width = 0.8) +
    theme(legend.position = "none") + xlab("MEDIATYPE") + ylab("SPENDS") +
    scale_x_discrete(labels = function(x) str_wrap(x, width = 1)) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    geom_text(aes(label=RENTAL), vjust = 0.5,hjust = 1, angle = 90, colour = "white",size = 3) + 
    ggtitle("MEDIAWISE SPENDS") + 
    theme(plot.title=element_text(size=rel(1.4), lineheight = 1, face = "bold")) +
    theme(axis.text = element_text(size = 8, color = "black")) +
    theme(axis.title = element_text(size=10, face = "bold")) + 
    theme(panel.background = element_rect(fill = "grey95"))  +
    ggplot2::annotate(geom = "text", label = "Source:ABC Monitors", x = Inf, y = -Inf, color = "blue",size = 3,fontface = "italic",hjust = 1, vjust = -30)

是否可以动态设置注释的位置?

最佳答案

扩展@user20650 的想法,textGrob允许使用相对坐标,但使用 annotation_custom将 grob 限制在绘图面板上。

b = structure(list(MEDIATYPE = c("BACKLIT TOWER", "BILLBOARDS", "BRIDGE PANEL", 
"BUILDING FACADES", "BUS SHELTER", "CANTILIVERS", "CYCLE SHELTER", 
"FOB", "FREE STANDING PANEL", "GANTRIES"), RENTAL = c(197, 278363, 
1423, 26, 35960, 6194, 70, 4845, 27, 9420)), .Names = c("MEDIATYPE", 
"RENTAL"), row.names = c(NA, 10L), class = "data.frame")

# Try a different y range
#      b[2, 2] = 30000

library(ggplot2)
library(stringr) # str_wrap
library(grid)

label = textGrob(label = "Source:ABC Monitors", x = .95, y = 0.95, 
      just = c("right", "top"),
      gp=gpar(fontface = "italic", col = "blue",size = 3))

p = ggplot(b, aes(x=reorder(MEDIATYPE,-RENTAL), y=RENTAL, fill=MEDIATYPE)) + geom_bar(stat = "identity", width = 0.8) +
    theme(legend.position = "none") + xlab("MEDIATYPE") + ylab("SPENDS") +
    scale_x_discrete(labels = function(x) str_wrap(x, width = 1)) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    geom_text(aes(label=RENTAL), vjust = 0.5,hjust = 1, angle = 90, colour = "white",size = 3) + 
    ggtitle("MEDIAWISE SPENDS") + 
    theme(plot.title=element_text(size=rel(1.4), lineheight = 1, face = "bold")) +
    theme(axis.text = element_text(size = 8, color = "black")) +
    theme(axis.title = element_text(size=10, face = "bold")) + 
    theme(panel.background = element_rect(fill = "grey95"))  +
    annotation_custom(label, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)

enter image description here

关于r - 图表中固定位置的ggplot注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37813655/

相关文章:

从字符串中删除第 n 个空格

python - 在 R 中做 dt[,y :=myfun(x), by=list(a,b,c)] 的 pythonic 方法是什么?

r - 如何将日期和时间转换为数值

r - 跨共享公共(public)列分配 groupid

r - 解析ggplot2的注释中的两个符号(==4==2*2)

r plm时间和个体固定效应 - "twoways"与因子(索引)时间

r - 如何避免 ggplot2 条形图中条形之间的间距不均匀?

r - 自定义函数中的 for 循环以创建 ggplot 时间序列图

r - 如何替换 geom_text 指南(图例)的图例 'bullet'

r - 增加 ggplot2 中图例演示的中断