gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:32:52 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com