R 从多列绘制 x 轴标签

R plot x axis label from multiple columns

我想绘制我的 data.frame:

A_ID <- c("A1", "A2", "A3", "A4", "A5","A6","A7", "A8")
B_ID <- c("B1","B2","B3","B4","B1","B2","B3","B4")
M <- c("+", "+","+","+","+","+","+","+")
N <- c("+", "+","+","+","-","-","-","-")
O <-c("+", "+","-","-","+","+","-","-")
P <-c("+", "-","+","-","+","-","+","-")
value <- c (1,2,3,4,5,6,7,8)
df <- data.frame(A_ID, B_ID,M,N,O,P,value)

看起来像这样:

A_ID   B_ID   M   N   O   P   value
A1     B1     +   +   +   +   1
A2     B2     +   +   +   -   2
A3     B3     +   +   -   +   3
A4     B4     +   +   -   -   4
A5     B1     +   -   +   +   5
A6     B2     +   -   +   -   6
A7     B3     +   -   -   +   7
A8     B4     +   -   -   -   8


barplot(as.matrix(df[,7]),main="tobefound", 
    horiz = FALSE,width = 0.5, 
    #names.arg= 
    las=2,
    ylim = c(0,10),
    col = 1+as.numeric(as.factor(df$`B_ID`)),
    border = NA,
    beside= TRUE)
box()
legend('topleft', fill = 1+as.numeric(as.factor(levels(df$`B_ID`))),
legend = levels(as.factor(df$`B_ID`))) 

我得到:

我真正想要的是这样的: 第 3 列到第 6 列应该是第 7 列的每个值具有不同值的标签(抱歉,此图片中没有适当的条、名称和选项数量)

我很高兴得到任何帮助,无论它是否包括 barplot、ggplot 2 或 plotly。

谢谢。

这是你的什么?

barplot(as.matrix(df[,7]),main = "tobefound", 
        horiz = FALSE, width = 0.5, 
        las = 1, 
        ylim = c(0,10), 
        xlim = c(.5, 4.5),
        col = 1 + as.numeric(as.factor(df$`B_ID`)),
        border = NA,
        beside = TRUE, 
        xaxs = "i")
tickloc <- c(.5, seq(.75, 4.25, .5))
axis(side = 1, at = tickloc, labels = c("M", M))
axis(side = 1, at = tickloc, labels = c("N", N), tick = FALSE, line = 1)
axis(side = 1, at = tickloc, labels = c("O", O), tick = FALSE, line = 2)
axis(side = 1, at = tickloc, labels = c("P", P), tick = FALSE, line = 3)
box()
legend('topleft', fill = 1+as.numeric(as.factor(levels(df$`B_ID`))),
       legend = levels(as.factor(df$`B_ID`)))

响应ggplot2中是否有axis()类型的函数:

据我所知没有。但是,您可以将标签组合成一个向量:

labels <- paste0(M, "\n", N, "\n", O, "\n", P)

然后使用scale_x_continuous。这是一个简单的例子:

ggplot(data.frame(x = 1:8, y = 3:10)) + geom_point(aes(x,y)) +
    scale_x_continuous(breaks = 1:8, labels = labels)