I would like to plot my 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)
which looks likes this:
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`)))
I get:
What I really would like to have is something like this:
column 3 to 6 should be the labels that have different values for each value of column 7 (sorry for not having the appropriate bars, names and amount of options in this pictures)
I am very happy for any help, no matter if it includes barplot, ggplot 2 or plotly.
Thanks.
Is this what you what?
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`)))

In response to whether an axis() type function is available in ggplot2:
Not that I know of. However, you can combine the labels into one vector:
labels <- paste0(M, "\n", N, "\n", O, "\n", P)
and then use scale_x_continuous. Here's a quick example:
ggplot(data.frame(x = 1:8, y = 3:10)) + geom_point(aes(x,y)) +
scale_x_continuous(breaks = 1:8, labels = labels)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With