I am plotting multiple vectors in one graph with ggplot2. I have the following dataframe
| Array | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Arr1 | 0.1 | 0.1 | 0.1 | 0.2 | 0.2 | 0.2 | 0.7 | 0.7 | 0.4 | 0.7 |
| Arr2 | 0.6 | 0.6 | 0.6 | 0.1 | 0.1 | 0.1 | 0.1 | 0.1 | 0.5 | 0.1 |
| Arr3 | 0.3 | 0.3 | 0.3 | 0.7 | 0.7 | 0.7 | 0.2 | 0.2 | 0.1 | 0.2 |
| Arr4 | 0.4 | 0.6 | 0.7 | 0.2 | 0.1 | 0.3 | 0.4 | 0.5 | 0.3 | 0.9 |
| B | a | a | a | b | b | b | a | a | a | b |
| C | b | b | b | a | a | b | b | a | b | a |
So I melt the data to plot all vectors with horizontal stack bar, which is as follows:
df2<-df %>%
melt(id.vars = "Array") %>%
mutate(variable = str_extract(variable, "[0-9]+")) %>%
mutate(value = case_when(
value == "a" ~ 1,
value == "b" ~ 2,
TRUE ~ as.numeric(value)
)) %>%
mutate(variable = as.numeric(variable))
df2 %>%
ggplot(aes(x = Array, y = variable, group = Array, fill = value)) +
geom_col() + coord_flip()
But the x-axis is not proper, the image shows that same number of a and b in vector B has different sizes, also last single element has bigger size than first three. The problem in x-axis is easier to detect with vector B and C then Arr.
When you look at df2 variable only has 1 to 10, I cannot figure out how there are more than 50 points on x - axis.


library(tidyverse)library(reshape2)?library(tidyverse) and library(reshape2)are used