簡體   English   中英

Ggplot2 中的左/右和底/頂軸操作

[英]Left/right and bottom/top axes manipulation in Ggplot2

我正在嘗試使用“axis.line.y.right”操縱右 Y 軸,但軸沒有改變。 我使用了以下主題,但只有左側 Y 軸和底部 X 軸根據所需參數更改了顏色和大小。 我不知道我做錯了什么。 太感謝了。

 C57.sum
 Genotype Group Strain   my_mean CI.plus CI.minus  my_n
 WT       C57BL C57BL/6J   0.403   0.482    0.319    12





my.theme = list(theme(axis.line.y.left = element_line(size = 3, color = "red"),
                      axis.line.y.right = element_line(size = 3, color = "red"),
                      axis.line.x.top =  element_line(size = 3, color = "red"),
                      axis.line.x.bottom = element_line(size = 3, color = "red")),
                ylim(0,5))


Plot <- ggplot() +
  
  geom_bar(data = C57.sum, 
           aes(y = my_mean, x = Strain, fill=Group),
           color="black", stat="identity", width = 0.6, show.legend = FALSE,
           position = position_dodge2(preserve = "single")) + 
  
  geom_linerange(data = C57.sum, 
                 aes(x = Strain, ymin = CI.minus, ymax = CI.plus),
                 color = "black", size = 0.42, 
                 position = position_dodge2(preserve = "single", width = 0.6)) +
  

  geom_text(data = C57.sum, 
            aes(y = 0, x = Strain, group = Group, label=paste0("n = ",my_n), vjust=1.4),
            size = 2.8, fontface = "italic", position = position_dodge(0.6)) +
  
  coord_fixed(ratio=4/5) +
  my_theme.2

  Plot

在影響這些軸的主題元素生效之前,您必須先在右側和頂部添加軸。 如果沒有生成頂軸或右軸,則不會繪制它們。 下面是兩個使用guides() function 和sec.axis arguments 的示例。

library(ggplot2)

my.theme = list(theme(axis.line.y.left = element_line(size = 3, color = "red"),
                      axis.line.y.right = element_line(size = 3, color = "red"),
                      axis.line.x.top =  element_line(size = 3, color = "red"),
                      axis.line.x.bottom = element_line(size = 3, color = "red")))


ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  guides(x.sec = "axis", y.sec = "axis") +
  my.theme


ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  scale_x_continuous(sec.axis = sec_axis(~.x)) +
  scale_y_continuous(sec.axis = sec_axis(~.x)) +
  my.theme

reprex package (v0.3.0) 創建於 2020-08-20

或者,您也可以設置例如scale_x_continuous(position = "top") ,但不會繪制底軸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM