【问题标题】:ggplot annotation in fixed place in the chart图表中固定位置的ggplot注释
【发布时间】:2016-06-14 13:35:40
【问题描述】:

我正在将以下数据绘制成 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)

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

【问题讨论】:

  • 建议,可以把所有的主题放到一个theme()中,代码会更干净。
  • @zx8754 谢谢你的建议一定会的。
  • 你能提供更多关于你想要什么的信息吗?您希望注释测试“来源:ABC Monitors”不动吗?是否要将其设置为 y 轴的固定百分比?
  • @MikeyMike 上面给出的当前代码在屏幕右上角以蓝色打印注释“Source:ABC Monitors”。这是因为 hjust 和 vjust 参数。但是,垂直对齐参数取决于 y 轴的值。每当 yaxis 的值发生变化时,注释就会被折腾。我的问题是我们是否可以设置 vjust 和 hjust 的动态参数,以便无论 x 轴和 y 轴值以何种方式摆动,注释都会出现在同一位置。
  • @zx8754 是一个很棒的博客 zx8754...我昨天自己浏览了它,但不得不赶时间,因此无法感谢你。它确实是一个很好的资源...谢谢。我尝试使用标题...但由于我的代码中存在一些空白,它没有呈现......但肯定会再试一次,因为它真的值得......谢谢。

标签: r ggplot2


【解决方案1】:

扩展@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)

【讨论】:

  • 非常感谢桑迪·穆斯普拉特。我确实尝试使用带有注释自定义的 textGrob 来自 SO 中的示例 - 但再次没有尝试在标签本身中指定 x 和 y 轴值......尽管我必须承认我从未使用过 textGrob。我发现 darren7 的建议非常简单,并且已经使用了它。非常感谢 Sandy Muspratt 的建议。
  • 不用担心。 Darren's 是一个简单的解决方案。
【解决方案2】:

不使用 hjust 和 vjust 调整位置,为什么不将 y 位置设置为最高条?

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 = max(b$RENTAL), 
                    color = "blue",size = 3,fontface = "italic",hjust = 1, vjust = 1)

【讨论】:

  • 非常感谢 darren17...我确实尝试过重新调整 y 轴 - 但用 vjust 搞砸了......这当然有效。再次感谢您。
猜你喜欢
  • 2011-09-17
  • 1970-01-01
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 2022-06-21
  • 1970-01-01
相关资源
最近更新 更多