Formatting X-Axis Data for GGplot

Hi All,

I am having some trouble formatting x-axis data to plot in R.

The main problem I am having is that I would like the format the X axis as a grouping of 5 independent variables (X0, X1, X2, X3, X4).

Taken together, these 5 independent variables form a survey method. The fill variable should be the value taken on the survey Q (i.e. a value of 1-3), and the y-axis should be the count.

I wondered if it would be possible if someone could show me how to group/merge/melt the X's into the equivalent of a single variable that could be plotted on the x-axis?

Would be greatly appreciated.

X0 <- c(1,2,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,1,1,1,1)
X1 <- c(3,3,1,1,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,1,1,1,1)
X2 <- c(1,2,1,1,1,1,1,1,3,2,2,3,2,2,2,2,2,2,2,1,1,1,1)
X3 <- c(1,2,3,2,2,2,2,2,3,2,2,1,3,2,2,2,2,2,2,1,1,3,1)
X4 <- c(1,2,3,3,3,3,3,3,3,2,2,2,2,2,2,2,1,1,1,1,1,2,3)

dat <- data.frame(X0, X1, X2, X3, X4)

X0 X1 X2 X3 X4
1   1  3  1  1  1
2   2  3  2  2  2
3   3  1  1  3  3
4   3  1  1  2  3
5   3  3  1  2  3
6   3  3  1  2  3
7   3  3  1  2  3
8   3  3  1  2  3
9   3  3  3  3  3
10  2  2  2  2  2
11  2  2  2  2  2
12  2  2  3  1  2
13  2  2  2  3  2
14  2  2  2  2  2


Is this what you need?

X0 <- c(1,2,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,1,1,1,1)
X1 <- c(3,3,1,1,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,1,1,1,1)
X2 <- c(1,2,1,1,1,1,1,1,3,2,2,3,2,2,2,2,2,2,2,1,1,1,1)
X3 <- c(1,2,3,2,2,2,2,2,3,2,2,1,3,2,2,2,2,2,2,1,1,3,1)
X4 <- c(1,2,3,3,3,3,3,3,3,2,2,2,2,2,2,2,1,1,1,1,1,2,3)
dat <- data.frame(X0, X1, X2, X3, X4)
library(tidyr)
datLong <- pivot_longer(data = dat,cols = everything(),
                         names_to = "Variable",values_to = "Value")
datLong
# A tibble: 115 × 2
   Variable Value
   <chr>    <dbl>
 1 X0           1
 2 X1           3
 3 X2           1
 4 X3           1
 5 X4           1
 6 X0           2
 7 X1           3
 8 X2           2
 9 X3           2
10 X4           2
# … with 105 more rows
# ℹ Use `print(n = ...)` to see more rows

Hi @FJCC. Yes, thank you! Formatting is exactly what I am looking for.

I am struggling with getting it into a stacked bar graph form with the breaks as value levels (1,2,3) however. Do you know how that is possible please?

datLong %>% 
  ggplot(aes(x = Variable, fill = Value)) +
  geom_bar() +
  xlab("Survey Variables") +
  ylab("Counts\n") +
  scale_fill_viridis(discrete = T, option = "D") +
  theme_bw()

I think you need to make a factor out of Value for the fill to work as intended.

X0 <- c(1,2,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,1,1,1,1)
X1 <- c(3,3,1,1,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,1,1,1,1)
X2 <- c(1,2,1,1,1,1,1,1,3,2,2,3,2,2,2,2,2,2,2,1,1,1,1)
X3 <- c(1,2,3,2,2,2,2,2,3,2,2,1,3,2,2,2,2,2,2,1,1,3,1)
X4 <- c(1,2,3,3,3,3,3,3,3,2,2,2,2,2,2,2,1,1,1,1,1,2,3)

dat <- data.frame(X0, X1, X2, X3, X4)
library(tidyr)
datLong <- pivot_longer(data = dat,cols = everything(),
                        names_to = "Variable",values_to = "Value")
library(ggplot2)

ggplot(datLong,aes(Variable, fill = factor(Value))) + geom_bar() +
  labs(fill ="Value")

Created on 2022-10-22 with reprex v2.0.2

Thank you @ FJCC. Very much appreciated

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.