I try to make a plot with an character-/factor-yaxis and mutiple values for x in R.
Essentially: the following graph turned by 90 degrees:
df <- data.frame(A = c(2,3,5,5,2), B = c(1,2,6,6,5))
rownames(df) <- c("02", "07", "08", "12", "99")
matplot(df, pch="x", xaxt = "n")
axis(side=1,at=1:nrow(df), labels = rownames(df))
(why would I want to do that? because I don't want my audience to look for patterns in the wrong direction. This apparently happens with my data, and turning the graph seems to help.)
It is very important to note that the rownames are codes and must be treated as characters/factors
I already tried, but none of these works:
matplot(df, factor(rownames(df)), pch = "x")
matplot(df, as.factor(rownames(df)), pch = "x")
matplot(df, as.character(rownames(df)), pch = "x")
Do I suspect correctly, that matplot is unable to accept character/factor input for yaxis?
I also tried ggplot, but I was unable to give it mutiple values for x. Also, I would prefer a solution without adding data separately, because I have at least 15 values for each observation in my original data.
ggplot(df, aes(x=df$A, y=rownames(df))) + geom_point(data = df$B)
maybe I am missing an obvious solution here?
some package I don't know of yet?
thank you!
For ggplot, you can plot a separate
geom_point
layer for each column in your dataset (i.e. one for "A" & one for "B"), but it's neater to convert your data frame to long format & use ggplot's aesthetic mapping: