3

我正在用不同的轴范围制作几个不同的图,所以这个问题不仅仅适用于我在这里展示的代码。我尝试修改刻度线的中断和间隔,但对于某些绘图,y 轴总是在最后一次中断后继续。但对于某些人来说,它就像这样工作得很好。

这是我当前的情节和代码。我想用 60 上的刻度线结束 y 轴:

在此处输入图像描述

# Set nucleotide sequence for x-axis labels
my_labs = c("C", "T", "A", "C", "A", "T", "A", "A", "A", "T", "A", "C", "A", "C", "A", "T", "G", "T", "C", "T", "C", "T", "G", "C", "T", "C", "G", "T", "T", "C", "G", "G", "G", "G", "G", "C", "C", "G", "G", "T", "A", "T", "G", "C", "T", "A", "C", "A", "C", "G", "G", "A", "A", "C", "G", "T", "G", "A", "G", "A", "G", "A", "C", "C", "C", "C", "T", "C", "G", "G", "A", "A", "C", "T", "G", "G", "C", "A", "T", "A", "G", "A", "C", "T", "T", "G", "T", "G", "T", "A", "T", "A", "A", "A", "A", "G", "A", "A", "T")

# Set color of each nucleotide
my_cols = c("Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black")

ggplot(data = miRNA2) + 
  geom_line(mapping = aes(x = Position, y = CPM), colour="red") +
  scale_y_continuous(breaks = seq(0, 60, 10)) +
  ylab("Counts per million") +
  scale_x_continuous(breaks=1:99, labels=my_labs, expand = c(0, 0)) +
  theme(axis.text.x = element_text(color = my_cols, family = "Courier", size = 6),
        panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank(), panel.grid.minor.y=element_blank(), panel.background = element_blank(),
        axis.line = element_line(colour = "black")) +
xlab("Supercontig_1.420:40270-40368") +
  ggtitle("Sar-Mir-Nov-2") +
  theme(plot.title = element_text(hjust = 0.5))
4

2 回答 2

6

只需将limits = c(0,60)和添加expand = c(0,0)到您的scale_y_continuous

scale_y_continuous(breaks = seq(0, 60, 10), 
                   limits = c(0,60), 
                   expand = c(0,0))
于 2017-02-27T12:46:33.037 回答
2

您是否要设置 ylim max 以便显示的最后一个中断四舍五入到最接近的 10?以及以 10 为增量的所有中断?

您可以通过获取 gg 对象的 y 范围来获取 ggplot 的当前 ylim,并将ggplot_build(gg)$layout$panel_ranges[[1]]$y.rangeylim 最大值的上限设置为最接近的 10,并同样设置您的休息时间。通过这种方式,您可以根据绘图动态设置限制和中断。

gg <- ggplot(data = miRNA2) + 
  geom_line(mapping = aes(x = Position, y = CPM), colour="red") +
  scale_y_continuous(breaks = seq(0, 60, 10)) +
  ylab("Counts per million") +
  scale_x_continuous(breaks=1:99, labels=my_labs, expand = c(0, 0)) +
  theme(axis.text.x = element_text(color = my_cols, family = "Courier", size = 6), 
  panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank(),
  panel.grid.minor.y=element_blank(), panel.background = element_blank(),
  axis.line = element_line(colour = "black")) +
  xlab("Supercontig_1.420:40270-40368") +
  ggtitle("Sar-Mir-Nov-2") +
  theme(plot.title = element_text(hjust = 0.5))


# grab y-range of current gg object
ggplot_build(gg)$layout$panel_ranges[[1]]$y.range
gg_ylim_max <- ggplot_build(gg)$layout$panel_ranges[[1]]$y.range[2]

# decide if you want to ceiling to nearest 10's or 5's
library(plyr)
round_any(gg_ylim_max, 10, f = ceiling) # ceiling 10's
round_any(gg_ylim_max, 5, f = ceiling) # ceiling 5's

请试试这个,我没有你的数据,所以我不知道它是否适合你。

gg_ylim_max_round <- round_any(gg_ylim_max, 10, f = ceiling)
gg + scale_y_continuous(breaks = seq(0, gg_ylim_max_round, 10), limits = c(c(0, gg_ylim_max_round)))

编辑了限制的冗余,代码现在应该是正确的。

于 2017-02-27T13:21:31.160 回答