ggplot批注在图表中的固定位置 [英] ggplot annotation in fixed place in the chart

查看:43
本文介绍了ggplot批注在图表中的固定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am plotting the following data into a ggplot bar chart.

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值不断变化,并且图表顶部的注释有时会消失,或者在其他情况下会出现在图表的中间.

I am using the following code to render the chart. It is working fine. However the problem is the yaxis values keeps changing and the annotation at the top of the chart sometimes disappears or in other instances appears in the middle of the chart.

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)

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

Is it possible to dynamically set the position of the annotation?

推荐答案

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

Instead of adjusting the position using hjust and vjust why not set the y-position to the tallest bar?

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)

这篇关于ggplot批注在图表中的固定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆