繁体   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