ggplot annotation in fixed place in the chart

别说谁变了你拦得住时间么 提交于 2021-02-05 05:01:46

问题


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")

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?


回答1:


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)




回答2:


Extending @user20650's idea, textGrob allows the use relative coordinates, but use annotation_custom to restrict the grob to the plot panel.

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)



来源:https://stackoverflow.com/questions/37813655/ggplot-annotation-in-fixed-place-in-the-chart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!