简体   繁体   English

如何使用facet_grid在ggplot中安装X轴标签?

[英]How can I fit X-axes labels in ggplot using facet_grid?

I have this code to plot my data d . 我有这段代码来绘制我的数据d I get a nice plot with the code below except the X-axes are clumped together. 我用下面的代码得到了一个不错的图,除了X轴聚集在一起。 How can I separate the axis values to look nice in this plot? 如何在此图中分离轴值以使其看起来更好?

my data : 我的数据:

d<- structure(list(chr = c("chr10", "chr10", "chr10", "chr11", 
"chr11", 
"chr11", "chr12", "chr12", "chr12", "chr13", "chr13", "chr13", 
"chr14", "chr14", "chr14", "chr15", "chr15", "chr15", "chr16", 
"chr16", "chr16", "chr1", "chr1", "chr1", "chr2", "chr2", "chr2", 
"chr3", "chr3", "chr3", "chr4", "chr4", "chr4", "chr5", "chr5", 
"chr5", "chr6", "chr6", "chr6", "chr7", "chr7", "chr7", "chr8", 
"chr8", "chr8", "chr9", "chr9", "chr9"), start = c(6, 14, 19, 
3, 10, 16, 60, 73, 80, 4, 11, 27, 67, 75, 81, 2, 7, 15, 13, 99, 
142, 1, 8, 21, 49, 53, 78, 92, 121, 165, 35, 42, 47, 5, 9, 17, 
12, 20, 34, 44, 51, 56, 61, 66, 94, 18, 31, 37), stop = c(6, 
14, 19, 3, 10, 16, 60, 73, 80, 4, 11, 27, 67, 75, 81, 2, 7, 15, 
13, 99, 142, 1, 8, 21, 49, 53, 78, 92, 121, 165, 35, 42, 47, 
5, 9, 17, 12, 20, 34, 44, 51, 56, 61, 66, 94, 18, 31, 37), density = 
c(10, 
4, 1, 13, 1, 1, 18, 48, 526, 3, 1, 1, 13, 74, 177, 1, 3, 5, 9432, 
3, 5, 1, 32, 1, 60, 4, 3, 2, 1, 10, 4, 10, 6, 6, 2, 3, 5, 65220, 
11136, 1, 25, 36, 5, 6, 1, 4, 7, 11)), .Names = c("chr", "start", 
"stop", "density"), row.names = c(NA, -48L), class = "data.frame")

my code: 我的代码:

  d$chr <- factor(d$chr, levels = c('chr1', 'chr2', 'chr3', 'chr4', 
  'chr5', 'chr6', 'chr7', 'chr8', 'chr9', 'chr10', 'chr11', 'chr12', 
  'chr13', 'chr14', 'chr15', 'chr16'))
  library(ggplot2)
  ggplot(d, aes(start, density)) +
  # ggplot(d, aes(start, density)) +
  geom_line(alpha = 0.9) +
  # facet_grid(. ~ chr) +
  facet_grid(. ~ chr, scales = "free_x")  +

  # limits from the data frame
  scale_x_continuous(expand = c(0, 0),
                     breaks = function(x) x) +

  labs(title = "Density profiles along the chromosomes",
       x = "Coordinate, bp",
       y = "Density") +
  theme(plot.title = element_text(hjust = 0.5),

  # align x-axis labels inwards for easier reading
  axis.text.x = element_text(hjust = c(-0.1, 4))) 

  filename <- "plot.tiff"
  ggsave(file = paste0(plot.dir,filename), dpi = 300, width = 8, 
  height = 6, units = "in")
(ggplot(d, aes(start, density)) +
  geom_line(alpha = 0.9) +
  scale_x_continuous(name = "Corodinate, bp", expand = c(0, 0), breaks = function(x) x) +
  scale_y_continuous(labels=scales::comma) +
  facet_grid(. ~ chr, scales = "free_x")  +
  labs(title = "Density profiles along the chromosomes") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.spacing.x=unit(12, "pt")) +
  theme(axis.text.x = element_text(size=6, hjust = c(0, 1))) -> gg)

filename <- "plot.tiff"
ggsave(file = filename, plot = gg, dpi = 300, width = 8, height = 6, units = "in")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 r-如何在ggplot2中使用facet_grid获得长标签以适合? - r - How can I get a lengthy label to fit using facet_grid in ggplot2? R:{ggplot2}:如何/我可以独立调整 facet_grid plot 上的 x 轴限制? - R: {ggplot2}: How / Can I independently adjust the x-axis limits on a facet_grid plot? 如何更改ggplot中facet_grid的顺序和名称? - How can I change the order and name of the facet_grid in ggplot? 使用R中的facet_grid的ggplot2 boxplot中的表达式分别更改x标签 - Individually change x labels using expressions in ggplot2 boxplot with facet_grid in R 如何使用 ggplot2 facet_grid 注释没有数据的构面? - How can I annotate facets that do not have data using ggplot2 facet_grid? 在facet_grid,ggplot中控制间距和标签 - Control spacing and labels in facet_grid, ggplot 在ggplot中使用facet_grid时如何为每个不同的面板指定不同的标签 - How to specify different labels for each distinct panels when using facet_grid in ggplot 如何在 facet_grid() function 中使用 ggplot2 增加条形标签的大小? - How to increase the size of the bar labels using ggplot2 in a facet_grid() function? 在facet_grid ggplot上强制x轴标签:x轴标签每行不同 - Force x-axis labels on facet_grid ggplot: x-axis labels differ per row 在 ggplot2 facet_grid 中旋转切换的刻面标签 - Rotate switched facet labels in ggplot2 facet_grid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM