gpt4 book ai didi

R从多列绘制x轴标签

转载 作者:行者123 更新时间:2023-12-04 03:14:03 25 4
gpt4 key购买 nike

我想绘制我的 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`)))

我得到: enter image description here

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

我很高兴得到任何帮助,无论它是否包含 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`)))
enter image description here
回复是否是 axis() type 函数在 ggplot2 中可用:
从来没听说过。但是,您可以将标签合并为一个向量:
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)
enter image description here

关于R从多列绘制x轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42598227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com