亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

gggplot 2-在圖外注釋

gggplot 2-在圖外注釋

尚方寶劍之說 2019-07-02 17:25:36
gggplot 2-在圖外注釋我想把樣本大小值和圖上的點聯系起來。我可以用geom_text將數字定位到接近點的位置,但這是混亂的。如果沿著地塊的外部邊緣把他們排成一排,那就更干凈了。例如,我有:df=data.frame(y=c("cat1","cat2","cat3"),x=c(12,10,14),n=c(5,15,20))ggplot(df,aes(x=x,y=y,label=n))+geom_point()+geom_text(size=8,hjust=-0.5)產生這個情節:我更喜歡這樣的東西:我知道我可以創建第二個情節grid.arrange(A)a la這個職位)但是,確定textGrobs與y軸之間的間距是很繁瑣的。有更簡單的方法嗎?謝謝!
查看完整描述

3 回答

?
LEATH

TA貢獻1936條經驗 獲得超7個贊

你不需要再畫一個情節。你可以用annotation_custom將Grobs定位在繪圖區域內或之外的任何位置。Grobs的定位是根據數據坐標進行的。假設“5”、“10”、“15”與“cat 1”、“cat 2”、“cat 3”對齊,TextGrobs的垂直定位將由三個數據點的y坐標給出。默認情況下,ggplot2剪輯格羅布到繪圖區域,但裁剪可以被覆蓋。需要擴大有關的差額,以便為格羅布會議騰出空間。下面(使用ggplot 2 0.9.2)給出了與第二幅圖類似的圖:

library (ggplot2)library(grid)df=data.frame(y=c("cat1","cat2","cat3"),x=c(12,10,14),n=c(5,15,20))p <- ggplot(df, aes(x,y)) + geom_point() +    
        # Base plot
     theme(plot.margin = unit(c(1,3,1,1), "lines"))   # Make room for the grobfor (i in 1:length(df$n))  {p <- p + annotation_custom(
      grob = textGrob(label = df$n[i], hjust = 0, gp = gpar(cex = 1.5)),
      ymin = df$y[i],      # Vertical position of the textGrob
      ymax = df$y[i],
      xmin = 14.3,         # Note: The grobs are positioned outside the plot area
      xmax = 14.3)
 }    # Code to override clippinggt <- ggplot_gtable(ggplot_build(p))gt$layout$clip[gt$layout$name == "panel"] <- "off"grid.draw(gt)


查看完整回答
反對 回復 2019-07-02
?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

基于grid


require(grid)


df = data.frame(y = c("cat1", "cat2", "cat3"), x = c(12, 10, 14), n = c(5, 15, 20))


p <- ggplot(df, aes(x, y)) + geom_point() + # Base plot

theme(plot.margin = unit(c(1, 3, 1, 1), "lines"))


p


grid.text("20", x = unit(0.91, "npc"), y = unit(0.80, "npc"))

grid.text("15", x = unit(0.91, "npc"), y = unit(0.56, "npc"))

grid.text("5", x = unit(0.91, "npc"), y = unit(0.31, "npc"))


查看完整回答
反對 回復 2019-07-02
  • 3 回答
  • 0 關注
  • 1656 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號