简体   繁体   English

将标签沿R的x轴放置在绘图中

[英]Placing the labels in a plot along the x-axis in R

I made a plot in R and now i want to place the labels along the x-axis of the plot. 我在R中绘制了一个图,现在我想将标签沿图的x轴放置。 I want to place the labels in the center of the number of samples that belong to a particular population. 我想将标签放置在属于特定总体的样本数量的中心。 If I have 5 populations with sample size 10, 20,20,35,100 then label should be at 10/2 for population 1, 20/2 for population 2 and so on. 如果我有5个样本量分别为10、20、20、35、100的人口,那么人口1的标签应为10/2,人口2的标签应为20/2,依此类推。 I tried the following but it doesnt work. 我尝试了以下方法,但没有用。 Ill appreciate any further help! 我将不胜感激任何进一步的帮助! sample of my input file (input.5.Q, showing only 5 samples.In real it consists of 185 rows and 5 columns) 输入文件的样本(input.5.Q,仅显示5个样本。实际上它由185行和5列组成)

1   0.364957    0.00001 1.00E-05    0.00001 1.00E-05
2   0.485423    0.017426    1.00E-05    0.00001 1.00E-05
3   0.399289    0.00001 1.00E-05    0.002894    1.00E-05
4   0.579652    0.00001 1.00E-05    0.00001 1.00E-05
5   0.578574    0.00001 1.00E-05    0.00001 1.00E-05
6   0.597478    0.00001 1.00E-05    0.00001 1.00E-05

tbl<-read.table(input.5.Q)    
pop<-c("pop1", "pop2", "pop3", "pop4", "pop5")
    n<-c(10, 20,20,35,100)
    at <- n/2
    barplot(t(as.matrix(tbl)),col=rainbow(5),width=1,space=1)
    mtext(1,at=at,text=pop,cex=1, las=2)
    for{....
    }

You can try: 你可以试试:

tbl <- read.table("clipboard", row.names= NULL) # read your data
head(tbl)
V1       V2       V3    V4       V5    V6
1  1 0.364957 0.000010 1e-05 0.000010 1e-05
2  2 0.485423 0.017426 1e-05 0.000010 1e-05
3  3 0.399289 0.000010 1e-05 0.002894 1e-05
4  4 0.579652 0.000010 1e-05 0.000010 1e-05
5  5 0.578574 0.000010 1e-05 0.000010 1e-05
6  6 0.597478 0.000010 1e-05 0.000010 1e-05
# the barplot
colnames(tbl)[-1] <-c("pop1", "pop2", "pop3", "pop4", "pop5")
barplot(as.matrix(tbl[,-1]),beside = T)

在此处输入图片说明

You could, for example, try something like this (it's not exactly what you wanted, but maybe close enough?): 例如,您可以尝试这样的操作(这不完全是您想要的,但也许足够接近?):

# example data
tbl <- data.frame(population = c(rep("pop1", 10), rep(c("pop2", "pop3"), each = 20), rep("pop4", 35), rep("pop5", 100)), 
              V1 = seq(0, 1, size = 185, replace = TRUE),
              V2 = seq(0, 0.1, size = 185, replace = TRUE),
              V3 = seq(1e-05, 0, size = 185, replace = TRUE),
              V4 = seq(0, 0.01, size = 185, replace = TRUE),
              V5 = seq(1e-05, 0, size = 185, replace = TRUE))


library(gridExtra)
library(grid)
library(ggplot2)
library(lattice)

I created a function for the plots, to make it easier to plot each variable individually. 我为绘图创建了一个函数,以使其更容易分别绘制每个变量。 I also tried putting all variables into one plot but this came out not very nice - too messy 我也尝试将所有变量放在一个图中,但是结果不是很好-太乱了

plot_func <- function(column){
  ggplot(data = tbl, aes_string(x = 1:nrow(tbl), y = column, fill = "population")) + 
    geom_bar(stat = "identity") + 
    theme(axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
    theme(legend.title = element_blank(), legend.position = "bottom")
}

# run the plot function for each variable and plot
for (i in 2:ncol(tbl)){
  assign(paste("p", i-1, sep = ""), plot_func(column = paste0(colnames(tbl)[i])))
}

grid.arrange(p1, p2, p3, p4, p5, ncol=2)

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM