我想绘制我的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,我都非常乐意为您提供帮助。
感谢。
答案 0 :(得分:1)
这就是你的意思吗?
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)`